For loop to set env var

April 27, 2011 at 18:52:52
Specs: Windows XP HE SP.3+
Win XP Home.

I'm trying to extract file creation and last written dates using For loops.

Dircmd is set to /og/on/p

Dir /tc produces

 
Mon 18/04/2011  10:01               760 trial.bat
               1 File(s)            760 bytes
               0 Dir(s)  18,096,984,064 bytes free

Dir /tw produces

Thu 28/04/2011  13:17               760 trial.bat
               1 File(s)            760 bytes
               0 Dir(s)  18,093,801,472 bytes free

Both of which are correctly displayed.

However the following script produces only the last written date i.e.


Creation date/time = Thu 28/04/2011 13:17

Written  date/time = Thu 28/04/2011 13:17

What is wrong with the script please?

@echo off
setlocal 
cls                            &::myref filedates670

:: Displays target file creation and last written date/time.
:: Syntax batfilename targetfilename.ext
:: (spaces are allowed in the targetfilename without double quotes).

if [%*] equ [] echo Target [path]\filename not entered    - ^
   job terminated&&exit/b

set Target=%*

if not exist "%Target%" echo Target file "%Target%" not found    - ^
   job terminated&&exit /b

for /f "tokens=*" %%1 in ('dir /tc /b "%target%"') do (
    set creation=%%~t1
    )

for /f "tokens=*" %%A in ('dir /tw /b "%target%"') do (
    set written=%%~tA
    )

echo Target file = %target%
echo.
echo Creation date/time = %creation%
echo.
echo Written  date/time = %written%


See More: For loop to set env var

Report •


#1
April 27, 2011 at 20:08:28
The problem is that you are using "/b"(file name only) then asking "for" to tell you the the file time(with "~t"), for will only give you last modified.

This is based on dir's output on xp home:

for /f "eol=  delims=" %%A in ('dir /tc "%target%"') do (
    set creation=%%A
    )

for /f "eol=  delims=" %%A in ('dir /tw "%target%"') do (
    set written=%%A
    )

for /f "delims=>" %%a in ("%creation:    =>%") do set creation=%%a
for /f "delims=>" %%a in ("%written:    =>%") do set written=%%a


Report •

#2
April 27, 2011 at 20:43:27
Excellent Judago - thank you VM for the quick response. I didn't find, or overlooked, that For would only return the default Dir setting.

Thanks again.


Report •

#3
April 27, 2011 at 23:22:41
I didn't find, or overlooked, that For would only return the default Dir setting.

I'm not sure that it follows the Dir setting at all, it may be that for has it's own unchangeable default. At least I couldn't find a way to change it.

Which way it goes doesn't seem to be documented:

From "for /?"
    %~tI        - expands %I to date/time of file

But either way it's not surprising that both results were the same because the "/b" switch just gives back a file name, so you passed the same file name to the same For variable modifier and got the same result.

All of batch For's operators a plain text based, Dir isn't passing anything but text(the text that would normally be seen) to For and For is just doing it's own thing with the text when modifiers are used.


Report •

Related Solutions

#4
April 28, 2011 at 01:20:38
Thank you for the bit of an explain, just another bit of M$ magic.

T.C.


Report •


Ask Question