Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect output to a file which name is the current date and time?

I would like to redirect the output of a command (in the Windows command line) to a file which name is the current date and time. For example:

my_path\mysqldump.exe my_database_name > auto_generated_file_name

where auto_generated_file_name should be something like 2010_09_30___11_41_58.txt.

This command will automatically run from time to time. This is the reason I need the file name to be automatically generated.

What is the easiest method to achieve this ?

like image 383
Misha Moroshko Avatar asked Sep 30 '10 01:09

Misha Moroshko


People also ask

How do I name a file with date and time?

I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.

How do I redirect an output to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do you write the date in a FileName?

a date, or at least the year, when the contents of the file were created, in the YYYY-MM-DD format (four digit year, two digit month, two digit day, with a dash in between.)

Can we redirect the output of a command to a file and display at the same time?

Redirecting output to Multiple files and screen: If you want to redirect the screen output to multiple files, the only thing you have to do is add the file names at the end of the tee command.


1 Answers

The following command creates a blank file with the expected filename:

> type nul > %date:~10,4%_%date:~4,2%_%date:~7,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%.txt

> dir /b
2010_09_29__22_12_44.txt

You can use the part after type nul > in place of your auto_generated_file_name.

like image 110
ars Avatar answered Sep 22 '22 05:09

ars