For Loop Variable issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Nagac
Posts: 4
Joined: 27 Aug 2014 03:40

For Loop Variable issue

#1 Post by Nagac » 27 Aug 2014 04:24

Hi

I am facing an issue after executing the thirdparty command(which datastage "IStool") in For Loop which is not assigning the second value to variable.
Below is the script i am executing:

Code: Select all

@SET
SETLOCAL EnableDelayedExpansion
REM ############### Assigning the Directories to Variables######################
SET "DSHOME_PATH=E:\IBM\InformationServer\"
SET "HOME_PATH=E:\Temp\"
SET "DSBIN_PATH=%DSHOME_PATH%Server\DSEngine\bin\"
SET "DSISTOOL_PATH=%DSHOME_PATH%Clients\istools\cli\"
SET "BACKUP_PATH=%HOME_PATH%"
SET "SCRIPT_PATH=%HOME_PATH"
SET "TEMP_PATH=%HOME_PATH%"
SET "LOG_PATH= %HOME_PATH%"
REM ################ Creating Log File Name #######################################
SET DDATE=%date:~10%%date:~4,2%%date:~7,2%
SET DTIME=%time: =0%
SET DTIME=%DTIME:~0,2%%DTIME:~3,2%%DTIME:~6,2%
SET LOGFNAME=%LOG_PATH%%~n0_%DDATE%_%DTIME%.log
SET PROJECTLIST=%TEMP_PATH%PROJECTLIST.txt
ECHO. >%LOGFNAME%
copy nul %LOGFNAME%

