Home / Microsoft / Microsoft DOS / Create a directory with today's date and delete one with yesterday's
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3  All - (Bottom) Print
Author Topic: Create a directory with today's date and delete one with yesterday's  (Read 6246 times)
carson
Topic Starter
Greenhorn



Posts: 9


« on: February 26, 2009, 09:11:35 AM »

I'm using plain, vanilla Windows XP SP2.  This is my work computer, so I'm a user, not an administrator--that is to say, I can't install anything.

In the course of my work, I (amongst other things) delete a directory with yesterday's date if it exists and create a directory with today's date.  I'm looking for a way to automate this (and, eventually, other things, but this is the starting point).  I use Linux and Mac OS X at home, so my first thought was to download Cygwin (which can just be run; it doesn't have to be installed) at http://www.cygwin.com and write a quick Bash script:

Code: [Select]
#!/bin/bash
desktop="/cygdrive/c/Documents and Settings/carson/Desktop"

if [ -e $desktop/`date --date=yesterday +%Y%m%d` ]; then
   rm -r $desktop/`date --date=yesterday +%Y%m%d`
   mkdir $desktop/`date +%Y%m%d`
else
   mkdir $desktop/`date +%Y%m%d`
fi

This failed to work because Cygwin doesn't handle spaces in directory names well (or at all).  Also, the IT department (rightly, let it be said) wouldn't be pleased if they should discover me with unapproved programs.  So I deleted Cygwin and started trying to write a batch file.  Which meant I had to read up on it since the last time I wrote a batch file was, oh, 1994 or so.

With a little research, creating a directory with today's date is pretty easy:
Code: [Select]
@echo off
cd %USERPROFILE%\Desktop
mkdir "%DATE%"

Not the same date format as my Bash script above, but that doesn't matter since it's just to have a human read it, not to be parsed by anything.  But I cannot for the life of me figure out how to test for the existance of a directory named for yesterday's date.  Can somebody help or point me in the right direction?
IP logged
macdad-
Expert



Thanked: 39
Posts: 2,520


LoneWolf's Circuits
« Reply #1 on: February 26, 2009, 11:34:13 AM »

this ought to do the trick:
Code: [Select]
@echo off
cd %USERPROFILE%\Desktop
echo  The current date is: %DATE%
echo Creating Today's Folder
set today=%DATE:~0,3%
mkdir "%today%"
if '%today%'=='Mon' rmdir Sun /S /Q
if '%today%'=='Tue' rmdir Mon /S /Q
if '%today%'=='Wed' rmdir Tue /S /Q
if '%today%'=='Thu' rmdir Wed /S /Q
if '%today%'=='Fri' rmdir Thu /S /Q
if '%today%'=='Sat' rmdir Fri /S /Q
if '%today%'=='Sun' rmdir Sat /S /Q
pause

Hope this Helps
,Nick(macdad-)
IP logged

If you dont know DOS, you dont know Windows...

Thats why Bill Gates created the Windows NT Family.
Dias de verano
Guest
« Reply #2 on: February 26, 2009, 11:54:30 AM »

Macdad, supposing his %date% doesn't start with a three letter day-of-the-week? Mine looks like this 26/02/2009.


IP logged
macdad-
Expert



Thanked: 39
Posts: 2,520


LoneWolf's Circuits
« Reply #3 on: February 26, 2009, 12:24:14 PM »

no i tested it on a Win XP computer it does start with a 3 letter day: this is the output of date in XP:

Thu 02/26/2009
IP logged

If you dont know DOS, you dont know Windows...

Thats why Bill Gates created the Windows NT Family.
Dias de verano
Guest
« Reply #4 on: February 26, 2009, 01:03:56 PM »

no i tested it on a Win XP computer it does start with a 3 letter day: this is the output of date in XP:

Thu 02/26/2009


It's got nothing to do with the OS.

The date format returned by the date command depends on what the system regional settings are.

That date format above is what you get with the USA regional settings. That is:

date Ddd MM/DD/YYYY

time HH:MM AM/PM (12 hour clock)

However, there is a world outside the USA, and they mostly use a different format.

For example I live in Europe, where the regional settings mean that the date is DD/MM/YYYY and time is HH:MM (24 hour clock)

Code: [Select]
C:\>date /t
26/02/2009

C:\>time /t
19:52

This is actually the format used in most of the world outside the USA. (In Japan it is YYYY/MM/DD.)

