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

Author Topic: Batch File/Script Help needed!!!  (Read 6910 times)

0 Members and 1 Guest are viewing this topic.

Fox43Guy

  • Guest
Batch File/Script Help needed!!!
« on: May 14, 2009, 10:09:39 AM »
To sum it up, I need assistance or ideas of how to write a batch or script that each morning at 8am copies the previous day’s log file to a network drive and renames the file  (for example) 513ADTCH.VER to 0513ADTCH.VER…

Here is an exact example of the problem I’m running into…

Our business begins and ends each day’s record at 5am.

A file is named for the 24 hour period that it represents such as “513ADTCH.VER” for the time of 5am on May 13th to 5am on May 14th…

At 5am each day a new file is created for the next 24 hour period…

I am trying to copy the previous day’s file to a network share and rename the file with the month represented as a two digit number…such as “0513ADTCH.VER”

I run into difficulty when trying to write a batch file that copies the file to the network drive, as the file name changes each day and the currently running log cannot be altered until the completion of its own 24 hour period….

To sum it up, I need assistance or ideas of how to write a batch or script that each morning at 8am copies the previous day’s log file to a network drive and renames the file  (for example) 513ADTCH.VER to 0513ADTCH.VER…

Any help would be greatly appreciated!!!

BatchFileBasics



    Hopeful

    Thanked: 18
    Re: Batch File/Script Help needed!!!
    « Reply #1 on: May 19, 2009, 08:02:43 PM »
    well, just a hint. for the "scheduling" you will be using the "at" command
    ex: at 16:30(which is 4:30 pm) [command]
    on the command prompt, type "at /?"

    for the renaming, to get the current month date(in 2 digits)
    Code: [Select]
    %date:~-10,2%

    the -10 signifies the beggining of where to start
    the date is in a 10 digit format xx/xx/xxxx
    slashes count as a digit too.
    and the end is 10
    so the -10 will start at the beggining and the 2 is how many digits to count
    so it will return as 05
    Code: [Select]
    %date:~-9,1% = 5
    %date:~-7,2% = the current date in days(19)
    so if you combine %date:~-10,2% and %date:~-7,2% (%date:~-10,2%%date:~-7,2%)
    youll get 0519
    so when you rename.
    Code: [Select]
    ren (thedatebefore)filename %date:~-10,2%%date:~-7,2%filename
    to rename the day before to the new date youll have to set a variable and minus a day from it
    Code: [Select]

    set daycurrent=%date:~-7,2%
    set /a daybefore=%days%-1
    rem so now, daybefore is the current day - minus one - was 19 now its 18
    if %daybefore% LSS 1 (
    set monthcurrent= %date:~-10,2%
    set /a monthbefore=%monthcurrent% - 1
    set yesdate=%monthcurrent%%daycurrent% )
    set yesdate=%monthcurrent%%daycurrent%
    rem since we have YESTERDAYS date, we can rename it
    ren %yesdate%(the rest of your files name) %date:~-10,2%%date:~-7,2%(the rest of your files name)
    rem it will take 0518filename.txt and rename it to 0519filename.txt

    So this is basicly what it takes to rename a timestamp
    When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Batch File/Script Help needed!!!
    « Reply #2 on: May 19, 2009, 08:07:52 PM »
    well, just a hint. for the "scheduling" you will be using the "at" command
    ex: at 16:30(which is 4:30 pm) [command]
    on the command prompt, type "at /?"

    for the renaming, to get the current month date(in 2 digits)
    Code: [Select]
    %date:~-10,2%

    the -10 signifies the beggining of where to start
    the date is in a 10 digit format xx/xx/xxxx
    slashes count as a digit too.
    and the end is 10
    so the -10 will start at the beggining and the 2 is how many digits to count
    so it will return as 05
    Code: [Select]
    %date:~-9,1% = 5
    %date:~-7,2% = the current date in days(19)
    so if you combine %date:~-10,2% and %date:~-7,2% (%date:~-10,2%%date:~-7,2%)
    youll get 0519
    so when you rename.
    Code: [Select]
    ren (thedatebefore)filename %date:~-10,2%%date:~-7,2%filename
    to rename the day before to the new date youll have to set a variable and minus a day from it
    Code: [Select]

    set daycurrent=%date:~-7,2%
    set /a daybefore=%days%-1
    rem so now, daybefore is the current day - minus one - was 19 now its 18
    if %daybefore% LSS 1 (
    set monthcurrent= %date:~-10,2%
    set /a monthbefore=%monthcurrent% - 1
    set yesdate=%monthcurrent%%daycurrent% )
    set yesdate=%monthcurrent%%daycurrent%
    rem since we have YESTERDAYS date, we can rename it
    ren %yesdate%(the rest of your files name) %date:~-10,2%%date:~-7,2%(the rest of your files name)
    rem it will take 0518filename.txt and rename it to 0519filename.txt

    So this is basicly what it takes to rename a timestamp
    Or you can use a super complicated FOR loop! (And neither of these solutions will work if the date format is different.

    But let's try this to see your computer date format.

    @echo off
    for /f "tokens=1-3 delims=/ " %%A in ("%date%") do (
    echo %%A
    echo %%B
    echo %%C
    pause
    )
    What does it say?
    « Last Edit: May 19, 2009, 08:26:46 PM by Helpmeh »
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    BatchFileBasics



      Hopeful

      Thanked: 18
      Re: Batch File/Script Help needed!!!
      « Reply #3 on: May 19, 2009, 08:18:58 PM »
      Code: [Select]
      19
      %B
      %C
      Press any key to continue . . .
      only came out with the days

      and hey, im batchfileBASICS
      lol
      When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: Batch File/Script Help needed!!!
      « Reply #4 on: May 19, 2009, 08:26:24 PM »
      Code: [Select]
      19
      %B
      %C
      Press any key to continue . . .
      only came out with the days

      and hey, im batchfileBASICS
      lol
      Ahh sorry, replace tokens=3 with tokens=1-3
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      BatchFileBasics



        Hopeful

        Thanked: 18
        Re: Batch File/Script Help needed!!!
        « Reply #5 on: May 19, 2009, 08:31:13 PM »
        Ahh sorry, replace tokens=3 with tokens=1-3

        ohh, i now under stand this now haha
        well the output came out as
        Code: [Select]
        Tue
        05
        19
        Press any key to continue . . .

        so to fix:
        Code: [Select]
        @echo off
        for /f "tokens=2-3 delims=/ " %%A in ("%date%") do (
        echo %%A
        echo %%B
        pause
        )

        skips the whole"teus" or whatever and goes onto the second and third
        When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: Batch File/Script Help needed!!!
        « Reply #6 on: May 19, 2009, 08:38:23 PM »
        ohh, i now under stand this now haha
        well the output came out as
        Code: [Select]
        Tue
        05
        19
        Press any key to continue . . .

        so to fix:
        Code: [Select]
        @echo off
        for /f "tokens=2-3 delims=/ " %%A in ("%date%") do (
        echo %%A
        echo %%B
        pause
        )

        skips the whole"teus" or whatever and goes onto the second and third


        Try setting the tokens to 2-4 and echo %%A, %%B and %%C...that should bring in the year... Quite strange how your's is formatted...try echoing %date% and see what it says. Mine just says 19/05/2009.
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        BatchFileBasics



          Hopeful

          Thanked: 18
          Re: Batch File/Script Help needed!!!
          « Reply #7 on: May 19, 2009, 08:42:44 PM »
          oh, yea thats strange.

          but wait, are we just trying to get the date? or timestamping the month and date
          When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

          Helpmeh



            Guru

          • Roar.
          • Thanked: 123
            • Yes
            • Yes
          • Computer: Specs
          • Experience: Familiar
          • OS: Windows 8
          Re: Batch File/Script Help needed!!!
          « Reply #8 on: May 20, 2009, 05:58:16 AM »
          oh, yea thats strange.

          but wait, are we just trying to get the date? or timestamping the month and date
          Well getting the date is just the first step.
          Where's MagicSpeed?
          Quote from: 'matt'
          He's playing a game called IRL. Great graphics, *censored* gameplay.

          Reno



            Hopeful
          • Thanked: 32
            Re: Batch File/Script Help needed!!!
            « Reply #9 on: May 20, 2009, 06:16:47 AM »
            Code: [Select]

            set daycurrent=%date:~-7,2%
            set /a daybefore=%days%-1
            rem so now, daybefore is the current day - minus one - was 19 now its 18
            if %daybefore% LSS 1 (
            set monthcurrent= %date:~-10,2%
            set /a monthbefore=%monthcurrent% - 1
            set yesdate=%monthcurrent%%daycurrent% )
            set yesdate=%monthcurrent%%daycurrent%
            rem since we have YESTERDAYS date, we can rename it
            ren %yesdate%(the rest of your files name) %date:~-10,2%%date:~-7,2%(the rest of your files name)
            rem it will take 0518filename.txt and rename it to 0519filename.txt

            the above code contain logic-bug when today date is 1-mm-yyyy.
            the only way i know to do in cmd batch is using julian algorithm or using some if-else-if-else, otherwise there is an easy way to do it in vbscript.

            BatchFileBasics



              Hopeful

              Thanked: 18
              Re: Batch File/Script Help needed!!!
              « Reply #10 on: May 20, 2009, 08:51:54 AM »
              the above code contain logic-bug when today date is 1-mm-yyyy.
              the only way i know to do in cmd batch is using julian algorithm or using some if-else-if-else, otherwise there is an easy way to do it in vbscript.

              yea, that was my only problem, if the month is 1.

              but i guess vbscript would work. i am point blank though so i guess this ends my help
              When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

              Dias de verano

              • Guest
              Re: Batch File/Script Help needed!!!
              « Reply #11 on: May 20, 2009, 11:59:19 AM »
              Some points:

              1. This one keeps coming up. The position of the month digits in the date variable IS NOT FIXED. In the USA, the date is written mm/dd/yyyy so today (20th May 2009) is written 5/20/2009. In the rest of the world, the date is written in a variety of ways. The most common is dd/mm/yyyy so today would be 20/05/2009. So you cannot say with certainty where the month and day digits will be.

              2. A simple VBS script (one line!) will get you a date in the past or the future.

              Code: [Select]
              @echo off

              Rem Create VBS script
              echo Wscript.echo eval(WScript.Arguments(0))>Evaluate.vbs

              Rem Get Yesterdays Date
              for /f "delims=" %%A in ('Cscript //nologo Evaluate.vbs "year(Date-1)"') do set yyyy=%%A
              for /f "delims=" %%A in ('Cscript //nologo Evaluate.vbs "month(Date-1)"') do set mm=%%A
              for /f "delims=" %%A in ('Cscript //nologo Evaluate.vbs "day(Date-1)"') do set dd=%%A
              Del Evaluate.vbs

              Rem I presume that the log file will be named
              Rem dmADTCH.VER if both day and month are single digits?

              Rem that is, 1st January would be 11ADTCH.VER ...?

              Rem You have not made this clear.

              set nmm=%mm%
              set ndd=%dd%

              REM if month is less than 10 add a leading zero for new filename
              if %mm% LSS 10 set nmm=0%mm%

              REM if day is less than 10 add a leading zero for new filename
              if %dd% LSS 10 set ndd=0%dd%

              set logfile=%mm%%dd%ADTCH.VER

              set newfile=%nmm%%ndd%ADTCH.VER

              echo log file is %logfile%
              echo new file is %newfile%


              « Last Edit: May 21, 2009, 12:52:15 AM by Dias de verano »

              BatchFileBasics



                Hopeful

                Thanked: 18
                Re: Batch File/Script Help needed!!!
                « Reply #12 on: May 20, 2009, 12:24:28 PM »
                Some points:

                1. This one keeps coming up. The position of the month digits in the date variable IS NOT FIXED. In the USA, the date is written mm/dd/yyyy so today (20th May 2009) is written 5/20/2009. In the rest of the world, the date is written in a variety of ways. The most common is dd/mm/yyyy so today would be 20/05/2009. So you cannot say with certainty where the month and day digits will be.

                2. A simple VBS script (one line!) will get you a date in the past or the future.

                Code: [Select]
                ...
                Rem I presume that the log file will be named
                Rem dmADTCH.VER if both day and month are single digits?

                Rem that is, 1st January would be 11ADTCH.VER ...?

                Rem You have not made this clear.

                set nmm=%mm%
                set ndd=%dd%

                REM if month is less than 10 add a leading zero for new filename
                if %mm% LSS 10 set nmm=0%mm%

                REM if day is less than 10 add a leading zero new filename
                if %dd% LSS 10 set ndd=0%dd%

                set logfile=%mm%%dd%ADTCH.VER

                set newfile=%nmm%%ndd%ADTCH.VER

                echo log file is %logfile%
                echo new file is %newfile%




                i believe he said in the very beggining:
                Quote
                ...I am trying to copy the previous day’s file to a network share and rename the file with the month represented as a two digit number…such as “0513ADTCH.VER”...
                When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

                Dias de verano

                • Guest
                Re: Batch File/Script Help needed!!!
                « Reply #13 on: May 20, 2009, 12:29:00 PM »
                What is your point, BatchFileBasics? You clearly did not read my post very carefully. Or the OP's. He has two files involved - that his batch code will need to know (i.e. calculate) the numerical portions of the names of: one that already exists, his log file, and one that he will copy it to. He has not been clear about how the system names the first log file. I want to verify the number of month and day digits.
                 
                « Last Edit: May 20, 2009, 12:39:46 PM by Dias de verano »

                BatchFileBasics



                  Hopeful

                  Thanked: 18
                  Re: Batch File/Script Help needed!!!
                  « Reply #14 on: May 20, 2009, 12:38:57 PM »
                  oh, sorry, i thought you ment the whole "2 digit month" thing
                  When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.