locked
Batch File For/If Loop problem

    Question

  • Alright, so I like to think I'm good with batch, but inevitably I miss something really silly and end up scratching my head for days. I've asked a lot of people that I work with, and no one seems to have an answer aside from "Well, don't use batch use (insert scripting language that I don't know here)". While I'm working on learning other languages, I'm still a big fan of batch, and it's what I've used for years. Anyway, to my problem...

    There is a specific application installed throughout our domain on random computers which needs to go away. The easiest way I can see to do this is to have a batch file called from the startup scripts that checks the machine for the files, and if it finds them, it kills them. Here's what I've got...

    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCS.exe" > %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DNTUS26.exe" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCS.INI" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCK.DLL" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCSET.DLL" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCSHELL.DLL" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCST.EXE" >> %SYSTEMDRIVE%\dwtoremove.txt

    :Loop

    For /f %%i IN (%SYSTEMDRIVE%\dwtoremove.txt) DO (
    Set DWRPATH="%%i"&& If /i %DWRPATH:~-14,13% EQU DWRCSHELL.DLL (
    Del /f /q %DWRPATH%
    ) Else (
    If /i %DWRPATH:~-11,10% EQU DWRCST.EXE (
    Del /f /q %DWRPATH%
    )
    )

    Now then, here's the various problems that I've run into.

    If I put the IF loop outside of the For loop so that %%i is updated each time for use in the DWRPATH variable. When I do that, %%i never truly gets updated, even if I try to use an increment variable counter to know how many times I've been through the loops, because it thinks each execution of the For loop is the first execution (which logically it is).

    I can't pass a wildcard to the If statement to try and match the file path, hence the need for the variable DWRPATH, and parsing only the last x characters of the string given by the data file for the compare statement.

    With the If statement embedded WITHIN the For Loop, %%i never actually gets updated. Here's the kicker. If DWRPATH has a value BEFORE the loop begins (i.e. after i've run the batch file, but didn't clean up the variables, or just set the variable beforehand for testing) then the IF statements work fine (in that the variable is present, and gets compared). If DWRPATH does **NOT** have a value prior to the batch file being run, the If statement doesn't work at all. It doesn't like me trying to parse a variable that doesn't exist (13 was unexpected at this time.)

    If I embed the If loop outside of the For loop sure the variable gets updated, but by the time the If Loop gets to it, it's the final line of the file and no good. Or, I goto the If loop from within the for loop (i.e. Goto Loop2), and the file just loops infinitely because the For loop only ever grabs the first line of the text file.

    Also, I tried it with SetLocal EnableDelayedExtensions but the If statements choke on the ! in place of %.

    I think I'm out of tricks at this point. Hoping someone more skilled in the ways of the almighty batch file might have some insight.

    Thanks,

    Misty

    • Edited by PixieDust Tuesday, May 05, 2009 5:28 PM
    • Moved by Max Wang_Chinasoft Wednesday, April 27, 2011 7:53 PM forum consolidation (From:General Discussion)
    Tuesday, May 05, 2009 5:26 PM

All replies

  • you'll have to put the command SETLOCAL ENABLEDELAYEDEXPANSION  as one of the first in your batch, this way the vbariables will get updated inside a loop
    Tuesday, September 22, 2009 8:56 AM
  • to get around this just use a subroutine -
    just like doing a call to another program but the code is within the main script

    ie -

    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCS.exe" > %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DNTUS26.exe" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCS.INI" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCK.DLL" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCSET.DLL" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCSHELL.DLL" >> %SYSTEMDRIVE%\dwtoremove.txt
    Dir /s /b %SYSTEMDRIVE%\ | Find /i "DWRCST.EXE" >> %SYSTEMDRIVE%\dwtoremove.txt

    :Loop

    For /f %%i IN (%SYSTEMDRIVE%\dwtoremove.txt) DO call :sub %%i

    REM assuming you have no other code to run
    goto :EOF

    :sub
    Set DWRPATH="%%i"
    If /i %DWRPATH:~-14,13% EQU DWRCSHELL.DLL (
    Del /f /q %DWRPATH%
    ) Else (
    If /i %DWRPATH:~-11,10% EQU DWRCST.EXE (
    Del /f /q %DWRPATH%
    )
    )
    REM this forces the program back to where the call came from so the for loop continues
    goto :EOF

    hope this helps

    Monday, November 16, 2009 9:49 AM