My evaluate VBS script will return yesterday's date in the format set by the regional settings.

The vbs format for past dates is date-N where N is the numbers of days ago the date is.

http://www.computerhope.com/forum/index.php/topic,77361.msg507046.html#msg507046

Code: [Select]
C:\>evaluate date
26/02/2009

C:\>evaluate date-1
25/02/2009

C:\>evaluate date-10
16/02/2009

C:\>evaluate date-100
18/11/2008

C:\>evaluate date-365
27/02/2008

C:\>evaluate date-3650
01/03/1999

C:\>evaluate date-36500
23/03/1909


Or here is a complete batch solution

Code: [Select]

@echo off
echo wscript.echo (date-1)>yesterday.vbs
for /f "delims==" %%D in ('cscript //nologo yesterday.vbs') do set yesterdate=%%D
del yesterday.vbs
echo Yesterday's date was %yesterdate%


Maybe you would care to run it and confirm that it uses local date settings? I would be grateful for the information.

THis was my output, I called it yester.bat

Code: [Select]
C:>yester.bat
Yesterday's date was 25/02/2009

« Last Edit: February 26, 2009, 01:30:35 PM by Dias de verano » IP logged
macdad-
Expert



Thanked: 39
Posts: 2,520


LoneWolf's Circuits
« Reply #5 on: February 26, 2009, 03:50:41 PM »

i know there is a world outside of the US...i was kinda asumming again...

but...Carson are your regional settings like this:
Ddd MM/DD/YYYY
e.g.:  Thu 02/26/2009

at the very least if the first part is the same, "Thu" then it should work.
IP logged

If you dont know DOS, you dont know Windows...

Thats why Bill Gates created the Windows NT Family.
GuruGary
Adviser



Posts: 771




« Reply #6 on: February 26, 2009, 09:20:22 PM »

Or, I think you could use your cygwin to do
Code: [Select]
desktop="/cygdrive/c/Documents\ and\ Settings/carson/Desktop"
IP logged
Dusty
Egghead



Thanked: 72
Posts: 3,387

Experience: Beginner
OS: Windows XP


I could if she would, but she won't so I don't.

« Reply #7 on: February 26, 2009, 11:43:26 PM »

this ought to do the trick:
Code: [Select]
@echo off
cd %USERPROFILE%\Desktop
echo  The current date is: %DATE%
echo Creating Today's Folder
set today=%DATE:~0,3%
mkdir "%today%"
if '%today%'=='Mon' rmdir Sun /S /Q
if '%today%'=='Tue' rmdir Mon /S /Q
if '%today%'=='Wed' rmdir Tue /S /Q
if '%today%'=='Thu' rmdir Wed /S /Q
if '%today%'=='Fri' rmdir Thu /S /Q
if '%today%'=='Sat' rmdir Fri /S /Q
if '%today%'=='Sun' rmdir Sat /S /Q
pause


Anyways, would this work?  If today is Sat and a folder named Sat already exists would the Mkdir command not fail due to duplication?

Apart from that the OP posted
Quote
In the course of my work, I (amongst other things) delete a directory with yesterday's date if it exists and create a directory with today's date.  I'm looking for a way to automate this (and, eventually, other things, but this is the starting point)
so what is wanted is a folder named with today's date not day-of-the-week.

Perhaps the following if the date format day mm/dd/yyyy is correct:

Code: [Select]
@echo off
cls

set today=%date:~-4%%date:~4,2%%date:~7,2%

echo wscript.echo (date-1)>yesterday.vbs
for /f "delims==" %%D in ('cscript //nologo yesterday.vbs') do (
        set yesterdate=%%D
)
set yesterdate=%yesterdate:~-4%%yesterdate:~4,2%%yesterdate:~7,2%
del yesterday.vbs

echo Todays date is %today%
echo Yesterday's date was %yesterdate%

If exist %userprofile%\desktop\%yesterdate% rd %userprofile\desktop\%yesterdate% /S /Q

:: md %userprofile%\desktop\%today%


After testing remove the :: before md to create the folder with the name in the format yyyymmdd

No doubt Dias can do that in just a couple of lines ;D
« Last Edit: February 27, 2009, 12:38:23 AM by Dusty » IP logged

One good deed is worth more than a year of good intentions.
Dias de verano
Guest
« Reply #8 on: February 27, 2009, 12:18:33 AM »

 I think we need the OP to tell us

