Hai All,
How to add date to the file at DOS(CMD line).
I would like to use for AutoBackUps on date wise.
Regards
Hai All,
How to add date to the file at DOS(CMD line).
I would like to use for AutoBackUps on date wise.
Regards
Quick summary and recommended approaches for adding the date to a filename from the Windows command line (for automated backups). wanted a DOS/CMD solution; as pointed out, use only characters valid in filenames (avoid / \ : * ? " < > |). Several replies show simple substring tricks that rely on the system %DATE% layout — those can work but are brittle because the date string changes with regional settings and with how a scheduled task runs the script.
A robust, locale-independent batch method uses the OS timestamp returned by WMIC and then builds an ISO-style date (YYYY-MM-DD). Put this in a .bat file (double percent signs are required inside a batch file):
@echo off
for /f "tokens=1 skip=1" %%a in ('wmic os get LocalDateTime') do if not defined LDT set LDT=%%a
set YYYY=%LDT:~0,4%
set MM=%LDT:~4,2%
set DD=%LDT:~6,2%
set FNAME=Backup_%YYYY%-%MM%-%DD%.zip
echo Would create "%FNAME%"
rem Replace the echo with your backup command, for example a compression or copy command. On systems with PowerShell available, the simplest and most readable option is to ask PowerShell for a formatted date and build the target name there (good for modern Windows and avoids parsing). Example one-liner:
powershell -NoProfile -Command "Compress-Archive -Path 'C:\Data\*' -DestinationPath ('C:\Backups\backup_' + (Get-Date -Format yyyy-MM-dd) + '.zip')" Practical tips: test first by echoing the filename instead of performing the backup; when scheduled, always use full paths and check the account that runs the task (it may have different locale/settings); avoid characters forbidden in filenames; and remember that in a batch file for variables use %%a but on the interactive prompt use %a. ’s substring approach is fine for quick, local scripts, but for scheduled, multi-machine, or long-lived automation prefer the WMIC or PowerShell patterns above.
Jump to Post— MartyMcFly 9Are you trying to use / or . or \ . You can't use these in dos or windows as part of a file name.
Use something like Backup060405.
Hope I actually understood your problem there.
Jump to Post— Thong_Ispector 2I wrote a program to do that a few years back. It would create a filename using the current date. It was easy to set the parameters of the backup to whatever files I wanted to save in the backup. I will try to find it and make it available.
…
Are you trying to use / or . or \ . You can't use these in dos or windows as part of a file name.
Use something like Backup060405.
Hope I actually understood your problem there.
I wrote a program to do that a few years back. It would create a filename using the current date. It was easy to set the parameters of the backup to whatever files I wanted to save in the backup. I will try to find it and make it available.
I do remember looking for a program to do it and did not have much luck.
It also turned out to be harder to write than I expected... About 8 pages...
I'll check around for it.
Sorry, I just re-read your post, you want to run your DOS backup on specific dates not include the date in the filename...
Do you want this to be a TSR terminate and stay resident?
or
Are you planing to put this into the autoexec.bat and check the date on boot?
I see what you meant, sorry.
This is the easiest way i have found. This works natively with Windows and you don't have to download anything or install any scripts.
Hi,
With ref to
I would like share how I generated a file with date as filename.
In my environment (it varies according to your environment), my date command output is mm/dd/yyyy
eg: 11/01/2011
So to generate a filename with yyyy-mm-dd, I used following command
echo > %date:~6,4%-%date:~0,2%-%date:~3,2%.txt
So the output is 2011-11-01.txt
to elaborate %date:~6,4% --> From left, the cursor is moved six characters right and displayed only next four characters.
Another method is to change the date format of the system from Regional and Language options. i.e Change the date separator from "/" to "-" and required date format.
~~SAJU~~
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.