For Loop Help

Microsoft Windows xp professional w/serv...
December 15, 2009 at 06:45:57
Specs: Windows XP, Intel/2GB
Hello Everyone,

I'm stuck with a script which I'm hoping to get some help with.

The script is supposed to run on a schedule and it begins by checking if todays date is included in a txt file. If the date is in the file the script should terminate without excuting any code.

My code below:

for /f "tokens=1-4 delims=/ " %%d in ("%date%") do Set TodayDate=%%e-%%f-%%g

for /f %%D in (FILE.txt) do SET FileDate=%%D

IF %TodayDate%==%FileDate% (
) ELSE (

As you may have already noticed my problem is that on the second loop the only thing that remains in the vairiable is would be the last line in the file.txt.

I need the for loop to check every line, compare it to %TodayDate% variable and if any point it is == then it should go to EOF.

I'm thinking that some sort of nested for loop would be the answer but I have no clue how to do that.

Thanks in advance

See More: For Loop Help

Report •

December 15, 2009 at 07:38:58
You're not looping. Do this...

for /f %%D in (FILE.txt) do (
  Set FileDate=%%D
  IF %TodayDate%==%FileDate% (
  ) ELSE (

Now your if is inside the loop.

I can't test this because I need a sample of what is in your TXT file to test with.

P.S. What is it that you are trying to do? If it is a cleanup process then check this code out...

echo off
setLocal EnableDELAYedExpansion

if "%1"=="" (
  echo Must specify a tombstone [number of days].
  goto :EOF
Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%

Call :jdate %Year% %Month% %Day%
Set /a CompairDate=%jdate%
Set /a CompairDate=!CompairDate!-%1 
Set FileDate=1

for /f "tokens=*" %%a in ('dir %2 /s/b') do (
  set T=%%~Ta
  set FileDay=!T:~3,2!
  set FileMonth=!T:~0,2!
  set FileYear=!T:~6,4!
  call :jdate !FileYear! !FileMonth! !FileDay!
  set /a FileDate=!Jdate!
  echo %%a !FileMonth!-!FileDay!-!FileYear!

  if "!FileDate!" lss "!CompairDate!" (
    echo *Deleted* %%a !FileMonth!-!FileDay!-!FileYear!
    rem del %%a
Goto :EOF

:: Convert date to Julian

IF %MM:~0,1% EQU 0 SET MM=%MM:~1%
IF %DD:~0,1% EQU 0 SET DD=%DD:~1%

SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1  = %1 + 4800
SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
SET Month1=
SET Year1=

You don't have to mess with text files this way.

Report •

December 15, 2009 at 10:43:32
thanks for he reply, but it tell me that the syntax is incorrect.

My Txt file has dates if you copy/paste the below dates on your test file you should be able to test.


What i'm dong is that I need to run a process Monday through Friday execpt holidays and other specific dates that may change from time to time.

If he script finds any of the dates in my file it should then stop running.

My code now looks like this

for /f "tokens=1-4 delims=/ " %%d in ("%date%") do Set TodayDate=%%e-%%f-%%g

for /f %%D in (FILE.txt) do (
Set FileDate=%%D
IF %TodayDate%==%FileDate% (
GOTO end
) ELSE (


I put a pause so I can see what is going on.


Report •

December 15, 2009 at 11:07:45
Again I do not know what you are doing so it is hard to help but I made a file with the example content you gave me called file.txt and wrote this code. I simplified yours a little...

@echo off

Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%
Set TodayDate=%Month%-%Day%-%Year%

for /f %%D in (file.txt) do (
  IF %TodayDate%==%%D (
    echo %%D is the same as today.
    rem goto :EOF  
  ) ELSE (
    echo Will run code on %%D

I REMed out the "goto :EOF" which you forgot the ":" by the way; because it would just terminate the loop on the first match. Not sure why you would want this but you could just remove the "REM" to put it back in.

Report •

Related Solutions

December 15, 2009 at 11:25:10

That worked like a charm.


Report •

Ask Question