DoodleKana
DoodleKana

Reputation: 2206

same time stamp in batch script when using %time and %date inside for loop

    for /f %%i in ('dir /s /b "%FolderLocation%"') do (
    MOVE %%i "%processedFolder%Imported-%date:~4,2%-%date:~7,2%-date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%"
PING 1.1.1.1 -n 1 -w 10000 >nul
    )

The above script is always resulting in same timestamp. Basically it is moving file to new location from a given folder and adding timestamp to new files. But they all end up with same timestamp. I have 10 second delay which should make timestamp different. What is wrong?

Upvotes: 1

Views: 1273

Answers (1)

Monacraft
Monacraft

Reputation: 6630

In a for loop, using % to expand variables will use the value they had before the for-loop started. EnableDelayedExpansion lets you use ! instead of % and will work in a for-loop:

@echo off
setlocal enabledelayedexpansion

    for /f %%i in ('dir /s /b "%FolderLocation%"') do (
MOVE %%i "!processedFolder!Imported-!date:~4,2!-!date:~7,2!-!date:~10,4!_@_!time:~1,1!h!time:~3,2!m!time:~6,2!s!"
PING 1.1.1.1 -n 1 -w 10000 >nul
)

And that should work.

Upvotes: 2

Related Questions