batch-filevariablesicacls

Odd issue when feeding icacls an external variable


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


Solution

  • 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.