0
for /f %%f in ('dir /b "\\rspdb1\e$\master10\rspadvrec.b*" "\\rspdb1\e$\master10\rspadvrec.d*" "\\rspdb1\e$\master10\rspadvrec.lg*" "\\rspdb1\e$\master10\rspadvpdt.a*"') do (
    set fileDateTime=%%~tf
    set fileName=%%f
    echo %fileDateTime%
    echo %fileName%
)

As the code above, I want to list down the modified date of the files in the folder, however, it doesn`t work, any suggestions?

user2400583
  • 13
  • 1
  • 5

1 Answers1

1

You should add setlocal EnableDelayedExpansion in the beginning of the script, and use !variable! instead of %variable%.

For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.

You also need to add option /s to dir to get the full path of your file to get the data time correctly. And then if you only want the file name, use %%~nxf instead of %%~f.

Here are the codes.

@echo off & setlocal EnableDelayedExpansion
for /f %%f in ('dir /b /s "\\rspdb1\e$\master10\rspadvrec.b*" "\\rspdb1\e$\master10\rspadvrec.d*" "\\rspdb1\e$\master10\rspadvrec.lg*" "\\rspdb1\e$\master10\rspadvpdt.a*"') do (
    set fileDateTime=%%~tf
    set fileName=%%~nxf
    echo !fileDateTime!
    echo !fileName!
)
Community
  • 1
  • 1
Landys
  • 7,169
  • 3
  • 25
  • 34
  • I`m sorry that I forgot to mention "setlocal EnableDelayedExpansion" is already at the start of code, and !fileDateTime! did not work. – user2400583 Sep 01 '14 at 07:05
  • Just modified the answer. To get the data time, you need to pass "/s" to "dir" to get the full path of your file. And then use "%%~nxf" to get the file name from the full path. – Landys Sep 01 '14 at 07:15
  • Excellent. It works. Thanks a lot for the valuable time and answer – user2400583 Sep 01 '14 at 07:55
  • Hi, I want to put a if else statement in the for loop as well , like if !fileDateTime:~0,10! == 01/09/2014, however it does not work, any suggestions? – user2400583 Sep 01 '14 at 08:00
  • You need to add quotes, like `if "!fileDateTime:~0,10!" == "01/09/2014"`. – Landys Sep 01 '14 at 08:06