在输入icacls外部变量时出现奇怪的问题


早上好

我有一个批处理脚本的问题。我有一个程序,它提供了一个变量,我用这个变量来创建一个文件夹,然后应用Icalcs权限。由于某些原因,它将创建具有变量名称的文件夹,但Icalcs将在变量应该是空的地方。下面是代码-

set whodo=%2
set username=%whodo%
set path="\examplesharesStudent%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:windowssystem32icacls.exe %path% /T /C /inheritance:e /grant:r %username%:(OI)(CI)M

%2正在从运行该脚本的程序中提取变量,然后我将该变量放入另一个变量中,看看这是否会使Icacls高兴,但它没有。如果没有从程序中提取的变量,这个脚本可以正常工作。我无法弄清楚为什么路径和用户名变量工作无处不在,但Icacls。这是icacls的缺陷吗?

谢谢

打开命令提示符窗口,运行set,输出预定义的环境变量列表。有关每个预定义环境变量的描述,请参见Wikipedia关于Windows环境变量的文章。

预定义的环境变量USERNAMEPATH不应该在批处理文件中修改,除非有很好的理由这样做。

在使用set variable="value"而不是set "variable=value"时也要小心,因为在第一种情况下,双引号也作为字符串值的一部分分配给环境变量,也可能是现有的末尾空格/制表符。有关详细描述,请参阅

上的答案。
  • 如何用空格设置环境变量?
  • 为什么在命令行上使用'set var = text'后没有'echo %var%'的字符串输出?

包含1个或更多空格的字符串必须用双引号括起来,因为如果在双引号字符串中没有找到空格字符,则使用空格字符作为字符串分隔符。用户名可以包含一个空格。目录名Saved Games肯定包含一个空格。

我建议使用这个批处理代码:

rem Get name of user with surrounding double quotes removed.
set "whodo=%~2"
set "NameUser=%whodo%"
set "PathUser=\examplesharesStudent%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%System32icacls.exe "%PathUser%" /T /C /inheritance:e /grant:r "%NameUser%:(OI)(CI)M"

为了理解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • call /?…解释%~2(第二个参数不带引号)。
  • cmd /?…当需要双引号时,在最后一个帮助页解释。
  • icacls /?
  • md /?
  • rem /?
  • set /?

最新更新