Reputation: 388
I want to create a folder with the name of the current date and time. After searching a lot i found this which actually works.
Can someone explain what these batch commands do?
set timestamp=%DATE:/=-%@%TIME::=-%
set timestamp=%timestamp: =%
mkdir "%timestamp%"
Upvotes: 1
Views: 204
Reputation: 7095
Insert echo statements between the lines and you can see what the value of timestamp is
set timestamp=%DATE:/=-%@%TIME::=-%
echo %timestamp%
set timestamp=%timestamp: =%
echo %timestamp%
mkdir "%timestamp%"
Basically, the code is just removing the forward slash from the date and the colon from time since those are not valid directory names replacing them with hypens.
Read set /? Environment variable substitution to get a better idea.
Upvotes: 5
Reputation: 20464
set timestamp=%DATE:/=-%@%TIME::=-%
That's a string replacement.
1st:
%DATE:/=-% Replaces "/" character to "-" character in the DATE variable
(See: Echo %DATE% on your console)
2nd:
Adds the "@" character to the string after the DATE var and before the TIME var.
3rd:
%TIME::=-% Replaces ":" character to "-" character.
(See: Echo %Time% on your console)
set timestamp=%timestamp: =%
Next in that replacement replaces itself spaces to any characarter (so deletes spaces), but really any space is given so is not necessary in your example code.
You can learn more about Variable string replacement here: http://ss64.com/nt/syntax-replace.html
Also you can simplify your code 'cause no need to setting the value first:
mkdir "%DATE:/=-%@%TIME::=-%"
Upvotes: 4