简体   繁体   中英

When and what to escape within FOR /F and WMIC in a batch file

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM