Copy file and append modified date in batch script

时间:2015-07-28 17:10:27

标签: batch-file

So I would like to create a batch script so that I could put it in my SendTo folder. I would like it to do the following:

When I select the script in my send to window, I would like it to make a copy of the file to the same directory but add the date (_YYYYMMDD) to the following. So if the file is called

test.txt

and I send it to the script, it should make a new file called

test_YYYYMMDD.txt

I have a script that adds the current date, but I would like one to do the last modified date. The current script I have is the following:

@echo off

:: Tokenize date for format yyyymmdd
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do ( 
Set Month=%%A
Set Day=%%B
Set Year=%%C
Set All=%%C%%A%%B
)

copy %1 "%~d1%~p1%~n1_%All%%~x1" 

Thanks in advance

1 个答案:

答案 0 :(得分:0)

@echo off
setlocal enableDelayedExpansion

rem Get the file date
for /f %%f in ('forfiles /p "%~dp1\" /m "%~nx1" /c "cmd /c echo @fdate"') do (

    rem Split the date by commonly used separators
    for /f "delims=.,/ tokens=1,2,3" %%a in ("%%f") do (

        rem Zero-pad month and day
        set month=0%%a
        set day=0%%b

        rem Rename the file to FileName_YYYYMMDD.Ext
        copy /b "%~1" "%~dpn1_%%c!month:~-2!!day:~-2!%~x1"
    )
)
pause