REM ############### Exporting the Datastage Projects to Backup File ########################################
   FOR /F "tokens=1" %%I IN (%PROJECTLIST%) DO (
   SET PROJECTNAME=%%I
    ECHO %date:~4% %time% Exporting the Datastage Project: !PROJECTNAME! >> %LOGFNAME%
   SET DDATE=%date:~10%%date:~4,2%%date:~7,2%
   SET DTIME=%time: =0%
   SET DTIME=%DTIME:~0,2%%DTIME:~3,2%%DTIME:~6,2%
   SET EXPORTFILENAME=%BACKUP_PATH%!PROJECTNAME!_%DDATE%_%DTIME%.isx
   ECHO Export File Name !EXPORTFILENAME! >> %LOGFNAME%
   %DSISTOOL_PATH%istool export -dom USAWS09WVDBS002 -u ChalamN -p Tuesday38$ -datastage USAWS09WVDBS002/!PROJECTNAME!/*/*.* -archive !EXPORTFILENAME!
)



Log File says:

08/27/2014 10:24:51.41 Exporting the Datastage Project: dstage1
Export File Name E:\Temp\dstage1_20140827_102451.isx
08/27/2014 10:24:51.41 Exporting the Datastage Project: !PROJECTNAME!
Export File Name !EXPORTFILENAME!


When i comment the line

Code: Select all

%DSISTOOL_PATH%istool export -dom USAWS09WVDBS002 -u ChalamN -p Tuesday38$ -datastage USAWS09WVDBS002/!PROJECTNAME!/*/*.* -archive !EXPORTFILENAME!

it passes the values to variable as expected:
08/27/2014 10:09:51.75 Exporting the Datastage Project: dstage1
Export File Name E:\Temp\dstage1_20140827_100951.isx
08/27/2014 10:09:51.75 Exporting the Datastage Project: Test
Export File Name E:\Temp\Test_20140827_100951.isx




Thanks
Naga

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop Variable issue

#2 Post by foxidrive » 27 Aug 2014 04:29

Do your tests again - commenting out that line will not affect the lines above it.

Delayed expansion was not enabled in the code that generated the problem log, but the look of it.

Nagac
Posts: 4
Joined: 27 Aug 2014 03:40

Re: For Loop Variable issue

#3 Post by Nagac » 27 Aug 2014 04:37

Thanks for your reply foxidrive.

I put this "SETLOCAL EnableDelayedExpansion" at the start of the Script. Don't this enable the delayed expansion?

Do i need to change anything else in the script to enable Delayed expansion?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop Variable issue

#4 Post by foxidrive » 27 Aug 2014 06:02

Nagac wrote:I put this "SETLOCAL EnableDelayedExpansion" at the start of the Script. Don't this enable the delayed expansion?


Yes, though your log file shows it wasn't there during that test or delayed expansion was disabled in a later statement,
or the variable wasn't set.

Test it all again.

Nagac
Posts: 4
Joined: 27 Aug 2014 03:40

Re: For Loop Variable issue

#5 Post by Nagac » 27 Aug 2014 06:32

I tried again but there is no change

Code: Select all

@SET
SETLOCAL EnableDelayedExpansion
REM ############### Assigning the Directories to Variables######################
SET "DSHOME_PATH=E:\IBM\InformationServer\"
SET "HOME_PATH=E:\Temp\"
SET "DSBIN_PATH=%DSHOME_PATH%Server\DSEngine\bin\"
SET "DSISTOOL_PATH=%DSHOME_PATH%Clients\istools\cli\"
SET "BACKUP_PATH=%HOME_PATH%"
SET "SCRIPT_PATH=%HOME_PATH"
SET "TEMP_PATH=%HOME_PATH%"
SET "LOG_PATH= %HOME_PATH%"
REM ################ Creating Log File Name #######################################
SET DDATE=%date:~10%%date:~4,2%%date:~7,2%
SET DTIME=%time: =0%
SET DTIME=%DTIME:~0,2%%DTIME:~3,2%%DTIME:~6,2%
SET LOGFNAME=%LOG_PATH%%~n0_%DDATE%_%DTIME%.log
SET PROJECTLIST=%TEMP_PATH%PROJECTLIST.txt
ECHO. >%LOGFNAME%
copy nul %LOGFNAME%

REM ############### Exporting the Datastage Projects to Backup File ########################################
   FOR /F "tokens=1" %%I IN (%PROJECTLIST%) DO (
   SET PROJECTNAME=%%I
    ECHO %date:~4% %time% Exporting the Datastage Project: !PROJECTNAME! >> %LOGFNAME%
   SET DDATE=%date:~10%%date:~4,2%%date:~7,2%
   SET DTIME=%time: =0%
   SET DTIME=%DTIME:~0,2%%DTIME:~3,2%%DTIME:~6,2%
   SET EXPORTFILENAME=%BACKUP_PATH%!PROJECTNAME!_%DDATE%_%DTIME%.isx
   ECHO Export File Name !EXPORTFILENAME! >> %LOGFNAME%
   cd E:\IBM\InformationServer\Clients\istools\cli\
   %DSISTOOL_PATH%istool export -dom USAWS09WVDBS002 -u ChalamN -p Tuesday38$ -datastage USAWS09WVDBS002/!PROJECTNAME!/*/*.* -archive !EXPORTFILENAME!
)



08/27/2014 13:31:51.18 Exporting the Datastage Project: dstage1
Export File Name E:\Temp\dstage1_20140827_133151.isx
08/27/2014 13:31:51.18 Exporting the Datastage Project: !PROJECTNAME!
Export File Name !EXPORTFILENAME!

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: For Loop Variable issue

#6 Post by Squashman » 27 Aug 2014 06:44

If you coded this differently I see no need to even use delayed expansion in this script. In my opinion the only reason you are using it is for readability of the code. I never use delayed expansion unless I have to. And that is usually because I am manipulating the token variable from the FOR command inside the code block. You are not manipulating your environmental variables at all. You are just assigning them.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop Variable issue

#7 Post by foxidrive » 27 Aug 2014 06:45

Test this and see what shows in the log file (created in the current folder).
If any messages appear on the console before the pause prompt then report them too.

Code: Select all

@echo off
setlocal enabledelayedexpansion
SET "HOME_PATH=E:\Temp\"
SET "TEMP_PATH=%HOME_PATH%"
SET LOGFNAME=file.log
SET PROJECTLIST=%TEMP_PATH%PROJECTLIST.txt
break >%LOGFNAME%

   FOR /F "tokens=1" %%I IN (%PROJECTLIST%) DO (
   SET PROJECTNAME=%%I
   ECHO %date:~4% %time% Exporting the Datastage Project: !PROJECTNAME! >> %LOGFNAME%
   )

Pause

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: For Loop Variable issue

#8 Post by Squashman » 27 Aug 2014 06:56

You would need delayed expansion for all your time and date variables because you are inside a code block. But assigning the token to an environmental variable is not needed.

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: For Loop Variable issue

#9 Post by Aacini » 27 Aug 2014 07:35

Interesting! All your problems are derived by the fact that "istool" program is a Batch file itself: "istool.bat" that is called with NO "call" command. See this test:

This is prueba0.bat:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for %%a in (*.xml) do (
   set fileName=%%a
   echo Called with: !filename!
   prueba1 !fileName!
)

This is prueba1.bat:

Code: Select all

@echo off
rem

This is a copy of the screen:

Code: Select all

C:\> dir /b *.xml
theFile.xml
theFile2.xml

C:\> prueba0
Called with: theFile.xml

C:\> (
set fileName=theFile2.xml
 echo Called with: !filename!
 prueba1 !fileName!
)
Called with: !filename!

->Just include a "call" command in the line that executes "istool", that is:

Code: Select all

CALL  %DSISTOOL_PATH%istool export -dom USAWS09WVDBS002 -u ChalamN -p Tuesday38$ -datastage USAWS09WVDBS002/!PROJECTNAME!/*/*.* -archive !EXPORTFILENAME!


Antonio

Nagac
Posts: 4
Joined: 27 Aug 2014 03:40

Re: For Loop Variable issue

#10 Post by Nagac » 27 Aug 2014 09:11

Thanks Every one.

It worked

Code: Select all

CALL  %DSISTOOL_PATH%istool export -dom USAWS09WVDBS002 -u ChalamN -p Tuesday38$ -datastage USAWS09WVDBS002/!PROJECTNAME!/*/*.* -archive !EXPORTFILENAME!


Cheers

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop Variable issue

#11 Post by foxidrive » 27 Aug 2014 09:33

Aacini wrote:Interesting! All your problems are derived by the fact that "istool" program is a Batch file itself: "istool.bat" that is called with NO "call" command. See this test:


That is a curious behaviour.

The key piece of information that the OP didn't supply is that it works for the first time through the loop.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: For Loop Variable issue

#12 Post by Squashman » 27 Aug 2014 10:16

Yep. Problem would have been plain as day if we knew the OP was using another batch file within the batch file.

Post Reply