Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am taking backups of my files everyday and I am using a batch file to do this. Currently it is being copied to the same folder and gets overwritten. I don't want this to get overwritten and want to be able to create folders with the current date appended to it and store these for a period of time, for example folder test_20031105. Is this possible to do in the DOS batch file? Also I would like to zip the backup files, how can I do this in the batch file?

To create a directory with the current date use the following (based upon the system XP variable %Date%)
MD TEST_%Date:~-4,4%%Date:~-10,2%%Date:~-7,2%
I assume you use the US date format (MMDDYYYY) otherwise change the relative Date element position (%Date:....%).
To zip the files is possible, but the exact format of the command is related to the software you currently use to compress (WinZip, PowerArchiver and so on...)

Hi jasmine
Try Sometime like this
@echo off
Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
md test_%Yr%%Mth%%Day%

Thanks so much! IVO's solution worked. I tried using dtech10's code into a batch file and it was not successful.
IVO,I use Winzip for compression. Can you let me know what command I should use?

Hi Jasmine
I pasted the text from the above item into notepad and saved it as a batch file and it worked ok for me. I'm using WinXP Pro
but I would'nt have throught take would make a difference.
Anyway IVO's solution is much neater.

IVO,
Could you explain what ~-(number) represents in the string?
TEST_%Date:~-4,4%%Date:~-10,2%%Date:~-7,2%
Thanks Kemet

Hi Kernet,
the :~ means you want to refer to the substring embedded in the source variable, according to the syntax %Var:~Off,Lgt% where Off is the offset starting from 0 and Lgt the size of the substring you want to extract.
If the offset is negative (-), the offset is computed from the end of the string. So the first %Date:... means select the substring in Date that is located at the last four positions (in other words the last four characters). I leave you to interpret the other two substrings.
Why did I process the %Date% backward? To get compatibility between NT/2K and XP Date format, as XP omits to return the day of week. Parsing as I do gives invariance respect to the operating environment.Anyway to know more type Set /? at command prompt (Win NT/2K/XP required).

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |