Embedding IF inside FOR statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
collector00
Posts: 2
Joined: 09 May 2016 08:54

Embedding IF inside FOR statement

#1 Post by collector00 » 09 May 2016 09:00

Hi, I have the following script that I'm trying to run (this is just a subsection of it where the issue exists). Basically, I receive 4 files -- one for 2016, 2015, 2014 and 2013. The script, in this instances, simply moves that file (for that year) to a folder that corresponds to the date. Rather than coding each one I wanted to loop through them. The script worked perfectly before adding the "FOR" statement so it has to be how I'm using it. Any assist would be greatly appreciated!!

Code: Select all

FOR /L %%A IN (%firstYear%,1,%lastYear%) DO (
   SET tFileCur=my_filename%%A.xls
   IF EXIST "%foldernm%\!tFileCur!" (
      ECHO.   Processing: !tFileCur!... found.
      SET /a repCount=repCount+1
      : move to todays folder
      MOVE /Y "%foldernm%\%tFileCur%" "%foldernm%\%fileDate%\"
      :
   ) else (
      ECHO.   Processing: !tFileCur!... Not found.
      SET /a missCount=missCount+1
   )
)


When I reach the IF statement I receive an error which I cannot catch but believe it is referring to the FOR not being closed.

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

Re: Embedding IF inside FOR statement

#2 Post by Squashman » 09 May 2016 10:03

You missed using delayed expansion on your tFileCur variable on this line of code.

Code: Select all

MOVE /Y "%foldernm%\%tFileCur%" "%foldernm%\%fileDate%\"

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

Re: Embedding IF inside FOR statement

#3 Post by Aacini » 09 May 2016 10:11

Do not use labels like if they were comments!

Code: Select all

FOR /L %%A IN (%firstYear%,1,%lastYear%) DO (
   SET tFileCur=my_filename%%A.xls
   IF EXIST "%foldernm%\!tFileCur!" (
      ECHO.   Processing: !tFileCur!... found.
      SET /a repCount=repCount+1
      REM move to todays folder
      MOVE /Y "%foldernm%\!tFileCur!" "%foldernm%\%fileDate%\"
      REM
   ) else (
      ECHO.   Processing: !tFileCur!... Not found.
      SET /a missCount=missCount+1
   )
)

Antonio

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Embedding IF inside FOR statement

#4 Post by Compo » 09 May 2016 12:11

I see no need for delayedexpansion at all:

Code: Select all

@Echo Off
SetLocal EnableExtensions

(Set firstYear=2013)
(Set lastYear=2016)
(Set foldernm=X:\My_Folder
(Set tFileCur=%foldernm%\My_FileName)

Set "repCount=0"
Set "missCount=0"
For /L %%A In (%firstYear%,1,%lastYear%) Do (
   If Exist "%tFileCur%%%A.xls" (
      Echo=   Processing: %tFileCur%%%A.xls... found.
      If Not Exist "%foldernm%\%%A\" MD "%foldernm%\%%A"
      Move "%tFileCur%%%A.xls" "%foldernm%\%%A"
      Set/A repCount+=1
   ) Else (
      Echo=   Processing: %tFileCur%%%A.xls... Not found.
      Set/A missCount+=1
   )
)

collector00
Posts: 2
Joined: 09 May 2016 08:54

Re: Embedding IF inside FOR statement

#5 Post by collector00 » 09 May 2016 14:44

Aacini wrote:Do not use labels like if they were comments!

Code: Select all

FOR /L %%A IN (%firstYear%,1,%lastYear%) DO (
   SET tFileCur=my_filename%%A.xls
   IF EXIST "%foldernm%\!tFileCur!" (
      ECHO.   Processing: !tFileCur!... found.
      SET /a repCount=repCount+1
      REM move to todays folder
      MOVE /Y "%foldernm%\!tFileCur!" "%foldernm%\%fileDate%\"
      REM
   ) else (
      ECHO.   Processing: !tFileCur!... Not found.
      SET /a missCount=missCount+1
   )
)

Antonio


Antonio - thanks for going through this. It works perfectly. Initially I had an issue but it was a stupid error on my part but once I fixed that the rest works flawless. Thank you so much for your time!

Post Reply