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

Author Topic: how to get next saturday 'Date' in DOS batch file  (Read 26845 times)

0 Members and 1 Guest are viewing this topic.

Zensar

    Topic Starter


    Greenhorn

    how to get next saturday 'Date' in DOS batch file
    « on: July 29, 2010, 06:48:54 AM »
    I have a dos batch file which processes a text file and generates a .csv output file. I want to append the next Saturday date to the output file name.
    Right now I am able to append the current date by using the below mentioned logic


    Code:

    set Pdate=%date:~10,4%%date:~4,2%%date:~7,2% {setting current date to variable}
    -outputFile %OUTDIR%\AglItemMaster_"%PDATE%".csv



    now I want to pass next saturday date in place of current date.
    Kindly help.
    A quick response will be really helpful.

    southpaw63119



      Beginner
    • Thanked: 3
      • Yes
      • Yes
    • Experience: Experienced
    • OS: Linux variant
    Re: how to get next saturday 'Date' in DOS batch file
    « Reply #1 on: July 29, 2010, 10:49:19 AM »
    This is why we can't have nice things.

    Salmon Trout

    • Guest
    Re: how to get next saturday 'Date' in DOS batch file
    « Reply #2 on: July 29, 2010, 11:39:41 AM »
    I am (or was) Dias de verano.

    Output should be next Saturday's date in your local date format. Mine is European dd/mm/yyyy.

    Code: [Select]
    @echo off
    >NextSat.vbs echo wscript.echo FormatDateTime(Date + (7 - Weekday(Date)))
    For /f "delims=" %%D in ('cscript //nologo NextSat.vbs') do set nextsaturday=%%D
    del NextSat.vbs
    echo Next Saturday Is %Nextsaturday%

    Code: [Select]
    Next Saturday Is 31/07/2010



    Zensar

      Topic Starter


      Greenhorn

      Re: how to get next saturday 'Date' in DOS batch file
      « Reply #3 on: July 29, 2010, 02:28:45 PM »
      Thanks a ton Salmon for resolving my issue.
      Just a quick one 'how do I convert the output date format from dd/mm/yyyy to yyyymmdd'

      Thanks in advance

      Salmon Trout

      • Guest
      Re: how to get next saturday 'Date' in DOS batch file
      « Reply #4 on: July 29, 2010, 02:51:45 PM »
      convert the output date format from dd/mm/yyyy to yyyymmdd'

      assuming %NextSaturday% is dd/mm/yyyy

      Code: [Select]
      set yyyy=%NextSaturday:~6,4%
      set mm=%NextSaturday:~3,2%
      set dd=%NextSaturday:~0,2%
      set yourformat=%yyyy%%mm%%dd%
      echo %yourformat%

      Zensar

        Topic Starter


        Greenhorn

        Re: how to get next saturday 'Date' in DOS batch file
        « Reply #5 on: July 30, 2010, 12:02:48 AM »
        Hi Salmon,
        Code: [Select]
        set yyyy=%NextSaturday:~6,4%
        set mm=%NextSaturday:~3,2%
        set dd=%NextSaturday:~0,2%
        set yourformat=%yyyy%%mm%%dd%
        echo yourformat %yourformat%

        this is not working.

        Salmon Trout

        • Guest
        Re: how to get next saturday 'Date' in DOS batch file
        « Reply #6 on: July 30, 2010, 12:09:30 AM »
        Did you read this?

        Quote from: Me
        assuming %NextSaturday% is dd/mm/yyyy

        I tested it before posting. It works fine here.



        « Last Edit: July 30, 2010, 12:21:43 AM by Salmon Trout »

        Zensar

          Topic Starter


          Greenhorn

          Re: how to get next saturday 'Date' in DOS batch file
          « Reply #7 on: July 30, 2010, 02:15:24 AM »
          Hi Salmon,
          I am using your code in the below mentioned manner
          Code: [Select]
          @echo off
          >NextSat.vbs echo wscript.echo FormatDateTime(Date + (7 - Weekday(Date)))
          For /f "delims=" %%D in ('cscript //nologo NextSat.vbs') do set nextsaturday=%%D
          del NextSat.vbs

          set yyyy=%NextSaturday:~10,4%
          set mm=%NextSaturday:~4,2%
          set dd=%NextSaturday:~7,2%
          set yourformat=%yyyy%%mm%%dd%

          echo Next Saturday Is %Nextsaturday%
          echo yourformat %yourformat%


          And getting the output as

          Code: [Select]
          Next Saturday Is 7/31/2010
          yourformat /210

          Please help.
          Thanks in advance

          Salmon Trout

          • Guest
          Re: how to get next saturday 'Date' in DOS batch file
          « Reply #8 on: July 30, 2010, 03:11:01 AM »
          you wrongly gave your date format as dd/mm/yyyy when it appears to actually be m/d/yyyy, which is why the code I supplied does not give the result you require.

          Zensar

            Topic Starter


            Greenhorn

            Re: how to get next saturday 'Date' in DOS batch file
            « Reply #9 on: July 30, 2010, 03:28:08 AM »
            ohhhhhhh yes I overlooked the date format.
            do you know a workaround for converting 'm/d/yyyy' to 'yyyymmdd'

            Thanks in advance.

            Salmon Trout

            • Guest
            Re: how to get next saturday 'Date' in DOS batch file
            « Reply #10 on: July 30, 2010, 10:58:39 AM »
            This will place into a batch variable the following Saturday's date in yyyymmdd format. That is, 4 figures for the year and 2 figures each for month and day, (example 20100731) whatever the local date format.

            Code: [Select]
            @echo off
            >NextSat.vbs echo NextSaturday=FormatDateTime(Date+(7-Weekday(Date)))
            >>NextSat.vbs echo SY=Year(NextSaturday)
            >>NextSat.vbs echo SM=Month(NextSaturday)
            >>NextSat.vbs echo SD=Day(NextSaturday)
            >>NextSat.vbs echo wscript.echo SY ^& "," ^& SM ^& "," ^& SD
            For /f "tokens=1-3 delims=," %%A in ('cscript //nologo NextSat.vbs') do (
            Set yyyy=%%A
            Set mm=%%B
            Set dd=%%C
            )
            del Nextsat.vbs
            If %mm% lss 10 Set mm=0%mm%
            If %dd% lss 10 Set dd=0%dd%
            Set Result=%yyyy%%mm%%dd%
            echo Next Saturday is %Result%
            pause

            Code: [Select]
            Next Saturday is 20100731



            « Last Edit: July 30, 2010, 11:23:22 AM by Salmon Trout »

            Salmon Trout

            • Guest
            Re: how to get next saturday 'Date' in DOS batch file
            « Reply #11 on: July 31, 2010, 02:02:43 AM »
            The previous code had a possible problem, namely that if today is Saturday, it gives today's date. If you do not want that to happen, the following avoids that, and can be adjusted for any day of the week.

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            >evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))
            set VBcmd=cscript //nologo evaluate.vbs
            set NDW=Saturday
            For /L %%A in (1,1,7) do for /f "delims=" %%N in ('%VBcmd% "weekdayname(weekday(date+%%A))"') do if "%%N"=="%NDW%" set Offset=%%A
            for /f "delims=" %%Y in (' %VBcmd% "year(date+%Offset%)" ') do set yyyy=%%Y
            for /f "delims=" %%M in (' %VBcmd% "month(date+%Offset%)" ') do set mm=%%M
            for /f "delims=" %%D in (' %VBcmd% "day(date+%Offset%)" ') do set dd=%%D
            if %mm% LSS 10 set mm=0%mm%
            if %dd% LSS 10 set dd=0%dd%
            del evaluate.vbs
            echo Next %NDW% is %yyyy%%mm%%dd%
            pause



            Zensar

              Topic Starter


              Greenhorn

              Re: how to get next saturday 'Date' in DOS batch file
              « Reply #12 on: August 13, 2010, 01:50:55 AM »
              Thanks a lot  Salmon!!!
              You ROCK!!!!!! (|
              Issue resolved.

              vishuvishal



                Beginner
              • Thanked: 3
                Re: how to get next saturday 'Date' in DOS batch file
                « Reply #13 on: August 13, 2010, 05:25:31 PM »
                Code: [Select]
                @echo off
                >NextSat.vbs echo wscript.echo FormatDateTime(Date + (7 - Weekday(Date)))
                For /f "delims=" %%D in ('cscript //nologo NextSat.vbs') do set nextsaturday=%%D
                echo Next Saturday Is %Nextsaturday%

                set yyyy=%NextSaturday:~5,4%
                set mm=%NextSaturday:~2,2%
                set dd=%NextSaturday:~0,1%
                set yourformat=%yyyy%/%mm%/%dd%
                echo %yourformat%


                Hey I got correction.

                Thanks and regards
                vishu

                vishuvishal



                  Beginner
                • Thanked: 3
                  Re: how to get next saturday 'Date' in DOS batch file
                  « Reply #14 on: August 13, 2010, 05:32:03 PM »
                  Hey Salmon.
                  You are getting help from VBscript or windows script.

                  Is it fair?