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

Author Topic: How to add date to folder in batch-file  (Read 14290 times)

0 Members and 1 Guest are viewing this topic.

jussi

    Topic Starter


    Starter

    How to add date to folder in batch-file
    « on: January 08, 2010, 12:41:08 PM »
    I need a little bit help with batch-files: how can I rename a folder in a bat-file so that it contains a current date - such as folder1_20090108 (or any other date string format)? Without the need of typing the date myself, of course :)

    I'm planning to create a simple batch file that would make a backup copy of one certain folder that contains my school project, and need that date to identify the version.

    Cheers

    Jussi

    Dusty



      Egghead

    • I could if she would, but she won't so I don't.
    • Thanked: 75
    • Experience: Beginner
    • OS: Windows XP
    Re: How to add date to folder in batch-file
    « Reply #1 on: January 09, 2010, 02:30:47 AM »
    It's always difficult when we do not know the format of your date which you didn't advise.  Here is a rename if your date format is 'day mm/dd/yyyy' - not tested.

    Ren folder1 folder1_%date:~-4%%date:~4,2%%date:~7,2%

    Renamed folder should be in the format folder1_yyyymmdd

    You can substitute Copy for Ren when you copy the folder, no sense in copying then renaming.

    Hope this helps.
    One good deed is worth more than a year of good intentions.

    jussi

      Topic Starter


      Starter

      Re: How to add date to folder in batch-file
      « Reply #2 on: January 09, 2010, 05:28:21 AM »
      It's always difficult when we do not know the format of your date which you didn't advise.  Here is a rename if your date format is 'day mm/dd/yyyy' - not tested.

      Ren folder1 folder1_%date:~-4%%date:~4,2%%date:~7,2%

      Renamed folder should be in the format folder1_yyyymmdd

      You can substitute Copy for Ren when you copy the folder, no sense in copying then renaming.

      Hope this helps.

      Thanks for the code. My mistake, I didn't explain the order of numbers at example in my post - I did indeed mean format yyyymmdd. :)

      -Jussi

      Salmon Trout

      • Guest
      Re: How to add date to folder in batch-file
      « Reply #3 on: January 09, 2010, 06:18:46 AM »
      Dusty means, you need to tell us the local date format you are using. You can see it using the date /t command. For example my local settings make the date look like this

      Code: [Select]
      C:\>date /t
      09/01/2010

      Or use a vbs / batch hybrid which is locale - independent

      Code: [Select]
      @echo off
      Echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs
      For /f "delims=" %%Y in ( ' cscript //nologo evaluate.vbs "year(date)" ' ) do set yyyy=%%Y
      For /f "delims=" %%M in ( ' cscript //nologo evaluate.vbs "month(date)" ' ) do set mm=%%M
      For /f "delims=" %%D in ( ' cscript //nologo evaluate.vbs "day(date)" ' ) do set dd=%%D
      del evaluate.vbs
      if %mm% LSS 10 set mm=0%mm%
      if %dd% LSS 10 set dd=0%dd%
      set datestamp=%yyyy%%mm%%dd%
      echo Datestamp is %datestamp%

      Code: [Select]
      Datestamp is 20100109
      « Last Edit: January 09, 2010, 06:31:22 AM by Salmon Trout »