2

I used the following to create a folder with a date and timestamp

@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a-%%b)

set mydir="%mydate%-%mytime%"

mkdir %mydir%

Instead of created a folder named 2020-5-19-11-30 AM, I get one folder named 2020-5-19 and another folder called AM.

Any idea? I just want to run a batch file that will create a date and time including am or pm for a folder.

Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 1
    Would you mind trying to print the result of `date /t` and `time /t`? The values are different for different machines. – loadingnow May 19 '20 at 15:32
  • 3
    `date` and `time` formats depend on your settings (you can change them in the control panel or through the registry) You can also check [this](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format/19799236#19799236) – npocmaka May 19 '20 at 15:41
  • Are you saying you get two folders next to each other, although there is a single `mkdir` command? Well, I can't reproduce that... – aschipfl May 19 '20 at 16:30
  • There are already two variables called `%date` and `%time%` that are built in. Put quotes around the `%mydir%` in the `mkdir "%mydir%"`. There might be a delimiter in the `%mydir%` variable – Nico Nekoru May 19 '20 at 17:13
  • Like @npocmaka said, `date` and `time` formats are different depending on the computer, however for me, if I remove the quotes in teh definition of `%mydir%` so it is defined like `set mydir=%mydate%-%mytime%` and then `mkdir "%mydir%"` it worked fine. `%date%` and `%time%` are already environment automatic variables so your `for` loop is redundant unless you want a different format than the actual `%date%` and `%time%` commands. – Nico Nekoru May 19 '20 at 17:20

0 Answers0