0

I want to create a new folder in the current directory with the current date and time and save the string to a variable to save other files inside the directory.

This is what I have tried so far.

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%") do (set mytime=%%a%%b)
set dirname /f = %mydate%_%mytime%
mkdir "%dirname%"

copy a.txt %dirname%/
copy b.txt %dirname%/
copy c.txt %dirname%/

The variable dirname contains the name of the folder that I want. But, I cant use it to make a directory and copy files in it.

pnkjmndhl
  • 565
  • 3
  • 7
  • 21
  • 1
    Possible duplicate of [How do I get current datetime on the Windows command line, in a suitable format for using in a filename?](https://stackoverflow.com/questions/203090/how-do-i-get-current-datetime-on-the-windows-command-line-in-a-suitable-format) – aschipfl Jul 31 '17 at 16:15
  • The only Issue I have is, I cannot save the string with date to a variable and use it to create a folder and use it again to copy multiple files in it. – pnkjmndhl Jul 31 '17 at 16:24
  • 1
    That is not correct batch syntax: `set dirname /f = %mydate%_%mytime%` use `set "dirname=%mydate%_%mytime%"` But IMO you should use wmic to get a locale/user settings independent date time string - there are lots of examples on [SO]. Also use a trailing backslash on the folder not a slash. –  Jul 31 '17 at 16:43

3 Answers3

0

I would create the file that I want to save the data to by myself, and then, to save the date and time to it, I would do this when your program finishes running (provided that you have datetime imported):

finishtime = datetime.datetime.now()
finishtime = finishtime.strftime('%Y-%m-%d %H:%M')
filename.write(finishtime)
filename.write('\n')

The newline is just so you can view the times separately. You can write anything else if you want.

Does this help?

Sorry for being so late, I had to take a phone call.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I run a program that creates several outputs in the current folder. So, just to keep a backup of the results that I get during each execution, I want to create a folder using the current date/time and save all files in it. – pnkjmndhl Jul 31 '17 at 16:05
  • Okay. I'll give you the answer in the main body. –  Jul 31 '17 at 16:07
  • what language are you using? I am not familiar with that. I am trying to solve this using a batch file. – pnkjmndhl Jul 31 '17 at 16:26
0

Before I started using Microsoft PowerShell, I used the following technique:

@set @_=1/*
@echo off
for /f %%a in ('cscript.exe "%~0" //e:jscript //i //nologo') do set dirname=%%a
mkdir "%dirname%"
copy a.txt "%dirname%"
copy b.txt "%dirname%"
copy c.txt "%dirname%"
exit
*/
var d = new Date();
WScript.echo( d.getYear().toString(10)
            + '-' + ('0' + (d.getMonth() + 1).toString(10)).slice(-2)
            + '-' + ('0' + d.getDate().toString(10)).slice(-2)
            + '_' + ('0' + d.getHours().toString(10)).slice(-2)
            + ('0' + d.getMinutes().toString(10)).slice(-2) );
WScript.Quit(0);
Andrei Odegov
  • 2,925
  • 2
  • 15
  • 21
0
@Echo off
For /f %%A in ('powershell -NoP -C "Get-Date -f \"yyyy-MM-dd_HHmm\""') Do Set "dirname=%%A"
mkdir "%dirname%"
copy a.txt "%dirname%\"
copy b.txt "%dirname%\"
copy c.txt "%dirname%\"