Question

I'm creating a batch script to pull permissions from all directories (not individual files) from all locally fixed disks on a system. Here's what works; I am able to create a temp file that lists all locally fixed NTFS partitions. I'm able to take the first line from that temp file (normally the C: drive) and run a dir command against it and the icacls/cacls command against it. The problem I'm having is it doesn't cycle back through to run the dir command and the icacls/cacls command against any other drives listed in the temp file. Any help would be appreciated. Here is my script:

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
Was it helpful?

Solution

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

I do so dislike labels within a code block. I believe that under some editions, they don't work and terminate the block. Try this - it's just an edited version of your original but without the labels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top