1

Good morning.

I am having an issue with a batch script I have. I have a program that feeds it a variable and I use that variable to create a folder and then apply Icalcs permissions on it. For some reason it will create the folder with the variable name but Icalcs will be blank where the variable should be. Here is the code -

set whodo=%2
set username=%whodo%
set path="\\example\shares\Student\%username%"

md %path%
md %path%\Desktop
md %path%\Contacts
md %path%\Favorites
md %path%\Links
md %path%\Music
md %path%\Pictures
md %path%\Saved Games
md %path%\Searches
md %path%\Video
md %path%\Documents

c:\windows\system32\icacls.exe %path% /T /C /inheritance:e /grant:r %username%:(OI)(CI)M

The %2 is pulling the variable from the program that runs this script, I was then putting the variable into another variable to see if that would make Icacls happy, but it doesn't. Without the variable pulled from the program this script works fine. I cannot figure out why the Path and Username variables work everywhere but Icacls. Is this some flaw icacls has?

Thanks

MattMack
  • 23
  • 6
  • May be %2 contain spaces? Check its value with echo. Try enclose %path% and %username% into brackets (ie "%path"). – alexander Oct 17 '16 at 21:44
  • Hey, i output to a text file. Here is the result for the path variable - \\example\shares\Student\"16137"" . the username comes out as "16137". – MattMack Oct 17 '16 at 22:05
  • With brackets does the brackets just inclose like this ? - (%path%) and (%username%) – MattMack Oct 17 '16 at 22:08
  • The thing is, the MD command is happy with the path variable, and if I make the whodo variable some random string then the script works. It is just that icacls for some reason doesn't like the variable if it came from the program. – MattMack Oct 17 '16 at 22:42
  • 2
    "the" variable? Which variable? Are you aware that both "path" and "username" are predefined reserved variablenames which are established by the system? `path` is the sequence of directories searched by the system if a required executable isn't found in the curent directory and `username` is, believe it or not, the current username. Have you tried this batch with `echo` on and observed what the actual `icacls` command is resolved to? Is that resolution as you expect? – Magoo Oct 18 '16 at 06:03
  • Thanks for the tip, when looking at what icacls resolved to it was correct (apart from the quotes which is fixed with the script posted below). – MattMack Oct 18 '16 at 20:08

1 Answers1

1

Open a command prompt window and run set to get output the list of predefined environment variables. For a description of each predefined environment variable see for example Wikipedia article about Windows Environment Variables.

The predefined environment variables USERNAME and PATH should not be modified in a batch file except there is a really good reason to do that.

Also be careful on using set variable="value" instead of set "variable=value" because in first case the double quotes are also assigned as part of the string value to the environment variable and perhaps existing trailing spaces/tabs, too. For a detailed description read the answers on

And strings containing 1 or more spaces must be enclosed in double quotes as the space character is used as string separator if not found within a double quoted string. The name of the user could contain a space. The directory name Saved Games contains definitely a space.

I suggest to use this batch code:

rem Get name of user with surrounding double quotes removed.
set "whodo=%~2"
set "NameUser=%whodo%"
set "PathUser=\\example\shares\Student\%NameUser%"

rem Create directories for this user on server. With command extensions
rem enabled as by default the command MD creates the entire directory
rem tree if that is necessary. Therefore it is not necessary to create
rem separately the profile directory of the user first.
md "%PathUser%\Desktop"
md "%PathUser%\Contacts"
md "%PathUser%\Favorites"
md "%PathUser%\Links"
md "%PathUser%\Music
md "%PathUser%\Pictures"
md "%PathUser%\Saved Games"
md "%PathUser%\Searches"
md "%PathUser%\Video"
md "%PathUser%\Documents"

%SystemRoot%\System32\icacls.exe "%PathUser%" /T /C /inheritance:e /grant:r "%NameUser%:(OI)(CI)M"

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~2 (second argument without surrounding quotes).
  • cmd /? ... explains on last help page when double quotes are needed.
  • icacls /?
  • md /?
  • rem /?
  • set /?
Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115
  • I have been going through your info and practicing, I'm not the best programmer as you can probably tell. Your script worked perfectly. The quote placement on variables is very helpful. – MattMack Oct 18 '16 at 22:51
  • I will stay away from %path% and %username%, it is interesting that they worked, I guess overwriting those EVs can cause huge problems. – MattMack Oct 18 '16 at 22:59
  • I have to make a similar script shortly so I will hopefully have more success on the next one. Thank you very much. – MattMack Oct 18 '16 at 23:01