Change date format in Batch

0

I am creating a script in Batch that self-downloads the backup of an S3 every day. I already have everything that has to do with the S3Browser but I have a small problem when downloading the last backup (the most recent one). The backups are assigned the name in this date format YYYY-MM-DD (ex: 2018-04-26). How could I make the batch take the% date% of the PC and convert it to the correct format? Here I leave the script so far:

cd %ProgramFiles%\S3 Browser
s3browser-con
s3browser-con.exe download Server:[password] bucket/2018-04-24/ C:\Backups
    
asked by blee1d 26.04.2018 в 18:47
source

2 answers

1

In the end I found another way to solve the problem: creating 3 variables for the years, months and days.

set ano=%date:~6,4%
set mes=%date:~3,2%
set dia=%date:~0,2%

And then replacing the variables in the file name with the order that we want.

cd %ProgramFiles%\S3 Browser
s3browser-con
s3browser-con.exe download Server:[password] bucket/%ano%-%mes%-%dia%/ C:\Backups
    
answered by 02.05.2018 / 16:35
source
2

Try this:

set DateTimeNow=
for /f "tokens=2,3,4 delims=/ "  %%a in ("%date%") do (set MM=%%a& set DD=%%b& set YYYY=%%c)

It is important for this case in delims=/ " to leave the space after / with this we are establishing that carateres we are going to use to split the output in my case when executing date sale vie. 27/04/2018 in your case this may vary, depending on the regional configuration. In my case windows 10 this line to run it within a bat goes like this for /F "tokens=2,3,4 delims=/ " %a in ("vie. 27/04/2018") do (set MM=%a & set DD=%b & set YYYY=%c )

Then by console appears (set MM=27 & set DD=04 & set YYYY=2018 )

set DateTimeNow=%YYYY%-%MM%-%DD%

Then it appears like set DateTimeNow=2018-27-04

And then this was what you already had.

cd %ProgramFiles%\S3 Browser
s3browser-con
s3browser-con.exe download Server:[password] bucket/%DateTimeNow%/ C:\Backups

Page with example

about tokens

    
answered by 26.04.2018 в 19:08