0

I am trying to pull folder names from a database, and use this script to loop into those specific folders in a directory and delete certain file names that are stored in another txt file. For some reason it runs the SQL command but it skips everything and reaches the pause. I am trying to familiarize myself with the commands and syntax so it may just be a matter of another pair of eyes on it to see something out of whack.

The sql command creates the PicRemoverTemp.txt file just fine and the deletethese.txt just holds a couple .txt files that the script needs to loop through the folder names pulled from the DB and delete the files if they exist.

Thanks for the help!

@ECHO OFF
REM Creates "deletethese.txt" via sql command
FOR /F "tokens=1 delims=," %%G IN (PicRemoverTEMP.txt) DO (
    SET "PathToCheck=P:\My Documents\Comm Trax Test\%%G"
    FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
            REM Verify file exists before attempting to delete.
            IF EXIST "%PathToCheck%\%%v" (
                ECHO Deleting File %%v
                DEL "%PathToCheck%\%%v"
            )
    )
)
PAUSE
2
  • where is the invocation of SQL command? is it a .BAT file? do you invoke it by CALL? Commented Jan 14, 2015 at 17:55
  • @PA. I use an sqlcmd in the .bat file, it was irrelevant to the question so I just left it out. Commented Jan 14, 2015 at 20:58

2 Answers 2

2

In batch files, each line or block of lines (lines in parenthesis) is first parsed and the executed. During the parse phase, all variable reads are replaced with the value in the variable before the line/block starts to execute. If a variable changes its value inside the line/block, this changed value is not accesible as the read operation was removed, replaced with the initial value.

In your case it means that in this code

FOR /F "tokens=1 delims=," %%G IN (PicRemoverTEMP.txt) DO (
    SET "PathToCheck=P:\My Documents\Comm Trax Test\%%G"
    FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
            IF EXIST "%PathToCheck%\%%v" (
....

the variable PathToCheck is changed inside a block, but as all the read operations (value retrieval) were removed, the IF EXIST "%PathToCheck%... will not found the indicated file as the value that is being used is not the correct one.

To solve, you can use delayed expansion (setlocal enabledelayedexpansion) that will allow you to change, where needed, the syntax to read a variable from %var% to !var!, indicating to the parser that the read operation must be delayed until the moment the command is executed.

setlocal enabledelayedexpansion

FOR /F "tokens=1 delims=," %%G IN (PicRemoverTEMP.txt) DO (
    SET "PathToCheck=P:\My Documents\Comm Trax Test\%%G"
    FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
            IF EXIST "!PathToCheck!\%%v" (
                ECHO Deleting File %%v
                DEL "!PathToCheck!\%%v"
            )
    )
)

Or, you can change your code to

FOR /F "tokens=1 delims=," %%G IN (PicRemoverTEMP.txt) DO (
    FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
            IF EXIST "P:\My Documents\Comm Trax Test\%%G\%%v" (
                ECHO Deleting File %%v
                DEL "P:\My Documents\Comm Trax Test\%%G\%%v"
            )
    )
)

Or

set "PathToCheck=P:\My Documents\Comm Trax Test"

FOR /F "tokens=1 delims=," %%G IN (PicRemoverTEMP.txt) DO (
    FOR /F "tokens=1 delims=," %%v IN (deletethese.txt) DO (
            IF EXIST "%PathToChange%\%%G\%%v" (
                ECHO Deleting File %%v
                DEL "%PathToChange%\%%G\%%v"
            )
    )
)

In both cases, as no variable is changed and readed inside the block, there is no need for delayed expansion

Sign up to request clarification or add additional context in comments.

Comments

1

try

@ECHO OFF
REM Creates "deletethese.txt" via sql command
FOR /F "delims=," %%G IN (PicRemoverTEMP.txt) DO (
    FOR /F "delims=," %%v IN (deletethese.txt) DO (
            REM Verify file exists before attempting to delete.
            IF EXIST "P:\My Documents\Comm Trax Test\%%~G\%%~v" (
                ECHO Deleting File %%v
                DEL "P:\My Documents\Comm Trax Test\%%~G\%%~v"
            )
    )
)
PAUSE

You cannot use a variable inside a code block without delayed expansion. http://ss64.com/nt/delayedexpansion.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.