views:

640

answers:

4

I have been using the following command to get the file date, however the fileDate variable has been returning blank value ever since we moved to a different server (windows 2003)

FOR /f %%a in ('dir myfile.txt^|find /i " myfile.txt"')  DO SET fileDate=%%a 

Is there any other more reliable way to get the file date?

+1  A: 

It works for me on Vista. Some things to try:

  1. Replace find with the fully-qualified path of the find command. find is a common tool name. There's a unix find that is very differet from the Windows built-in find. like this:
    FOR /f %%a in ('dir ^|%windir%\system32\find.exe /i "myfile.txt"') DO SET fileDate=%%a

  2. examine the output of the command in a cmd.exe window. To do that, You need to replace the %% with %.
    FOR /f %a in ('dir ^|c:\windows\system32\find.exe /i "myfile.txt"') DO SET fileDate=%a
    That may give you some ideas.

  3. If that shows up as blank, then again, at a command prompt, try this:

    dir | c:\windows\system32\find.exe /i "myfile.txt"

This should show you what you need to see.

If you still can't figure it out from that, edit your post to include what you see from these commands and someone will help you.

Cheeso
my statetement and (the step 1 and 2) work on my computer and any other win server 2003 that I can get my hand on. could this be an issue where it run under service id and trigger by a scheduler?
Ricky Supit
yes it could be. The find program may be different for a different service ID, because it has a different path. Best to run these commands under the service ID to capture and examine the output.
Cheeso
Some years ago we had some similar issues regarding the batch processing of the output of the DIR command, because of different language of the command processor (in our case, our batch file worked in win2000 english and spanish, but failed in german).
PA
Also can happen with UK versus US english!
Cheeso
A: 

What output (exactly) does dir myfile.txt give in the current directory? What happens if you set the delimiters?

FOR /f "tokens=1,2* delims= " %%a in ('dir myfile.txt^|find /i " myfile.txt"') DO SET fileDate=%%a 

(note the space after delims=)
(to make life easier, you can do this from the command line by replacing %%a with %a)

BlueRaja - Danny Pflughoeft
A: 

you can get a files modified date using vbscript too

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile= objArgs(0)
WScript.Echo objFS.GetFile(strFile).DateLastModified

save the above as mygetdate.vbs and on command line

c:\test> cscript //nologo mygetdate.vbs myfile
ghostdog74
+2  A: 

Change % to %% for use in batch file, for %~ta syntax enter call /?

for %a in (MyFile.txt) do set FileDate=%~ta
Andy Morris
+1. That's actually the correct one. Parsing the output of `dir` is insanity at best. Side note: In a batch file, double the `%` signs.
Joey
Thanks, one more question: how get just the date portion. Is the following syntax reliable enough or is there a better way. set FileDate=%FileDate:~0,10%
Ricky Supit
It may not work if you change your international settings, but otherwise it should be OK
Andy Morris