For loop runs in command prompt screen but not in cmd file??

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

For loop runs in command prompt screen but not in cmd file??

#1 Post by Bowlardo » 08 Aug 2013 08:17

When I open a command prompt window and run each of the lines it does exactly what I want it to do.
-Variable(mm) assigned to current time minutes
-Variable (ff)assigned to File time minutes
- Variable(cc) assigned to current time minutes minus File time Minutes
-If CC is greater than 10 then I have an error
However when I create a '.cmd' file and run it get the error "~ta was unexpected at this time."on the highlighted line below.

echo on
cd..
cd workdir\CUSTDCHECK
IF NOT EXIST P:\Gensrvnt\workdir\CUSTDCHECK\o_100_00000000* GOTO end
REM GET CURRENT TIME(MINUTES only!!)
set mm=%time:~3,-6%
REM get date time of files in folder &REM GET FILE TIME( IN MINUTES!!)
for %a in (*.*) do set FileDate=%~ta
set ff=%filedate:~14%
REM Calculate the difference between minutes of current time minus minutes of the file
set /a cc= %mm%-%ff%
REM if difference is more than 10 then we have an error.
if /i %cc% GTR 10 (echo Error) ELSE echo no error
:end
echo this is the end my friend!


I would greatly appreciate some help on this?

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

Re: For loop runs in command prompt screen but not in cmd fi

#2 Post by foxidrive » 08 Aug 2013 08:26

use for %%a in (*.*) do set FileDate=%%~ta

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

Re: For loop runs in command prompt screen but not in cmd fi

#3 Post by Bowlardo » 08 Aug 2013 09:41

foxidrive wrote:use for %%a in (*.*) do set FileDate=%%~ta


Foxi to the rescue (as always) :D
thanks again kind sir

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

Re: For loop runs in command prompt screen but not in cmd fi

#4 Post by Squashman » 08 Aug 2013 10:05

Reading is FUN!
H:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.
Variable names are case sensitive, so %i is different
from %I.

Bowlardo
Posts: 15
Joined: 28 Jun 2012 07:28

Re: For loop runs in command prompt screen but not in cmd fi

#5 Post by Bowlardo » 22 Aug 2013 08:32

for %%a in (*.*) do set FileDate=%%~ta

How do I just get the oldest file in the folder and only use that time.
(Squeezeman) - I have read for/? and nothing jumps out at me that can carry this out.

There is nothing in the FIND command that does it by oldest file either which was another thought

There is a DIR command that can output the oldest . DIR /O:D

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

Re: For loop runs in command prompt screen but not in cmd fi

#6 Post by foxidrive » 23 Aug 2013 06:32

This is meant to go through the list of files and remember the oldest one.

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /b /a-d /o:-d ') do set "olddate=%%~ta"

Post Reply