Page 1 of 1

Embedding IF inside FOR statement

Posted: 09 May 2016 09:00
by collector00
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.

Re: Embedding IF inside FOR statement

Posted: 09 May 2016 10:03
by Squashman
You missed using delayed expansion on your tFileCur variable on this line of code.

Code: Select all

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

Re: Embedding IF inside FOR statement

Posted: 09 May 2016 10:11
by Aacini
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

Re: Embedding IF inside FOR statement

Posted: 09 May 2016 12:11
by Compo
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
   )
)

Re: Embedding IF inside FOR statement

Posted: 09 May 2016 14:44
by collector00
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!