10

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?

6
  • Why are you using DOS? Commented Apr 7, 2016 at 15:35
  • 2
    I think you mean Windows Command Prompt (cmd.exe) rather than MS-DOS. You can't have file name extensions > 3 letters in MS-DOS so you could not create a file called something.xlsx for instance. Commented Apr 7, 2016 at 15:41
  • @Radmation: Because there is no alternative to it here Commented Apr 7, 2016 at 15:41
  • @Jack Hughes: I have edited my post. We are taking .bat Commented Apr 7, 2016 at 15:44
  • 1
    You may find the question answered more quickly on the Super User or Server Fault forums. Commented Apr 7, 2016 at 15:49

2 Answers 2

13

?

/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 /?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your post, I did not know these rules (no use of concatenation symbols, quotation marks...) I am still interested in how to debug such script (run line by line).
batch is very basic. Debugging is basically done manually with echo and pause
here is an overview with possible cmd commands (a few of them don't work on all windows versions) including a good description of usage and syntax. If you are new to it, consider learning powershell instead (native to current windows versions). It's much more powerful and has a designed consistent syntax. If you decide to stay with batch - welcome to the land of headache (well - one gets used to it...).
0
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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.