Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Create a directory with today's date and delete one with yesterday's  (Read 23571 times)

0 Members and 1 Guest are viewing this topic.

carson

    Topic Starter


    Greenhorn

    Re: Create a directory with today's date and delete one with yesterday's
    « Reply #15 on: February 27, 2009, 10:12:27 AM »
    And I should also say: "echo %date%" returns the same as "date /t"

    Dias de verano

    • Guest
    Re: Create a directory with today's date and delete one with yesterday's
    « Reply #16 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.






    « Last Edit: February 27, 2009, 10:38:06 AM by Dias de verano »

    Dusty



      Egghead

    • I could if she would, but she won't so I don't.
    • Thanked: 75
    • Experience: Beginner
    • OS: Windows XP
    Re: Create a directory with today's date and delete one with yesterday's
    « Reply #17 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
    One good deed is worth more than a year of good intentions.

    carson

      Topic Starter


      Greenhorn

      Re: Create a directory with today's date and delete one with yesterday's
      « Reply #18 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!

      carson

        Topic Starter


        Greenhorn

        Re: Create a directory with today's date and delete one with yesterday's
        « Reply #19 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>

        macdad-



          Expert

          Thanked: 40
          Re: Create a directory with today's date and delete one with yesterday's
          « Reply #20 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%
          « Last Edit: February 27, 2009, 11:50:29 AM by macdad- »
          If you dont know DOS, you dont know Windows...

          Thats why Bill Gates created the Windows NT Family.

          Dias de verano

          • Guest
          Re: Create a directory with today's date and delete one with yesterday's
          « Reply #21 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:/=%

          macdad-



            Expert

            Thanked: 40
            Re: Create a directory with today's date and delete one with yesterday's
            « Reply #22 on: February 27, 2009, 05:51:03 PM »
            got me on there...
            If you dont know DOS, you dont know Windows...

            Thats why Bill Gates created the Windows NT Family.

            DasTwitcH



              Starter

              Re: Create a directory with today's date and delete one with yesterday's
              « Reply #23 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...

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: Create a directory with today's date and delete one with yesterday's
                « Reply #24 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.

                DasTwitcH



                  Starter

                  Re: Create a directory with today's date and delete one with yesterday's
                  « Reply #25 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)

                  gh0std0g74



                    Apprentice

                    Thanked: 37
                    Re: Create a directory with today's date and delete one with yesterday's
                    « Reply #26 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?

                    DasTwitcH



                      Starter

                      Re: Create a directory with today's date and delete one with yesterday's
                      « Reply #27 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

                      gh0std0g74



                        Apprentice

                        Thanked: 37
                        Re: Create a directory with today's date and delete one with yesterday's
                        « Reply #28 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.

                        carson

                          Topic Starter


                          Greenhorn

                          Re: Create a directory with today's date and delete one with yesterday's
                          « Reply #29 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.