How to append date to directory path in xcopy

独自空忆成欢 提交于 2019-12-01 06:37:51

问题


I do have a xcopy statement in bat file.. would you please help me to append today's date to one of directories in destination xcopy /S /E /I %sourceFolder% "C:\Shared\copy\%destinationFolder%"

today date is 06072013 so I want my destination look like below

C:\Shared\copy-today's date........

Thanks


回答1:


This is method of getting a date stamp that doesn't depend on the regional settings. Wmic is available in Windows XP Pro and higher.

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set stamp=%YYYY%-%MM%-%DD%

md "C:\Shared\copy-%stamp%"
xcopy here...



回答2:


xcopy /S /E /I %sourceFolder% "C:\Shared\copy-%date:/=%\%destinationFolder%"



回答3:


Just use %date% in your command:

xcopy /S /E /I %sourceFolder% "C:\Shared\copy\copy-%date%"

Note: this will keep the date in the original format.

Assuming your local date format is Fri 06/07/2013 you can format it into 06072013 by cutting up the string like this:

%date:~4,2%%date:~7,2%%date:~10,4%

So the final command will be:

xcopy /S /E /I %sourceFolder% "C:\Shared\copy\copy-%date:~4,2%%date:~7,2%%date:~10,4%"



回答4:


Something like this...

for /f "tokens=2-4 delims=/ " %%A in ('echo.%Date%') do set Dest=C:\Shared\copy-%%A%%B%%C
xcopy /S /E /I "%sourceFolder%" "%Dest%"


来源:https://stackoverflow.com/questions/16994879/how-to-append-date-to-directory-path-in-xcopy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!