sandy
sandy

Reputation: 75

"ECHO is on" is setting in variable

I have a batch file which takes three parameters [log:path], [logok:path], [logerr:path]. The values of the respective parameter is log:c:\logs\install.log, logok:c:\logs\installok.log, logerr:c:\logs\installerr.log.

I need to process these 3 parameters and set it on respective variable log=c:\logs\install.log, logok=c:\logs\installok.log, logerr=c:\logs\installerr.log so that I can use them in next step to create a file in those paths.

I have written below script but somehow each variable is printing "ECHO is on". It should actually print the location path. Any idea how to achieve this?

    REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
    REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'

    REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS

    SETLOCAL ENABLEDELAYEDEXPANSION
    FOR %%A IN (%*) DO (
    ECHO %%A >> C:\LOGS\TEST1.TXT
        FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
          SET %%G=%%H:%%I
        )
    )
    ENDLOCAL

    ECHO %LOG% >> C:\LOGS\TEST2.TXT
    ECHO %LOGOK% >> C:\LOGS\TEST3.TXT
    ECHO %LOGERR% >> C:\LOGS\TEST4.TXT

    START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN

    SET EXIT_CODE=%ERRORLEVEL%


    REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
       IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> %LOGOK%  
    REM If it fails then it should a create installerr.log file 'c:\logs')
       IF NOT EXIST %LOGOK% ECHO FAILED >> %LOGERR%

Output of TEST1.TXT:

log:c:\logs\install.log 
logok:c:\logs\installok.log
logerr:c:\logs\installerr.log 

Output of TEST1.TXT,TEST2.TXT,TEST3.TXT:

 ECHO is on.

Upvotes: 1

Views: 605

Answers (1)

Dvd848
Dvd848

Reputation: 288

Try this:

REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'

REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS

SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> c:\Logs\TEST1.TXT
    FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
      SET %%G=%%H:%%I
    )
)

ECHO !LOG! >> c:\LOGS\TEST2.TXT
ECHO !LOGOK! >> c:\LOGS\TEST3.TXT
ECHO !LOGERR! >> c:\LOGS\TEST4.TXT

START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN

SET EXIT_CODE=%ERRORLEVEL%


REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
   IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> !LOGOK!
REM If it fails then it should a create installerr.log file 'c:\logs')
   IF NOT EXIST !LOGOK! ECHO FAILED >> !LOGERR!
ENDLOCAL

Basically this:

  • Extends the local scope so that the variables which were assigned inside the FOR loop will be available in the rest of the script
  • Moves variables to the !delayed! variable syntax in order to cause them to be evaluated as late as possible. See the documentation for "EnableDelayedExpansion" for more details

Note that "ECHO is on." is the output that you get when calling echo with no parameters:

C:\>type echo_test.bat
echo 
echo %no_such_variable%
C:\>echo_test.bat

C:\>echo
ECHO is on.

C:\>echo
ECHO is on.

Upvotes: 2

Related Questions