raddevon
raddevon

Reputation: 3340

Windows batch scripting: compare two files' created dates

I would like to fork my Windows batch script based on a comparison of the created dates of two files, and I'm not sure where to begin. I feel like there must be a way. Any ideas?

UPDATE: Tried the solution in PA's answer. I copied the code snippet verbatim to the end of my current script. Then, I added this early in the script:

IF EXIST "%PROGRAMFILES(X86)%" CALL :getCreationDate "%PROGRAMFILES(X86)%\oracle\jinitiator 1.3.1.28\lib\security\certdb.txt"

which gives me an error when I execute: Invalid alias verb.

Upvotes: 1

Views: 7899

Answers (3)

Michael South
Michael South

Reputation: 31

You need to put a caret before the equals sign to escape it (cmd.exe is sooo wonderful). I've verified this works:

setlocal enableextensions enabledelayedexpansion
call :getCreationDate "C:\Windows\Notepad.exe"
echo Creation Date is: %creationdate%
endlocal
goto :EOF
:getCreationDate
set FILE=%~f1
set FILE=%FILE:\=\\%
for /F "skip=1 tokens=* usebackq" %%A in (`wmic datafile where name^="%FILE%" get creationdate`) do (
    set creationdate=%%A
)
goto :EOF

Upvotes: 3

ovadia
ovadia

Reputation: 1

try this:

wmic datafile where name='c:\\users\\ovadia\\test\\addx.txt'     get 'LAST MODIFIED' > dateofNEWadd.txt

wmic datafile where name='c:\\users\\ovadia\\test\\prevaddx.txt' get 'LAST MODIFIED' > dateofOLDadd.txt

fc /LB1 dateofNEWadd.txt dateofOLDadd.txt

if errorlevel 1 echo "fc err not 0" 

del dateof*

attributes for the 'get' may be...

Access Rights,
Caption,
Class Name,
Compressed,
Compression Method,
Computer System Class Name,
Computer System Name,
Creation Date,
Current File Open Count,
Description,
Drive,
Eight Dot Three File Name,
Encrypted,
Encryption Method,
File Extension,
File Name,
File System Class Name,
File System Name,
File Type,
Hidden,
Install Date,
Last Accessed,
Last Modified,
Manufacturer,
Name,
Path,
Readable,
Should Be Archived,
Size,
Status,
System File,
Version,
Writeable

Upvotes: 0

PA.
PA.

Reputation: 29339

In a bat you can get the creation date of a file with WMIC DATAFILE command, using the GET CREATIONDATE verb.

You need to capture the output of the command into a variable, see HELP FOR and HELP SET.

You can use :label and GOTO :eof to create a function that puts together this functionality.

Notice that for WMIC DATAFILE, the WHERE NAME= clause requires a fully specified filename. See HELP CALL and the %~f modifier.

Notice also that WMIC DATAFILE WHERE NAME= requires doubling the backslashes in the filename. See HELP SET and the % : = % syntax for replacing single backslashes with double backslashes.

something like this.....

:getCreationDate
set FILE=%~f1
set FILE=%FILE:\=\\%
FOR /F "skip=1 tokens=* usebackq" %%A IN (`wmic datafile where name="%FILE%" get creationdate`) DO (
  SET CREATIONDATE=%%A
)
goto :eof

You will need to use CALL :label for invoking it.

CALL :getCreationDate myfile.txt

You'll need to extract the part of the datetime you are interested in compating. See HELP SET using the ~ modifier.

Finally, you'll need to compare the returned datefiles. See HELP IF.

Upvotes: 2

Related Questions