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

Author Topic: Batch file to copy and Increment  (Read 16753 times)

0 Members and 1 Guest are viewing this topic.

Nicmos

    Topic Starter


    Starter

    Thanked: 1
    • Experience: Experienced
    • OS: Windows XP
    Batch file to copy and Increment
    « on: December 05, 2013, 02:37:05 PM »

    After wasting far too much time trying to figure this out I thought I'd just ask.

    I'm trying to write a batch file (OS is WInXP) that will copy (or use xcopy) one .txt file from 1 directory into another directory on a different drive and then increment the file name so I have several backups of the original file.

    This is simple DOS stuff but I'm stumped never having written batch files.


    Any help would be greatly appreciated.

    David....

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch file to copy and Increment
    « Reply #1 on: December 05, 2013, 07:12:10 PM »
    This will date and time stamp a file.  That's much more useful than a plain number.

    The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

    Code: [Select]
    @echo off
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

    set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

    copy "c:\folder A\file.txt" "c:\folder B\file-%fullstamp%.txt" >nul

    pause

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Batch file to copy and Increment
    « Reply #2 on: December 05, 2013, 07:17:39 PM »
    set destination=C:\Dummy\
    set Location=D:\Dummy\Test.txt
    set Filename=Test
    set a=1

    :loop
    if exist %destination%\%filename%_%a% set /a a+=1 && goto :loop
    copy %location% %destination%\%filename%_%a%.txt

    Untested
    Though what foxidrive say's is true, a date\time would be much better for you.
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    Nicmos

      Topic Starter


      Starter

      Thanked: 1
      • Experience: Experienced
      • OS: Windows XP
      Re: Batch file to copy and Increment
      « Reply #3 on: December 06, 2013, 08:05:24 AM »
      Thank you both for your efforts. I will most likely use the one that adds the date stamp but I also like the one that adds -1 to the file name.

      Is there a string I can add to that batch file where it will increment file name (filename-1.txt, filename-2.txt, -3.....etc)?

      Lemonilla



        Apprentice

      • "Too sweet"
      • Thanked: 70
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Batch file to copy and Increment
      « Reply #4 on: December 07, 2013, 07:39:23 PM »
      Not in one command, I created a loop that would increment a variable (%a%) by 1 each time it ran, then had it copy the file X times saving it with "-%a%" at the end.  This would be the way I would do it, but I'm sure there are may other ways to complete the task.
      Quote from: patio
      God Bless the DOS Helpers...
      Quote
      If it compiles, send the files.