运行第一个变量后,批处理文件中的嵌套for循环关闭

时间:2014-01-28 17:56:32

标签: batch-file for-loop icacls

我正在创建一个批处理脚本,以从系统上所有本地修复的磁盘中提取所有目录(而非单个文件)的权限。这是有效的;我能够创建一个列出所有本地修复的NTFS分区的临时文件。我能够从该临时文件(通常是C:驱动器)获取第一行并对其运行dir命令并对其执行icacls / cacls命令。我遇到的问题是它不会循环回来运行dir命令和icacls / cacls命令对临时文件中列出的任何其他驱动器。任何帮助,将不胜感激。这是我的剧本:

wmic logicaldisk get caption, drivetype, filesystem | find "3" | find "NTFS" | find /v "Caption" > "tmp_drives.txt"

setlocal enableDelayedExpansion
for /f "delims=:" %%A in (tmp_drives.txt) do (

    dir %%A:\ /ad /s /b >> "%%A_dir.txt"

    set drive=%%A

    if exist "%systemroot%\system32\icacls.exe" goto icacls

    for /f "tokens=* usebackq" %%J in ("!drive!_dir.txt") do cacls "%%J" | find /v   "GENERIC_READ" | find /v "GENERIC_EXECUTE" >> "!drive!_Permissions.txt" 
    goto end

    :icacls
    for /f "tokens=* usebackq" %%J in ("!drive!_dir.txt") do icacls "%%J" | find /i /v "Successfully" >> "!drive!_Permissions.txt"

    :end
)
setlocal disableDelayedExpansion
del tmp_drives.txt
pause

1 个答案:

答案 0 :(得分:0)

wmic logicaldisk get caption, drivetype, filesystem | find "3" | find "NTFS" | find /v "Caption" > "tmp_drives.txt"

setlocal enableDelayedExpansion
for /f "delims=:" %%A in (tmp_drives.txt) do (

 dir %%A:\ /ad /s /b >> "%%A_dir.txt"

 set drive=%%A
 if exist "%systemroot%\system32\icacls.exe" (
  for /f "tokens=* usebackq" %%J in ("!drive!_dir.txt") do icacls "%%J" | find /i /v "Successfully" >> "!drive!_Permissions.txt"
 ) else (
  for /f "tokens=* usebackq" %%J in ("!drive!_dir.txt") do cacls "%%J" | find /v   "GENERIC_READ" | find /v "GENERIC_EXECUTE" >> "!drive!_Permissions.txt" 
 )
)
setlocal disableDelayedExpansion
del tmp_drives.txt
pause

我不喜欢代码块中的标签。我相信在某些版本下,它们不起作用并终止块。试试这个 - 它只是原版的编辑版本,但没有标签。