Computing.Net > Forums > Disk Operating System > Naming directory with current date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Naming directory with current date

Reply to Message Icon

Name: jasmine
Date: November 5, 2003 at 11:47:29 Pacific
OS: Windows XP
CPU/Ram: Pentium 4 and 1 GB RAM
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: November 5, 2003 at 14:10:35 Pacific
Reply:

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...)


0

Response Number 2
Name: dtech10
Date: November 5, 2003 at 14:12:24 Pacific
Reply:

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%



0

Response Number 3
Name: jasmine
Date: November 7, 2003 at 14:46:54 Pacific
Reply:

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?


0

Response Number 4
Name: dtech10
Date: November 8, 2003 at 14:17:19 Pacific
Reply:

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.



0

Response Number 5
Name: jasmine
Date: November 10, 2003 at 13:09:39 Pacific
Reply:

dtech10, I tried again and it worked now. I must have made a mistake earlier. Thanks to you both.


0

Related Posts

See More



Response Number 6
Name: IVO
Date: November 10, 2003 at 13:20:54 Pacific
Reply:

Check your inbox to see a tip for the zip question.


0

Response Number 7
Name: Kemet
Date: November 11, 2003 at 17:09:58 Pacific
Reply:

IVO,

Could you explain what ~-(number) represents in the string?

TEST_%Date:~-4,4%%Date:~-10,2%%Date:~-7,2%

Thanks Kemet


0

Response Number 8
Name: IVO
Date: November 12, 2003 at 01:48:21 Pacific
Reply:

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).


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Naming directory with current date

naming files with current date www.computing.net/answers/dos/naming-files-with-current-date/7725.html

Rename file with current date www.computing.net/answers/dos/rename-file-with-current-date/13069.html

create file with current datetime www.computing.net/answers/dos/create-file-with-current-datetime/13074.html