1

I'm trying to cycle through a list of users (office_id_list_%YY_MM_DD%\%%) that I have for each office (office_list.txt) and create a file when files newer that a date (%3) are found. The below code wasn't working so I decided to echo the errorlevel and found that it was always -1073741510 (working on a Windows 2003 machine). Ultimately, I'm trying to identify user home directories(organized by office) that have not been modified since a given date. Any thoughts would be greatly appreciated.


for /f "tokens=1 delims= " %%i in (U:\sysmon\u_cleanup\office_list.txt) do (
  if not exist u:\sysmon\u_cleanup\results\%%i mkdir u:\sysmon\u_cleanup\results\%%i
  for /f "tokens=1 delims= " %%j in (U:\sysmon\u_cleanup\results\office_lists_%YY_MM_DD%\%%i_dir_list_%YY_MM_DD%_final.txt) do (
    forfiles /P %1%%i\%%j /S /D +%3 /C "cmd /c if %errorlevel% == 0 echo ** Do not Archive - Found files modified after %3 > U:\sysmon\u_cleanup\results\%%i\%%j_%YY_MM_DD%.txt"
  )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
jim
  • 11
  • 3

2 Answers2

1

Add the /V:on option to cmd and use !errorlevel! instead of %errorlevel% which turns on delayed expansion.

Joey
  • 344,408
  • 85
  • 689
  • 683
0

Probably, the main problem is the part cmd /c if %errorlevel% == 0, it expands the errorlevel before any of your commands are executed.

Normally delayed expansion is the choice, but here it doesn't work(or as Joey mentioned, with /V:on), because it is in a new cmd context.

Here you could use it this way cmd /c if %%errorlevel%% == 0, so if the complete block is parsed the first time, the part is expanded to cmd /c if %errorlevel% == 0, and this is expanded a second time, when the cmd /c is executed.

And you could beautifying the code a bit

set "officePath=U:\sysmon\u_cleanup"
set "officeDatePath=%officePath%\results\office_lists_%YY_MM_DD%"
for /f "tokens=1 delims= " %%i in ("%officePath%\office_list.txt") do (
    if not exist "%officePath%\results\%%i" (
        mkdir "%officePath%\results\%%i"
    )
    for /f "tokens=1 delims= " %%j in ("%officeDatePath%\%%i_dir_list_%YY_MM_DD%_final.txt") do (
        forfiles /P %1%%i\%%j /S /D +%3 /C "cmd /c if %%errorlevel%% == 0 echo ** Do not Archive - Found files modified after %3 > %officePath%\results\%%i\%%j_%YY_MM_DD%.txt"
    )
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thanks. Also worked. Thanks for the code beautification tips. I do like to streamline if possible but it seems when I try, I introduce more issues. Do you have any good reference material on things like delayed expansion or how dos interprets variables (maybe load-time and run-time discussion ). Either way, thanks much. – jim Mar 23 '11 at 17:22
  • @jim: For beginners and also experts [Dostips.com](http://www.dostips.com), for understanding how batch works (experts) [how cmd parse one line delayed vs percent vs %%var](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133) – jeb Mar 26 '11 at 19:51