1

I have a batch file located in several folders and in this file I need to replace the old subst T: . command for net use because the units (drives) have different properties in windows 7.

However, I have only found that net use only allows for absolute paths such as:

net use T: \\path\to\folder

I cannot work with absolute paths, but net use T: . is not allowed (it throws an error).

Is there a way to execute the net use command from a folder so that it uses the current .bat directory?

I think the best solution for me it would be to be able to get the absolute qualified path (\path\to\folder) with a batch command. (%cd% and chdir variables do not work in this case). Questions about getting the current directory or folder name are not useful since the net usecommand does not allow paths such as x:\path\to\folder

3
  • What are you trying to accomplish? Create a 1:1 replacement for subst? Also, I don’t really understand why subst isn’t sufficient anymore. Could you perhaps elaborate? – Daniel B Jan 18 '16 at 18:17
  • Of course. I did not go into details because it was not within the scope of the question. When running not-signed executables from a network-drive created with subst windows always prompts the message: "publisher could not be verified...", and I need the message not to be displayed. – Jorge Jan 19 '16 at 9:00
  • @DanielB. Continuation: The thing is that I have lowered all local network security and tried it all to avoid this message but it keeps popping whenever I run it. However, if the network-drive is created from the computer --> map network drive button, even if the related folder path is the same it behaved differently from subst. Windows does not show me the message any more this way. So what I need is a windows command to do what that button does. – Jorge Jan 19 '16 at 9:08
1

The %~dp0 will give exactly what you're asking for (i.e. \\server\share\to\folder) . But you can't use it for your net use command. The net use command only accepts the \\server\share part to create a drive-letter.

You can do some work to strip off the \to\folder-part and add it later in your batch-files but it might be better to use the pushd \\server\share\to\folder command. With that command there is a temporary driver-letter created and the current directory is automatically changed to the correct folder. With the popd-command you're back where you started and the temporary drive is released.

So:

C:\>

C:\>pushd \\wdmycloud\public\new folder

Z:\New folder>::do your thing
Z:\New folder>
Z:\New folder>popd

C:\>

B.T.W. if you need to find out what temporary drive is created you can use the %~d0 in your batch-files. And %~dp0 for the complete path, and so on (or %cd% of course :)).

10
  • I have tried it and it does not work. A command like this: net use T: c:\path\to\folder does not work. Net use expects UNC paths such as \\server\path – Jorge Jan 18 '16 at 16:18
  • @Jorge The ~dp0 should give you a fully qualified UNC-path if you put the batch-file on the network and start it via the UNC path. It doesn't work if you start it from a drive letter already. What do you get with the echo-example? – Rik Jan 18 '16 at 17:24
  • @Jorge O wait. This might give you trouble with subfolders. ~dp0 will give you the complete subfolder-path which you can't use for net use. You would need to strip those. But wouldn't pushd be a better option or do you need it to be T: specifically. If you do pushd \\path\to\folder you end up in Z:\folder with Z: being a temporary drive-letter. With popd you go back and Z: is released. Isn't that a better option ?? (Otherwise I'll try to find a way to strip the folder for you) – Rik Jan 18 '16 at 17:46
  • @Jorge Seeing as that you can't use \\server\share\to\folder for net use-command you can't use %~dp0 either. I changed my answer completely to using pushd which would be much more suitable in this situation. – Rik Jan 18 '16 at 18:05
  • Ok, I dont know if I am missing something, but this is what is happening to me: I already have \\server\general\path mapped to X:\ (all at work have it this way). What I need now is a subfolder of X: to be mapped to T:. So: \\server\general\path\specific\path --> T:. But, when I open a command window from \\server\general\path and I execute a bat file with echo ~dp0 it gives me X:\specific\path, so ~dp0 is not working for me. – Jorge Jan 19 '16 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.