1

I have a batch script that checks to see if a directory exists then maps it, if it does exist.

if exist \\server\folder1\%username% net use g: \\server\folder1\%username%

Lately the script hasn't been mapping correctly with some computers, on those computers in needs to be change to

if exist \\server\folder1\%username%\ net use g: \\server\folder1\%username%

Why would that backslash make a difference? Isn't it pointing to the same directory? Why would "if exist" need it and "net use" not need it? Users do NOT have access to folder1.

Now, I came across an older version of the same file written by a previous employee and he wrote it as

net use g: \\\\server\folder1\%USERNAME%

Why would he put four slashes?

user2517266
  • 335
  • 1
  • 2
  • 6
  • for your first question maybe there is a whitespace in the username ? – Loïc MICHEL Jun 28 '13 at 15:58
  • I see the same behavior WRT `IF EXIST` with and without the backslash. I don't think that's the problem. As a rule, I always enclose paths in quotes to account for the possibility of them containing spaces or other special characters (hopefully, double quotes aren't allowed in user names). For the last question, the only reason someone would want to use four backslashes is if the value was in a string that was going to be interpolated. This doesn't work at all in batch/CMD. It's more likely a path copied from a Perl script (or something similar), like `'\\\\server\share\dir1'` – mojo Jun 28 '13 at 16:04

1 Answers1

2

I'm not sure if this is your problem, but if exist \\server\folder1\%username% is TRUE if
%username% is a valid file or folder within folder1.

Adding a backslash at the end forces the condition to only be true if a folder exists.

dbenham
  • 127,446
  • 28
  • 251
  • 390