Net Use command with relative target path

1

0

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

Jorge

Posted 2016-01-18T15:49:38.443

Reputation: 111

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 – 2016-01-18T18:17:57.827

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 – 2016-01-19T09:00:23.680

@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 – 2016-01-19T09:08:18.103

Answers

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 :)).

Rik

Posted 2016-01-18T15:49:38.443

Reputation: 11 800

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 – 2016-01-18T16:18:36.453

@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 – 2016-01-18T17:24:59.173

@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 – 2016-01-18T17:46:07.193

@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 – 2016-01-18T18:05:51.987

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 – 2016-01-19T09:25:26.427

What we use to have was this command: subst T: .(with the dot meaning the current folder). By the way, anytime I open a command window from a network drive subfoler windows creates a new drive letter! I dont know why this is happening. Is it related to those temporary drive-letters? – Jorge – 2016-01-19T09:28:41.573

@Jorge If you're on a drive-letter in a command-prompt and you go to a directory X:, cd folder and you do subst T: . when you go to T: you'll be in X:\folder. So the command subst T: . works just fine for me. That opening a folder directory on a network drive you get a temporary driveletter is normal. The cmd.exe detects that you don't have a letter so assigns one temporarily. This only happens with a command prompt. Not with a batch-file. With a batch-file you need to issue a pushd %~dp0 first to get that temporary drive-letter. – Rik – 2016-01-19T10:09:21.007

Let us continue this discussion in chat.

– Jorge – 2016-01-19T10:21:32.627

But although subst creates a new drive letter with the path it has a different behaviour as commented in the original post – Jorge – 2016-01-19T10:29:48.767

The %~dp0 ONLY gives a qualified UNC-path if you're really run it FROM a UNC-path. If you run it from a driveletter then you also get a drive-letter. But if you run it from a drive-letter you don't need %~dp0 because you can use relative path with subst. So if you HAVE a driveletter you can just use subst T: . and if you DON'T have a driveletter you need to use pushd %~dp0 to get a letter, after which you could use the subst T: .-method. – Rik – 2016-01-19T11:03:24.450