!fileName! (String variable) was unexpected at this time in batch?
-
18-06-2021 - |
Domanda
I'm running a batch file that:
1. Go thru ALL *.properties files in C:\ExecutionSDKTest_10.2.2
2. Run them
3. compare line by line of the outputted log with ref log
But the cmd.exe keeps throwing me this error: "!fileName! was unexpected at this time. Any ideas?
CALL ant clean build compile
REM Run ENG-zzz.properties --> output to ENG-zzz.properties.log in Logs dir
REM loop thru/Run ALL .properties file names from "ExecutionSDKTest_10.2.2" folder
FOR %%G in (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
set flag=true
Set fileName=%%~nxG
set testNum=!fileName:~0,7!
echo Running:!fileName!
java -jar Test.jar !fileName! > Logs\!fileName!.log
ECHO Create: Ref!fileName!.log at C:\ExecutionSDKTest_10.2.2\Logs
set logPath=C:\ExecutionSDKTest_10.2.2\Logs\!fileName!.log
set refLogPath=C:\ExecutionSDKTest_10.2.2\Logs\!testNum!-Ref.properties.txt
set lnCorrect=true
set errorLnCount=0
REM if ...Ref.txt doesnt Exist --> check certain lines of the log
REM assume ref.txt exists
<!logPath! (
For /F "tokens=*" %%R in (!refLogPath!) DO (
set logLine=
set /p logLine=
set refLogLine=%%R
REM Check line by line of log against refLog
REM assume ALL times have been replaced with: "xx:xx:xx"
REM if corresponding lines mismatch
if NOT !logLine!==!refLogLine! Do (
set lnCorrect=false
REM output to command line: can be put into .log/.txt later
if errorLnCount==1 AND lnCorrect==false DO (
ECHO The following line(s) !fileName! are incorrect against corresponding line(s) in !testNum!-Ref.properties.txt
)
ECHO !logLine!
)
)
)
if lnCorrect=true Do Echo !fileName! Passed
)
Pause
Soluzione
Your syntax is a bit odd (it's simply wrong)
if lnCorrect=true Do Echo !fileName! Passed
- The
IF
statement must not have aDO
clause. - compare needs two equal signs.
- There are not logic operators like
AND
,OR
- The variables must be expanded to be able to compare them
So your line should look like
if "!lnCorrect!"=="true" Echo !fileName! Passed
And the other bug is in the line
if errorLnCount==1 AND lnCorrect==false DO (
This could be refactored to
if !errorLnCount!==1 if !lnCorrect!==false (
There is also a problem with the line
ECHO The following line(s) !fileName! are incorrect against
The closing parenthesis will cause your problem, as it closes the if-clause and loop.
Remove them or escape them
ECHO The following line(s^) !fileName! are ... corresponding line(s^) in ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow