Simple windows batch file that will copy a file to a folder, and create a sub-folder with timestamp

18
2014-05
  • codecompleting

    I have a file at:

    c:\source\hello.txt
    

    That I want to backup as I modify it often, to here:

    c:\backups\source\????\hello.txt
    

    How can I create such a .bat file to do this?

    I want the script to somehow create a sub-folder with the current date and time, so that when I view the folders it will be ordered so I can get the latest or go back in time easily.

  • Answers
  • yuk

    Getting a main idea from here:

    @echo off
    set hh=%time:~-11,2%
    set /a hh=%hh%+100
    set hh=%hh:~1%
    set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%
    
    if not exist "c:\backups\source\%dateseed%" mkdir "c:\backups\source\%dateseed%"
    cd "c:\backups\source\%dateseed%"
    copy "c:\source\hello.txt" .
    

    It will create a folder named yyyymmdd_hhmmss (hh in 24-hours format).

    You can also check copy /? for further options.

    If you're working with files or folders other than on C: make sure you add a line to change to that drive before attempting to make a folder or change dir into one.


  • Related Question

    windows - Creating a folder from first 5 characters of filename in a batch file
  • billieh

    Here's what I have:

    C:\test\12345-test1.txt
    C:\test\23456-test2.txt
    C:\test\44444-test3.doc
    

    I would like to have the script read the first 5 digits from the file, create a folder with those digits, and include the letter T before it.

    The result after running should be like this:

    C:\test\T12345\12345-test1.txt
    C:\test\T23456\23456-test2.txt
    C:\test\T44444\44444-test3.doc
    

    Currently, I have this, which does the function; I just can't figure out where to put the SET command to extract the 5 characters.

    @echo off
    for %%a in (*.*) do (
    md "T%%a" 2>nul
    move "%%a" "T%%~a"  
    )
    

    I think this needs to be added to choose only the first 5 characters:

    set first5=%%a:~5,1%
    

    Thoughts?


  • Related Answers
  • pixnion
    @echo off
    
    for /f %%a in ('dir /a-d /b') do (
      if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
    )
    
    goto :EOF
    
    :func
    set file=%~1
    set dir=%file:~0,5%
    md "T%dir%" 2>nul
    move "%file%" "T%dir%" 
    goto :EOF
    

    This will process files (= no dirs) in the working directory. I also added a check to prevent the script from moving the batch file itself in case it happens to be located in the wd.

  • Jens Erat

    You can use this to get the first five characters:

    set first5=%a:~0,5%
    

    set assigns the value after the = to first5. %a% would return the value of variable a, by adding the following characters you reference the range starting at character 0 and the next five ones.

  • Jay Bazuzi

    As long as you're going through the trouble to learn something, why not learn something worthwhile? In PowerShell:

    $x = gci ; $x | % { $d = 'T' + $_.Name.Substring(0,5) ; mkdir $d ; move $_ $d } | Out-Null