DOS: create filename based on date? [Closed]

Report
-
Posts
1
Registration date
Saturday January 19, 2013
Status
Member
Last seen
January 19, 2013
-
Hello,

I need to be able to automatically generate a DOS .txt filename that is the 'date' of when it is created.

Any ideas? My DOS is rather rusty and have run out of ideas.

7 replies

You might try something like this...


set filea=MyBigFile%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.sav
copy MyData.dat %filea%

Since the date is Thu 07/30/2009 and the time is 14:07:00.00,
For today this would give a file named
MyBigFile200907301407.sav

good luck
24
Thank you

A few words of thanks would be greatly appreciated. Add comment

CCM 2942 users have said thank you to us this month

copy nul %date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.txt
Sorry, that won't work from a batch file! If you don't mind what is in the file then this will work:

echo >%DATE%.txt

And if a zero-length file is important to you then create one manually as described before, then use:

copy yourZeroLengthFile %DATE%.txt
Try this:

copy con %DATE%.txt

the press Ctrl-Z.

This will create a zero-length file named 24-Jul-2009.txt (or whatever).
You might try something like this...


set filea=MyBigFile%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.sav
copy MyData.dat %filea%

Since the date is Thu 07/30/2009 and the time is 14:07:00.00,
For today this would give a file named
MyBigFile200907301407.sav

good luck
Posts
25
Registration date
Saturday November 7, 2009
Status
Member
Last seen
July 18, 2012
14
It is better to use biterscripting for these kinds of things. DOS only has a subset of biterscripting commands.

This simple command will create the file <timestamp>.txt.

echo > { echo (gettime()+".txt") }
Posts
1
Registration date
Saturday January 19, 2013
Status
Member
Last seen
January 19, 2013

Small Business Server is NOT R2 - so you have to schedule it with a bat file.

Here is a script inspired by this page. I put it as a scheduled task. It writes to a log file, then does the backup to that log file and makes an end to the log.

It adds a time stamp to each backup.

rem
rem
rem get the date and time without the slashes and colons. Note if I echo date and echo time I get dd/mm/yyyy and time is hh:ss:mm.ms
set filea=%date:~6,4%-%date:~3,2%-%date:~0,2%_%time:~1,1%%time:~3,2%%time:~6,2%
rem now start the log file
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> E:\GeekCentral\scripts\results\backuplog_%filea%.log
echo %date% %time% %username% start >> E:\GeekCentral\scripts\results\backuplog_%filea%.log
rem
rem - make a place to put the new backups
rem - note that I have a drive mapped to a shared folder "GeekCentral" so if I went to \\sbs-server\GeekCentral I have a folder there
rem that has the drive mapped here - and to my f: disk.
rem - see https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc753321(v=ws.11)?redirectedfrom=MSDN
mkdir "e:\GeekCEntral\BackupMount\Backup_%filea%"
rem
rem - now start the backup - see https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc742083(v=ws.10)?redirectedfrom=MSDN and
rem webpage https://social.technet.microsoft.com/Forums/windowsserver/en-US/1ab34447-4e81-4589-8aa1-8275cff132fc/no-disks-are-available-for-use-a-backup-destination?forum=windowsbackup
rem
Wbadmin start backup -backupTarget:\\sbs-server\geekcentral\BackupMount\Backup_%filea% -include:C:,E: -vssFull -quiet >>E:\GeekCentral\scripts\results\backuplog_%filea%.log
rem
rem - end the log file
echo %date% %time% %username% end >> E:\GeekCentral\scripts\results\backuplog_%filea%.log
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end >> E:\GeekCentral\scripts\results\backuplog_%filea%.log