You are not logged in.

#1 27 Aug 2007 11:29

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 121
Website

get file date from for /f loop

Just a simple one this time I think

I am trying to display a date for a file name using the %~t1 parameter. However it does not create the value. So how can I get the file date of the file %%G in the for loop?

@echo off
setlocal enabledelayedexpansion

set ghostdir=D:\Image\Ghost
set txtfile=D:\Project\WinScan\ghostimages.txt

REM Scans the folder for *.gho files and creates text file
if exist "%txtfile%" del "%txtfile%"
echo ---=== Ghost Files ===--->>"%txtfile%"
set count=0
for /f "tokens=*" %%G in ('dir /b /o-d "%ghostdir%\*.gho"') do (
    set /a count+=1
    set file=%%~G
    set fdate=%%~tG
    echo !count! - !file! - updated !fdate!>>"%txtfile%"
    )

cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 28 Aug 2007 21:43

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: get file date from for /f loop

Sometimes for no logical reason you need to supply the full path..

@echo off
setlocal enabledelayedexpansion

set ghostdir=c:\dell
set txtfile=c:\dell\ghostimages.txt

REM Scans the folder for *.gho files and creates text file
if exist "%txtfile%" del "%txtfile%"
echo ---=== Ghost Files ===--->>"%txtfile%"
set count=0
for /f "tokens=*" %%G in ('dir /b /o-d "%ghostdir%\*.exe"') do (call :sub1 "%ghostdir%\%%G")

goto :eof

:sub1
    set /a count+=1
    set file=%~1
    set fdate=%~t1
    echo !count! - !file! - updated !fdate!>>"%txtfile%"
goto :eof

Offline

#3 03 Sep 2007 09:33

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: get file date from for /f loop

Oh but there is a logical reason, with a couple of contributing factors.

The first is down to the way "dir /b" (when you don't use the /s switch) will always, always, always show only the filename, even if you've fully pathed that which you are dir-ing.

The other factor is your working directory.  If you aren't running the script from the directory where the files are, you haven't specified the working directory by using a shortcut to run it and haven't CD-ed to it in the script the path-less output from the dir /b means that the FOR command will be looking in your working directory for the files.  If they don't happen to exist there, it'll spit it's dummy when you try to find out information about them.

So with the above in mind, you could fix the problem with Simon's method, store the script in the directory you want to work with (often a less than ideal option,) specify the correct working directory in a shortcut you use to run the script or simply do this:
* UNTESTED *

@echo off
setlocal enabledelayedexpansion

set ghostdir=D:\Image\Ghost
set txtfile=D:\Project\WinScan\ghostimages.txt

REM Scans the folder for *.gho files and creates text file
if exist "%txtfile%" del "%txtfile%"
echo ---=== Ghost Files ===--->>"%txtfile%"
set count=0
cd /d "%ghostdir%"
for /f "tokens=*" %%G in ('dir /b /o-d "*.gho"') do (
    set /a count+=1
    set file=%%~G
    set fdate=%%~tG
    echo !count! - !file! - updated !fdate!>>"%txtfile%"
    )

cmd | *sh | ruby | chef

Offline

Board footer

Powered by