How to create a script in a DOS batch file to do a loop?

Discussion in 'Scripting' started by Jon Osborn, May 23, 2008.

  1. Jon Osborn

    Jon Osborn Guest

    We are copy some files with a batch on a win2k3 server, but some files can't
    be copied over becuase they are in use for about 3 minutes. When it is not
    in use it can be copied over. So I have created a batch that looks like
    this:

    copy /y c:\folder1\file1.avi c:\folder2\file1.avi
    copy /y c:\folder1\file2.avi c:\folder2\file2.avi
    sleep 60
    copy /y c:\folder1\file1.avi c:\folder2\file1.avi
    copy /y c:\folder1\file2.avi c:\folder2\file2.avi
    sleep 60
    copy /y c:\folder1\file1.avi c:\folder2\file1.avi
    copy /y c:\folder1\file2.avi c:\folder2\file2.avi
    sleep 60
    copy /y c:\folder1\file1.avi c:\folder2\file1.avi
    copy /y c:\folder1\file2.avi c:\folder2\file2.avi
    sleep 60

    Sleep is a program for the resket that cause the program to pause for 60
    seconds. There are more than two files but I figured this would be enough
    for the excample. Since each file is 3 minutes long I know the file gets
    over written within the 4 atemps. I know DOS has a FOR loop but I read
    though the help and could not fivure it out. I know this is proably simple
    but I am just not getting it.

    Thanks,
    Jon
     
    Jon Osborn, May 23, 2008
    #1
    1. Advertisements

  2. Try this:
    @echo off
    for /L %%a in (1,1,10) do (
    xcopy /d /y /c c:\folder1\file1.avi c:\folder2\
    xcopy /d /y /c c:\folder1\file2.avi c:\folder2\
    ping localhost -n 60 > nul
    )

    This version will attempt the copy process ten times, with
    a pause of about one minute in between. Files that are
    already up-to-date won't be copied a second time. Locked
    files won't cause the batch file to hang.
     
    Pegasus \(MVP\), May 23, 2008
    #2
    1. Advertisements

  3. Jon Osborn

    Jon Osborn Guest

    thanks

    Jon
     
    Jon Osborn, May 27, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.