I have a batch file that takes two, renames the first file with one name and the second file another name. A variable is set to Y and then delayed expansion is used on the variable in an if statement. The code reads in the two files form the SOURCE location and places the SOURCE location and the file names in a txt file. So the file will look like this: \servername\Reports\531627.REPORT.CSV \servername\Reports\531638.REPORT.CSV The code then loops through the text file, copies and then renames the first file to REPORTLST and then the second on to REPORTAST. What's happening thought is it's like the if statement is being ignored. I've printed out the FIRSTFILE variable to confirm that it is Y and it is but the first file is never copied and renamed. Here is the code:
SetLocal EnableDelayedExpansion
@ECHO ON
if %1.==. (GOTO PARMERR) ELSE (SET ORIGFILENAME=%1)
if %2.==. (GOTO PARMERR) ELSE (SET SOURCE=%2)
if %3.==. (GOTO PARMERR) ELSE (SET DESTINATION=%3)
if %4.==. (GOTO PARMERR) ELSE (SET SMALLFILENAME=%4)
if %5.==. (GOTO PARMERR) ELSE (SET LARGEFILENAME=%5)
SET FIRSTFILE=Y
IF EXIST %SOURCE%dirlist.txt DEL %SOURCE%dirlist.txt
DIR %SOURCE%*%ORIGFILENAME% /B /S >> %SOURCE%DIRLIST.TXT
FOR /F "eol=; tokens=1* delims=," %%m in (%SOURCE%DIRLIST.TXT) DO (
ECHO !FIRSTFILE!
IF !FIRSTFILE! == "Y" (
COPY %%m %DESTINATION%%SMALLFILENAME%
ECHO "IN IF"
) else (
COPY %%m %DESTINATION%%LARGEFILENAME%
ECHO "IN ELSE"
)
ECHO !FIRSTFILE!
set FIRSTFILE=N
ECHO !FIRSTFILE!
)
del dirlist.txt
:RENAMEERROR
ECHO ****RENAME FAILED******
set err=4
GOTO END
:INVALIDSOURCE
ECHO ****cOULD NOT CD OR INVALID SOURCE******
set err=3
GOTO END
:INVALIDDESTINATION
ECHO ****cOULD NOT COPY OR INVALID DEST******
set err=2
GOTO END
:PARMERR
ECHO ****MISSING OR INVALID PARAMETER******
set err=1
GOTO END
set err=0
GOTO END
:END
EndLocal
exit /B %err%
And here is what is printed out:
C:\Windows\system32>(
ECHO !FIRSTFILE!
IF !FIRSTFILE! == Y (
COPY \\servername\Reports\531627.REPORT.CSV \\servername\c$\Gateway\REPORTLST.csv
ECHO "IN IF"
) else (
COPY \\servername\Reports\531627.REPORT.CSV \\servername\c$\Gateway\REPORTAST.csv
ECHO "IN ELSE"
)
ECHO !FIRSTFILE!
set FIRSTFILE=N
ECHO !FIRSTFILE!
)
Y
1 file(s) copied.
"IN ELSE"
Y
N
C:\Windows\system32>(
ECHO !FIRSTFILE!
IF !FIRSTFILE! == "Y" (
COPY \\servername\Reports\531638.ATMRECON.CSV \\servername\c$\Gateway\REPORTLST.csv
ECHO "IN IF"
) else (
COPY \\servername\Reports\531638.ATMRECON.CSV \\servername\c$\Gateway\REPORTAST.csv
ECHO "IN ELSE"
)
ECHO !FIRSTFILE!
set FIRSTFILE=N
ECHO !FIRSTFILE!
)
N
1 file(s) copied.
"IN ELSE"
N
N
So you can see that it's always going into the ELSE but I can't see what the issue is. Any ideas?