Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert date in batch file

Status
Not open for further replies.

2009luca

Programmer
Jul 27, 2013
222
IT
: need to copy \\Myserverdir\File.txt to C:\mydir\Test_(date_creation).txt

setlocal

:: the following are what I used to test - change to yours!
set folder=\\SynDS214\share
set file=File.txt
set target=D:\Backup

:: get the file's date as dd/mm/yyyy
for /f "tokens=1" %%a in ('dir "%folder%\%file%" ^| find /i "%file%"') do set filedate=%%a

:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~3,2%%filedate=~0,2%

:: perform the copy, if the file is not present in the target
if not exist "%target%\Test_(%date_created%).txt" ^
copy "%folder%\%file%" "%target%\Test_(%date_created%).txt"

:: check on the copied file
dir "%target%\Test_(%date_created%).txt"

endlocal

why the conversion od date return 201607 instead 20160712
 
Change "=" to ":" in day:
set date_created=%filedate:~6,4%%filedate:~3,2%%filedate:~0,2%

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top