Kale
Kale

Reputation: 65

Add Date/time/old location to file name Batch

I have the following code:

@echo off
setlocal disableDelayedExpansion

set "src=C:\test"
set "dst=C:\test2"
set "search=test"

for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal enableDelayedExpansion
copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!"
endlocal
)

what this code does is copy a file from 1 location and put it on other location and change the name and put date/time in the filename.

Where i was looking for is that behind the filename and time also comes the location of the copyed file. so something like this:

Filename-10-03-2014-15:58:45-C:\test\test1\testfile.txt

so i can see the date time and old path in my filename.

Hope you guys can help.

Kind regards,

Kaluh

Upvotes: 1

Views: 73

Answers (1)

Magoo
Magoo

Reputation: 79983

@echo off
setlocal disableDelayedExpansion

set "src=C:\sourcedir"
set "dst=C:\destdir"
set "search=jpg"

for /r "%src%" %%F in (*%search%*) do (
 set "full=%%~fF"
 set "name=%%~nxF"
 setlocal ENABLEDELAYEDEXPANSION
 SET "appendix=!full::=-!"
 SET "appendix=!appendix:\=_!"
 ECHO copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!-!appendix!"
 endlocal
)

GOTO :EOF

I changed the search pattern, source and destination directories to suit my system.

You would need to nominate your own substitute characters for : and \ which cannot appear in a filename.

The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.

Upvotes: 1

Related Questions