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 Server 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?
Change %
to %%
for use in batch file, for %~ta
syntax enter call /?
for %a in (MyFile.txt) do set FileDate=%~ta
You can do it
forfiles /M myfile.txt /C "cmd /c echo @fdate @ftime"
Useful reference to get file properties using a batch file, included is the last modified time:
FOR %%? IN ("C:\somefile\path\file.txt") DO (
ECHO File Name Only : %%~n?
ECHO File Extension : %%~x?
ECHO Name in 8.3 notation : %%~sn?
ECHO File Attributes : %%~a?
ECHO Located on Drive : %%~d?
ECHO File Size : %%~z?
ECHO Last-Modified Date : %%~t?
ECHO Parent Folder : %%~dp?
ECHO Fully Qualified Path : %%~f?
ECHO FQP in 8.3 notation : %%~sf?
ECHO Location in the PATH : %%~dp$PATH:?
)
It works for me on Vista. Some things to try:
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
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.
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.
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
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
)