Use value of a variable in batch command

1

What I would like to do is to insert a variable in a command such as RENAME . The goal is to change the name of the file to the current time and date. This is the code I have tried to use (so that they understand my intention):

SET nombre=%time%, %date%

rename archivo1.txt %nombre%.txt
    
asked by Jordi Fuentes 03.08.2017 в 13:57
source

2 answers

1

14:38:00, 03/08/2017.txt :

  • needs to be enclosed in quotes or the blank space will confuse the rename syntax.

  • is an illegal file name in windows ( : , / ).

You can replace the illegal characters using this example , which would you give us:

set nombre=%time%, %date%
set nombre=%nombre:/=_%
set nombre=%nombre::=-%
rename archivo1.txt %nombre%.txt
    
answered by 03.08.2017 в 14:46
0

There is a problem in your code, and that variable %nombre% will result in an invalid file name. Here is an option that outputs a file type log-2017.08.03-14.40.txt :

set FICHERO_DATE=%DATE:~6,4%.%DATE:~3,2%.%DATE:~0,2%
set FICHERO_TIME=%TIME:~0,2%.%TIME:~3,2%
set FICHERO=log-%FICHERO_DATE%-%FICHERO_TIME%.txt
rename archivo1.txt %FICHERO%
    
answered by 03.08.2017 в 14:46