ProtoVB
ProtoVB

Reputation: 793

Concatenating strings and variables in batch

I would like to create a dynamic file path in .bat file. At the moment the file path looks like this:

"C:\FolderA\FolderB\FileA.xlsx"

I would like to incorporate today's date in the file name to produce something like that:

/exp:"C:\FolderA\FolderB\FileA " & date() & ".xlsx" /T`

I have tried creating a variable and concatenating it with the hard coded part but it does not work:

set Mydate=!date:~10,4!!date:~7,2!!date:~4,2!
/exp:"C:\FolderA\FolderB\FileA "&%Mydate%&".xlsx" /T

What are the rules on concatenating characters and variables and on quotation marks in batch? How to debug in batch using Notepad?

Upvotes: 10

Views: 68664

Answers (2)

Brent
Brent

Reputation: 1

for /f "tokens=1-7 delims=/: " %%a in ("%date% %time%") do (

set idow=%%a
set imonth=%%b
set idate=%%c
set iyear=%%d
set ihour=%%e
set imin=%%f
set isec=%%g

set vDate=%%d-%%b-%%c-%%e-%%f

)
cd /d "%log_dir%"
rename %prefix%.log %prefix%-%vDate%.log

Upvotes: 0

Stephan
Stephan

Reputation: 56238

?

/exp:"C:\FolderA\FolderB\FileA "&%Mydate%&".xlsx" /T

?

This is not cmd syntax. To set a variable, use the set command. Also to concatenate, you don't have (read: must not) use something like concatenation symbols:

set "var=C:\FolderA\FolderB\FileA %Mydate%.xlsx"

(whatever /exp: or /t is supposed to do - it does not work in cmd)

To rename a file, use ren (or the long form rename). To get help to a command use command /? e.g. rename /?

Upvotes: 13

Related Questions