5

I have the following code that is supposed to return the last modified date of a file as a string:

:getLastModifiedDate
@echo on
SETLOCAL enabledelayedexpansion
set FILE=%~f1
set FILE=!FILE:%NETWORK_DRIVE_SHARE_PATH%=%NETWORK_DRIVE_NAME%!
set FILE=%FILE:\=\\%
set RETURN_VALUE="internal script error"

for /f "tokens=* usebackq" %%d in (`wmic datafile where Name^="%FILE%" get lastmodified ^| findstr ^"[0-9]^"`) do ( set tmpd="%%d ddd"
    echo 111111 %tmpd%
    echo 222222 !tmpd!
    echo 333333 %%tmpd%%
    set RETURN_VALUE=%tmpd:~0,14%
)
(ENDLOCAL
    set getLastModifiedDateResult=%RETURN_VALUE%
)

exit
@echo off
goto :eof

I expect that

set tmpd="%%d ddd"

sets at least ddd as value for %tmpd%.

However, during execution, nothing is done:

C:\Windows\system32>for /F "tokens=* usebackq" %d in (`wmic datafile where Name="S:\\Actually\\Existing File.csv" get lastmodified | findstr "[0-9]"`) do (
set tmpd="%d ddd"  
    echo 111111   
    echo 222222 !tmpd!  
    echo 333333 %tmpd%  
    set RETURN_VALUE=~0,14 
)

I expect my for loop to be the cause for this problem. When I execute the exact same string in cmd.exe, I get a result:

C:\Users\uuuu>for /f "tokens=* usebackq" %d in (`wmic datafile where Name^="S:\\Actually\\Existing File.csv" get lastmodified ^| findstr ^"[0-9]^"`) do ( set tmpd=%d )

[xxx@yyy auf zzzz]
  ) sers\yc067xd>(set tmpd=20150413172700.000000+120

[yc067xd@R0199 auf FS00QHE0]
C:\Users\uuuu>echo %tmpd%
20150413172700.000000+120

Where did I do something wrong?

Mofi
  • 46,139
  • 17
  • 80
  • 143
KoenigGunther
  • 130
  • 1
  • 8
  • For me it works in a batch file, probably your file `S:\\Actually\\Existing File.csv` doesn't exist. Btw. Only `echo 2222 !tmpd!` makes sense – jeb Apr 21 '15 at 15:50
  • 1
    To work with the result of WMIC you should read [SO: Text garble in batch script for wmic command](http://stackoverflow.com/a/25604222/463115) – jeb Apr 21 '15 at 15:54
  • @jeb The thing is: I can open the file by open this path in Notepad++. Thus, I am pretty sure that this file is existing :/ Thank you for the !tmpd! hint. I am very confused when to use which - how is it called? - variable identifier. – KoenigGunther Apr 22 '15 at 13:09
  • For the difference between percent and delayed expansion you can read [SO: Batch Beginner Bug](http://stackoverflow.com/a/14347131/463115) – jeb Apr 22 '15 at 13:59
  • To test it you should use the same file from the command line and in the batch file. And you could remove the `^| findstr ^"[0-9]^` part to see the real content – jeb Apr 22 '15 at 14:01
  • @jeb Thank your for the link! Whoops! I actually used the same file for commandline and in the batch file. The filename in the last example simple was not obfucated while writing the question. – KoenigGunther Apr 22 '15 at 14:24
  • Do I see it correctly that the command propmpted out while `@echo on` should be executable by copying it into a command prompt and hitting enter? If so, the generated command lacks the `^`-signs. Thus, I'd guess I have to escape them accordingly. However, a single `^^` results in a `^^` prompted out by `@echo on` in the end. `\^` and `%^` (yes, I am a noob) won't do what I want, too :/ – KoenigGunther Apr 23 '15 at 08:06
  • `echo on` is the default for the command prompt and also in a batch file, if it's on in a batch file then all commands/blocks are echoed before they are executed, but it's output an intermediate parser step where the percent expansion and the escaping is already done. – jeb Apr 23 '15 at 08:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76018/discussion-between-jeb-and-koeniggunther). – jeb Apr 23 '15 at 08:26
  • If you don't need exactly the WMIC date format, "for" can get the filedate for you using %~t: for %%a in (*.txt) do echo %%~ta – joeking May 13 '15 at 16:39

2 Answers2

0

Do you have to use wmic ? There is an easier way to return the last modified date:

FOR %%f IN ("%file%") DO SET lastmodified=%%~tf

So your script would look like:

:getLastModifiedDate
  @echo on
  SETLOCAL enabledelayedexpansion
  set FILE=%~f1
  set FILE=!FILE:%NETWORK_DRIVE_SHARE_PATH%=%NETWORK_DRIVE_NAME%!
  set FILE=%FILE:\=\\%
  set RETURN_VALUE="internal script error"

  FOR %%f IN ("%FILE%") DO SET getLastModifiedDateResult=%%~tf
  set getLastModifiedDateResult=%getLastModifiedDateResult:~0, 10%
  @echo off
endlocal & goto :eof
cyberponk
  • 1,585
  • 18
  • 19
0

The most straightforward solution to the problem of extracting a file's dates can be used to obtain, as a text string, both the CREATED date (and time) and the LAST MODIFIED date (and time).

This solution overcomes a particular difficulty, where the path to the file contains a space (or other non-alphanumeric character).

@echo off

:: File location
SET file=C:\Users\%username%\Desktop\Get TIMESTAMP\file.txt

:: Get CREATED timestamp of specified file
FOR /F "tokens=1,* delims= " %%A IN ('DIR /T:C /A:-D "%file%" ^|FIND "/"') DO (
  SET Cdate=%%A
  SET Ctime=%%B
  SET Ctime=!Ctime:~0,5!
  :: Get file Creation Date
  SET  DCREATED=!Cdate!
  ECHO CREATED DATE: !DCREATED!
  :: Get file Creation Time
  SET TCREATED=!Ctime!
  ECHO CREATED TIME: !TCREATED!
) & ECHO.

:: Get LAST MODIFIED timestamp of specified file
FOR /F "tokens=1,* delims= " %%A IN ('DIR /T:W /A:-D "%file%" ^|FIND "/"') DO (
  SET Mdate=%%A
  SET Mtime=%%B
  SET Mtime=!Mtime:~0,5!
  :: Get file MODIFIED Date
  SET  DMODIFIED=!Mdate!
  ECHO MODIFIED DATE:  !DMODIFIED!
  :: Get file MODIFIED Time
  SET TMODIFIED=!Mtime!
  ECHO MODIFIED TIME:  !TMODIFIED!
) & ECHO.
Ed999
  • 2,801
  • 2
  • 16
  • 19