meshell
meshell

Reputation: 1

BAT Using for /f on a text file not working?

I am trying to use the for /F to feed in 3 variables and use them to move files into the folders they need to be in for organization.

The script is doing exactly what I want it to do... EXCEPT it is only doing it for the very last line in the .txt file. I thought the /F would automatically do the command for each line in the text file but something is wrong and it is not.

Can anyone tell me how to fix the below code so it will work for every line in the text instead of just the last line?

Thank you!

SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2,3 delims=," %%G in (MoveFiles.txt) do (
@set "Account_ID=%%G" 
@set "PACKAGE_ID=%%H" 
@set "FILE_NAME=%%I")

mkdir -p "O:\Documents and Media\%PACKAGE_ID%\%ACCOUNT_ID%"

SET FOO="O:\MediaAttachments\%FILE_NAME%"
COPY %FOO% "O:\Documents and Media\%PACKAGE_ID%\%ACCOUNT_ID%\%FILE_NAME%"

Upvotes: 0

Views: 702

Answers (1)

evan
evan

Reputation: 12535

Everything that you want done on every line must be done IN the for loop.

What's happening is that you're updating your variables for each line. So, they are on the last set items when they come out of the for loop. Then you do your mkdir, set, & copy which is only for that last item.

Moving the end parenthesis to the bottom should fix only doing things once.

Then change % to ! to use the EnableDelayedExpansion that you've already set up.

SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2,3 delims=," %%G in (MoveFiles.txt) do (
    @set "Account_ID=%%G" 
    @set "PACKAGE_ID=%%H" 
    @set "FILE_NAME=%%I"

    mkdir -p "O:\Documents and Media\!PACKAGE_ID!\!ACCOUNT_ID!"

    SET FOO="O:\MediaAttachments\!FILE_NAME!"
    COPY !FOO! "O:\Documents and Media\!PACKAGE_ID!\!ACCOUNT_ID!\!FILE_NAME!"
)

Upvotes: 2

Related Questions