1. His system's date format

2. Exactly how the folders in question are named for the date (given that the / character is illegal in a file or folder name.)

That is if he or she is still around?

[Edit] Dusty, I see you picked up these points in your edit.
« Last Edit: February 27, 2009, 01:00:26 AM by Dias de verano » IP logged
macdad-
Expert



Thanked: 39
Posts: 2,520


LoneWolf's Circuits
« Reply #9 on: February 27, 2009, 06:14:56 AM »

Carson hasn't replied back...so we cant assume which would work for him until he says which he'll take.
IP logged

If you dont know DOS, you dont know Windows...

Thats why Bill Gates created the Windows NT Family.
carson
Topic Starter
Greenhorn



Posts: 9


« Reply #10 on: February 27, 2009, 07:01:06 AM »

Or, I think you could use your cygwin to do
Code: [Select]
desktop="/cygdrive/c/Documents\ and\ Settings/carson/Desktop"

That was my first thought as well, but apparently Cygwin already uses a backslash internally for spaces because I got errors about "Documents\\ and\\ Settings" not being a valid directory.
IP logged
carson
Topic Starter
Greenhorn



Posts: 9


« Reply #11 on: February 27, 2009, 07:05:40 AM »

I think we need the OP to tell us

1. His system's date format

I'm in the US, so "date /t" returns "Fri 02/07/2009".

2. Exactly how the folders in question are named for the date (given that the / character is illegal in a file or folder name.)

Right now I'm naming them YYYYMMDD, so today's, for example, would be 20090227.   I'm not absolutely insistent that it be that way; in fact, I don't really care how it gets expressed, as long as it's the date.

That is if he or she is still around?

Yes, still here.  Or here again, rather--I got tied up with the aforementioned work.
IP logged
carson
Topic Starter
Greenhorn



Posts: 9


« Reply #12 on: February 27, 2009, 07:17:08 AM »


Code: [Select]
@echo off
cls

set today=%date:~-4%%date:~4,2%%date:~7,2%

echo wscript.echo (date-1)>yesterday.vbs
for /f "delims==" %%D in ('cscript //nologo yesterday.vbs') do (
        set yesterdate=%%D
)
set yesterdate=%yesterdate:~-4%%yesterdate:~4,2%%yesterdate:~7,2%
del yesterday.vbs

echo Todays date is %today%
echo Yesterday's date was %yesterdate%

If exist %userprofile%\desktop\%yesterdate% rd %userprofile\desktop\%yesterdate% /S /Q

:: md %userprofile%\desktop\%today%


This outputs the correct date for today, but not for yesterday.  I saved it as dailysetup.bat.  Here's what I'm getting as output.

Code: [Select]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>cd c:\documents and settings\carson\desktop

C:\Documents and Settings\carson\Desktop>dailysetup
Today's date is 20090227
Yesterday's date was 2009/209
C:\Documents and Settings\carson\Desktop>
IP logged
Dusty
Egghead



Thanked: 72
Posts: 3,387

Experience: Beginner
OS: Windows XP


I could if she would, but she won't so I don't.

« Reply #13 on: February 27, 2009, 09:18:00 AM »

Peculiar, what I get is
Quote
Todays date is 20090228
Yesterday's date was 20090227
C:\>

Please check that the format of the Set %yesterdate% command in your saved version is:
Quote
set yesterdate=%yesterdate:~-4%%yesterdate:~4,2%%yesterdate:~7,2%

Also please confirm that the output of your %date% is "day mm/dd/yyyy"

IP logged

One good deed is worth more than a year of good intentions.
carson
Topic Starter
Greenhorn



Posts: 9


« Reply #14 on: February 27, 2009, 10:07:40 AM »

Peculiar, what I get is
Quote
Todays date is 20090228
Yesterday's date was 20090227
C:\>

Please check that the format of the Set %yesterdate% command in your saved version is:
Quote
set yesterdate=%yesterdate:~-4%%yesterdate:~4,2%%yesterdate:~7,2%

Yes.  I copy-and-pasted in the first place, but I just doublechecked to be sure.

Also please confirm that the output of your %date% is "day mm/dd/yyyy"

Yes.  Running "date /t" today outputs "Fri 02/27/2009".
IP logged
Pages: [1] 2 3  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Create a directory with today's date and delete one with yesterday's « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.191 seconds with 20 queries.