简体   繁体   中英

Copy file and append modified date in batch script

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

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM