Computer Hope

Microsoft => Microsoft DOS => Topic started by: carson on February 26, 2009, 09:11:35 AM

Title: Create a directory with today's date and delete one with yesterday's
Post by: carson 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 (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?
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: macdad- 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-)
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dias de verano 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.


Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: macdad- 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
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dias de verano 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

Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: macdad- 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.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: GuruGary 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"
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dusty 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
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dias de verano 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.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: macdad- 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.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson 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.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson 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.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson 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>
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dusty 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"

Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson 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".
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson on February 27, 2009, 10:12:27 AM
And I should also say: "echo %date%" returns the same as "date /t"
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dias de verano on February 27, 2009, 10:25:16 AM
Set yourself free from regional settings. Do it all in VBS with Evaluate.vbs (which I wrote!)  ;)

Code: [Select]
@echo off
echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "year(date-1)"') do set ydYear=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "month(date-1)"') do set ydMonth=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "day(date-1)"') do set ydDay=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "year(date)"') do set tdYear=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "month(date)"') do set tdMonth=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "day(date)"') do set tdDay=%%A
del evaluate.vbs
if %ydMonth% LEQ 9 set ydMonth=0%ydMonth%
if %tdmonth% LEQ 9 set tdmonth=0%tdmonth%
if %ydDay% LEQ 9 set ydDay=0%ydDay%
if %tdDay% LEQ 9 set tdDay=0%tdDay%
set ydate=%ydYear%%ydMonth%%ydDay%
set tdate=%tdYear%%tdMonth%%tdDay%
echo Yesterday was %ydate%
echo Today is      %tdate%

Code: [Select]
Yesterday was 20090226
Today is      20090227

Should work whatever the regional settings.

As I outlined in the other thread, you can evaluate all kinds of things - dates in the past & future, math expressions, trig functions, lots of stuff.






Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dusty on February 27, 2009, 10:37:39 AM
Dias has nailed it again but please run the following and post the outcome.

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
)
echo %yesterdate:~-4%
echo %yesterdate:~4,2%
echo %yesterdate:~7,2%
echo.

set yester=%yesterdate:~-4%%yesterdate:~4,2%%yesterdate:~7,2%
del yesterday.vbs

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


Thanks
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson on February 27, 2009, 10:51:14 AM
Set yourself free from regional settings. Do it all in VBS with Evaluate.vbs (which I wrote!)  ;)

This solution works perfectly.  Thank you!
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson on February 27, 2009, 10:53:33 AM
Dias has nailed it again but please run the following and post the outcome.

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
)
echo %yesterdate:~-4%
echo %yesterdate:~4,2%
echo %yesterdate:~7,2%
echo.

set yester=%yesterdate:~-4%%yesterdate:~4,2%%yesterdate:~7,2%
del yesterday.vbs

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


Thanks

Running the above code gives me:

Code: [Select]
2009
/2
09

Todays date is 20090227
Yesterday's date was 2009/209

C:\Documents and Settings\carson\Desktop>
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: macdad- on February 27, 2009, 11:16:23 AM
thats why i did it just for the day.
since when you do the entire mkdir "%DATE%", you get seperate sub folders from the slashes in %DATE%
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: Dias de verano on February 27, 2009, 12:19:36 PM
thats why i did it just for the day.
since when you do the entire mkdir "%DATE%", you get seperate sub folders from the slashes in %DATE%

Not with %date:/=%
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: macdad- on February 27, 2009, 05:51:03 PM
got me on there...
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: DasTwitcH on May 31, 2009, 08:01:37 PM
It's amazing how complicated some people seemed to make this solution!

FOR /F "tokens=2,3,4 delims=/ " in ("%date%") do call :METHOD %%a %%b %%c

set tdate=%3%2%1
set/a ydate=%tdate% - 1

echo %tdate%
echo %ydate%

returns:
20090531
20090530

----------------

Only Just noticed though that if you're at the beginning of the month, it'll return a 00 for the day...Little bit of error processing should sort that though...
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: gh0std0g74 on May 31, 2009, 08:12:23 PM
It's amazing how complicated some people seemed to make this solution!

FOR /F "tokens=2,3,4 delims=/ " in ("%date%") do call :METHOD %%a %%b %%c

set tdate=%3%2%1
set/a ydate=%tdate% - 1

echo %tdate%
echo %ydate%

returns:
20090531
20090530

----------------

Only Just noticed though that if you're at the beginning of the month, it'll return a 00 for the day...Little bit of error processing should sort that though...
why post something that does not really work. for example, if today is 20090501, if you do what your code does, what will be the value? take note you also need to decrement the month.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: DasTwitcH on May 31, 2009, 08:23:28 PM
@echo off
FOR /F "tokens=2,3,4 delims=/ " %%a in ("%date%") do call :METHOD %%a %%b %%c

echo %tdate%
echo %ydate%

:METHOD
set tdate=%3%2%1
set /a ydate=%tdate% - 1

if %ydate:~-2%==00 (
   if %ydate:~-4,2%==05 or %ydate:~-4,2%==07 or %ydate:~-4,2%==10 or %ydate:~-4,2%==12 (
      set /a ydate=%ydate% - 70
   ) else (
      set /a ydate=%ydate% - 69
   )
)
GOTO :EOF

happy?

(edited to correct the "last month has 30 days" processing)
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: gh0std0g74 on May 31, 2009, 08:31:44 PM
happy?
no, i am not happy.
Code: [Select]
C:\test>echo %date%
Mon 06/01/2009

C:\test>test.bat
20090106
20090036

first, can you amend your code to cater to different date settings
second, what about Feb with 28 days?
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: DasTwitcH on May 31, 2009, 08:35:49 PM
how complex do you want this to be???

He asked just for some hack code to give him yesterdays date.  Feb is easy.  Add one more conditional to the if statement.  I assume a little intelligence on the part of the person on the other end here, considering he's obviously not a scripting newbie.

You can modify that code easily to make it as complex or simple as you want.

As for different date formats?  Rearrange the tokens and args passed to :METHOD, and you're done.

I could abstract it out as much as I wanted, but it gets to the point where you may as well write a mini app.  Batches are great for speed, compatibility and simplicity.  Once you're trying to do rocket surgery, use a proper scripting language and environment.  *shrug*

T
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: gh0std0g74 on May 31, 2009, 08:46:54 PM
how complex do you want this to be???
your first post says:
Quote
It's amazing how complicated some people seemed to make this solution!
obviously now its getting complicated because you have all these checks to take care of.  that's why my question, why post something that doesn't really work , when already people like Dias and the rest already solved the problem with vbscript?

Quote
I could abstract it out as much as I wanted, but it gets to the point where you may as well write a mini app. 
that's why there are also suggestions like yesterday.vbs or evaluate.vbs....

Quote
Batches are great for speed, compatibility and simplicity.  Once you're trying to do rocket surgery, use a proper scripting language and environment.  *shrug*
this one i do agree with you.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: carson on June 01, 2009, 06:43:35 AM
Wow, I posted the original question what seems like forever ago.  My eventual solution has been working so well since then, I literally haven't thought about it (well, since I scheduled it to run at 7:40 every weekday morning).

how complex do you want this to be???

 ...

I could abstract it out as much as I wanted, but it gets to the point where you may as well write a mini app.  Batches are great for speed, compatibility and simplicity.  Once you're trying to do rocket surgery, use a proper scripting language and environment.  *shrug*


For the record, here is exactly how complex I want it:

Code: [Select]
@echo off
cd %userprofile%\Desktop
echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs

REM Set variables for three days ago, since it might be Monday.
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "year(date-3)"') do set FriYear=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "month(date-3)"') do set FriMonth=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "day(date-3)"') do set FriDay=%%A

REM Set variables for yesterday
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "year(date-1)"') do set ydYear=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "month(date-1)"') do set ydMonth=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "day(date-1)"') do set ydDay=%%A

REM Set variables for today
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "year(date)"') do set tdYear=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "month(date)"') do set tdMonth=%%A
for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "day(date)"') do set tdDay=%%A

REM Clean this up now that we're done with it.
del evaluate.vbs

REM Pad the months and days with a leading 0 if necessary.
if %FriMonth% LEQ 9 set FriMonth=0%FriMonth%
if %FriDay% LEQ 9 set FriDay=0%FriDay%
if %ydMonth% LEQ 9 set ydMonth=0%ydMonth%
if %tdmonth% LEQ 9 set tdmonth=0%tdmonth%
if %ydDay% LEQ 9 set ydDay=0%ydDay%
if %tdDay% LEQ 9 set tdDay=0%tdDay%


REM These aren't actually necessary; they just make things easier to read.
REM We may do something with them later if/when this script does more than just create/delete
REM directories.
set FriDate=%FriYear%%FriMonth%%FriDay%
set ydate=%ydYear%%ydMonth%%ydDay%
set tdate=%tdYear%%tdMonth%%tdDay%

REM Remove Friday's directory if it exists.
if exist %FriDate% rmdir /s /q %FriDate%

REM Remove yesterday's directory if it exists.
if exist %ydate% rmdir /s /q %ydate%

REM Create today's directory if it doesn't already exist.
if not exist %tdate% mkdir %tdate%

That there's probably a better way to do this, I don't doubt.  But it has the virtues of a) simplicity and b) working.  I could probably make it run slightly faster by  removing the REM statements, but six months from now when I finally get around to automating more tasks (like automagically downloading the stuff that's supposed to be in the created folder) I'll be glad for them.  So for now I'll leave it be.
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: ALAN_BR on June 01, 2009, 07:04:46 AM
An infinitely simpler system would be to NOT delete a folder with a name matching Yesterdays date.
Instead delete ALL folders whose name does not match today's date.

Or even simpler, delete all folders that were not created/modified today.

That not only gets rid of yesterday's folder, but on Monday it will get rid of Friday's folder (assuming a Monday to Friday working week).

The only requirement is that these folders must be sub-folders within one one common parent folder (and this parent never deleted).

Regards
Alan
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: ronchong on November 30, 2011, 07:09:43 PM
SET dwMONTH=%DATE:~4,2%
SET /A dwDAY=%DATE:~7,2%
SET dwYEAR=%DATE:~10,4%
SET dwDate=%dwMONTH%/%dwDAY%/%dwYEAR%
for /F "tokens=1" %%a IN (YesterdayDate.txt) DO set yest=%%a
@echo %yest%
forfiles /p "." /C "cmd /c if @fdate==%yest% rd /q /s @fname" > "DeleteDirectoryLog%DATE:~4,2%_%DATE:~7,2%_%DATE:~10,4%.txt"
@echo %dwDATE% > "YesterdayDate.txt"
Title: Re: Create a directory with today's date and delete one with yesterday's
Post by: ALAN_BR on December 01, 2011, 01:28:14 AM
After almost 3 years I think the O.P. has now giving up waiting   :)