Batch: Sequentially write the last modification time of a file into a variable

35 Views Asked by At

Within a FOR loop over all files from a folder, I try to set the file time into a variable, but the variable keeps displaying the same value for all files.

Here is the .bat file content:

@echo off

for /f %%a in ('dir /b .\*.*') do (
    echo a : %%a
    echo ta: %%~ta
    set filetime = %%~ta
    echo filetime: %filetime%
)

Here is the output:

a : File1.txt
ta: 10/03/2023 17:09
filetime: "02/10/2020 17:44"
a : File2.txt
ta: 10/03/2023 17:10
filetime: "02/10/2020 17:44"
a : File3.txt
ta: 10/03/2023 14:02
filetime: "02/10/2020 17:44"
a : test.bat
ta: 10/03/2023 14:35
filetime: "02/10/2020 17:44"

My purpose was to have the actual file time (%%~ta) written in the variable filetime.

1

There are 1 best solutions below

0
Matthew Snyder On

Try removing the spaces around the '='. New line should be: set filetime=%%~ta

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/set_1