0

I am trying to compare the current timestamp with the changed timestamp of a file..

I would use something like that for the current timestamp:

set d=%date:~6,4%-%date:~3,2%-%date:~0,2%
set t=%time::=.%
set t=%t: =0%
set currentstamp="%d%  %t%"

I would use something like that for the modified timestamp:

set filetimestamp=dir /T:W  C:\test.txt

And then I would compare these variables:

if filetimestamp>currentstamp (...) 

EDIT: My current version looks like this. There is a mistake, but I can't find him.

set NEWDIRECTORY=D:\test
set PROJECTNAMES=(projekt1)

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set TIMESTAMP2=%%I
set TIMESTAMP2=%TIMESTAMP2:~0,8%-%TIMESTAMP2:~8,6%

rem do some things

for %%A in %PROJECTNAMES% do (
    for %%F in (C:\%%A.exe) do set file=%%~fF
    for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set TIMESTAMP1=%%I
    if %TIMESTAMP1% lss %TIMESTAMP2% (XCOPY "C:\%%A.exe" "%NEWDIRECTORY%\%%A.exe" /Y)
)
Henrik
  • 43
  • 6
  • 1
    [this](https://stackoverflow.com/a/18024049/2152082) might be helpful. And read the output of `if /?` for correct `if` syntax. – Stephan Apr 28 '20 at 11:25
  • You may need to `setlocal enabledelayedexpansion` for your new code i.e. `"!file:\=\\!"` and `!TIMESTAMP1!`. Use `!` instead of `%` on delayed variables so they do not expand at parse time. – michael_heath Apr 28 '20 at 15:46
  • @michael_heath thanks a lot! Now it works – Henrik Apr 28 '20 at 16:28
  • 1
    My first impression from what you appear to be doing is that [tag:robocopy] should be able to copy files from a source, to a destination, and only overwrite those which exist, have changed, and have a modification date which both differs, and has a particular modification date, or is within a particular range. Have you not tried it? Please open up a Command Prompt window, type `robocopy /?`, press the 'ENTER' key, and read the information presented. – Compo Apr 28 '20 at 16:40
  • You inserted a dash (`-`) into the first timestamp but not into the second. I suggest to use `"tokens=2 delims==."` to cut the milliseconds and so the troublesome `wmic` line ending (and not inserting any dashes of course - the strings are fine and ready to be compared) – Stephan Apr 28 '20 at 17:34
  • @Compo Robocopy sounds good, but i can't set a maxage in hours and minutes only in days.. – Henrik Apr 28 '20 at 17:58
  • Nothing whatsoever in your question title, or body text suggested that you were looking for anything specific with regards to the dirfference in times, as opposed to just dates, hence the reason I posted an example which uses just today as the comparison basis. Even when you included the new code by way of edit, your question still did not clarify what span difference you were looking for. Also are you saying that if the file is older, and modified today, but not within your range, you don't want to touch it. I'll also re-mention, are you expecting files which were modified in the future? – Compo Apr 28 '20 at 18:34

2 Answers2

0

I am not sure why there would be a file with a timestamp more recent than the current time of the system. That sounds like a problem.

To do this in a cmd.exe .bat file script, I would avoid the string processing and localization problems by using PowerShell.

powershell -NoLogo -NoProfile -Command ^
    "(Get-ChildItem -Path '.\xyx.txt').LastWriteTime -gt (Get-Date)"

In the .bat file script, this sets the X variable to True or False.

FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command ^
    "(Get-ChildItem -Path '.\xyx.txt').LastWriteTime -gt (Get-Date)"') DO (SET "X=%%A")
IF /I %X% == True (
    REM Do True work
)

Of course, if the script could just be written in PowerShell, it would be easier to write and easier to read.

if ((Get-ChildItem -Path '.\xyx.txt').LastWriteTime -gt (Get-Date)) {
    # Do True work
}
lit
  • 14,456
  • 10
  • 65
  • 119
0

You could use , (Windows Vista+).

To find out how the ForFiles command works, please open a Command Prompt window, type forfiles /?, press the ENTER key, and read the information presented. You may find that you could even copy your files using it too, instead of using your current methodology.

The following example uses the forfiles.exe command and prints a message to let you know if the file was modified today or otherwise.

To use it, save the entire content below as a complete :

@"%__AppDir__%forfiles.exe" /P "%~dp1." /M "%~nx1" /D +0 /C "Cmd /C If @IsDir==FALSE Exit /B 0">NUL 2>&1&(If ErrorLevel 1 (Echo File was not modified today.) Else Echo File has a modified date of today.)&Pause

Then drag and drop your file onto the saved script.

If you wanted to use it within your provided context, you could place the code within a labelled section of the script and Call it from the loop. It would then need only minor adjustment to suit your currently presented case.

Compo
  • 36,585
  • 5
  • 27
  • 39