Thoughts on life, liberty, and information technology

Using the FOR command to copy files listed in a text file

A coworker asked me for a script. Here’s the request:

… would want to copy all files on this list [an attached text document] to another location (doesn’t really matter where for now). All are currently located in \\server\share\folder. The path in the new location should begin with the part after “folder”…

In a nutshell, here is the high-level description of what the script must do:

Given a text file which provides a list of files, copy the files from a fixed source to a fixed destination, recreating the directory trees on the destination. A simple file copy won’t work because there may be files in the source folders which should not be copied.

Sample content of the text file is:

client\CD120\Samarai Legends\Drafts\drafts folder.txt
client\CD120\Samarai Legends\Inbox\Legends.doc
client\CD120\Bushido Warriors\Inbox\Warrior Code.doc

The solution to this is to make a simple batch file that parses the content of the text file, generating the appropriate xcopy command to copy the file. We’ll call the batch file xcopylist.bat; its one line of content is below. Change c:\temp\ to whatever path you want the files copied to. (I used c:\temp\ for testing.) Change \\server\share\folder to the root folder of the files to copy.

for /f "delims=" %%i in (filelist.txt) do echo D|xcopy "\\server\share\folder\%%i" "c:\temp\%%i" /i /z /y

Put the file list in the same directory as the batch file and name the file list filelist.txt. Then run the batch file and viola! You’ve got to love the for command, which lets you (among other things) parse text files and use the line-by-line output. Another trick in here is the output parser pipe, which allows us to automatically press the “D” key with each xcopy command.

Note: The batch file overwrites files in the destination automatically. To turn this off (have it prompt you), change /y to /y- in the batch file. However, if you’re using Windows NT 4.0, just delete the /y switch altogether – it’s only supported in Windows XP and Windows 2000.

146 responses to “Using the FOR command to copy files listed in a text file”

  1. Happy Guy Avatar
    Happy Guy

    Thanks Brian for this nice solution. It saved a lot of time for us 🙂

    Like

  2. Joe Avatar
    Joe

    Hello,

    I would like to implement your script to backup starred picasa photos, these are stored in a file named starlist.txt in the format:

    C:\Users\Joe\Pictures\Play\Journeys\New Years 08\IMG_1682.JPG
    C:\Users\Joe\Pictures\Play\Journeys\New Years 08\IMG_1683.JPG
    etc

    The problem being that your script uses the whole file path when duplicating the files, resulting in the following (unusable) address:

    C:\temp\C:\Users\Joe\Pictures\Play\Journeys\New Years 08\IMG_1682.JPG

    Is there a simple modification that can be made to the script which would remove the “C:\Users\Joe\Pictures” element from the created file path?

    Many thanks in advance for your assistance.

    Like

  3. brian Avatar
    brian

    The simples thing is to make a copy of starlist.txt, do a search/replace removing all text c:\users\joe\pictures, and running the script against that.

    As far as I can remember (it’s been a while), there is no easy way to truncate or replace data in strings from within the Windows shell.

    Like

  4. Joe Avatar
    Joe

    Many thanks for your fast response!

    I modified starlist.txt as you suggested, so that each line reads as follows:
    \Play\Journeys\Spain 05\Chris’s Photos\DSCN0319.JPG
    etc

    This duplicated the files successfully, but put each individual image into a folder with the image name, within the correct folder structure e.g:
    D:\Starred Photos\Play\Journeys\Spain 05\Chris’s Photos\DSCN0319.JPG\DSCN0319.JPG

    I am using the following command:
    for /f “delims=” %%i in (starlist.txt) do echo D|xcopy “C:\Users\Joe\Pictures%%i” “D:\Starred Photos\%%i” /i /z /y

    Do you have any suggestions on why the folder is being created?

    Like

    1. Brett Avatar
      Brett

      Modify the following excerpt of code:
      echo D|xcopy

      To the following:
      echo F|xcopy

      XCOPY prompts the user to either copy F, file, or D, directory. When changed from D to F, the batch script was able to successfully copy only the photos files to the directory, without placing each photo in a self named directory.

      Like

  5. brian Avatar
    brian

    Only thing I can think to try is adding a backslash after the folder “Pictures” in the path “C:\Users\Joe\Pictures%%i” (that is, make it “C:\Users\Joe\Pictures\%%i”

    Like

  6. Manuel Avatar
    Manuel

    Hi Brian, this script is very useful, i apreciate to much if you can help me to modify
    a little this script.
    I´ve a filename.txt file with the names of the files something like these

    text.txt
    text.txt
    text.txt

    etc…

    But the really I need is copy the files with your relative path to another folder

    thanks and regards

    MM

    Like

  7. brian Avatar
    brian

    Not 100% sure what you need… You mention a relative path — relative to what?

    Something you can do to help explain may be to give an example of one line in the text file, and an example of how the script would use that line. For example, if one line was this:

    test.txt

    … and you wanted to generate this:

    copy test.txt c:\files\test.txt

    … the script would be:

    for /f “delims=” %%i in (filelist.txt) do copy “%%i” “c:\files\%%i”

    Give me an illustrative example and I’ll help if I can.

    Like

  8. Manuel Avatar
    Manuel

    Hi Brian,

    Supose that I got a file “filelist.txt” with this content

    test.txt
    test1.txt
    test2.txt

    ….etc

    but these files (test.txt, test1.txt, etc…) are in a differents folders, something like that

    c:\Temp\test\test.txt
    c:\Temp\test1\test1.txt
    c:\Temp\test2\test2.txt

    I need copy all of the contents in the “Temp” folder, to another folder “Final”, but including the same folder structure of the “Temp” folder, see below

    c:\Final\ \this folder are empty at first

    This is that i will to result after execute the batch file

    c:\Final\test\test.txt
    c:\Final\test1\test1.txt
    c:\Final\test2\test2.txt

    But I only have the filelist.txt, this file dont have the relative paths of the files

    Ps sorry for my enligish

    Thanks and regards

    MM

    Like

  9. brian Avatar
    brian

    So it sounds like you want to copy only those files whose names are in the given file list. The easiest way I can think of is by doing this:

    for /f "delims=" %%i in (filelist.txt) do xcopy "c:\Temp\%%i?" "c:\Final" /i /z /y /s

    The two key changes are the question mark at the end of the first path in the xcopy command, and the /s switch.

    By adding a question mark to the end of the first path, you allow xcopy to look for “all files that match the pattern”. Of course, this means that test1.txt and test1.txtt would both match, but since it’s unlikely you have files with more than three characters in the extension, this should be fine.

    The /s switch tells xcopy to search all subdirectories of the source directory for matching files, and copy them to the destination with the same folder hierarchy.

    To test it out, run the following:

    xcopy c:\windows\*.log c:\temp /i /z /y /s

    This will copy every *.log file from the Windows directory to your c:\temp directory. This will result in operations like the following:

    C:\windows\WindowsUpdate.log
    C:\windows\Debug\blastcln.log
    C:\windows\Debug\UserMode\userenv.log
    C:\windows\Logs\DirectX.log
    C:\windows\Microsoft.NET\Framework\v2.0.50727\ngen.log

    In the temp directory, you’d have this:

    C:\temp\WindowsUpdate.log
    C:\temp\Debug\blastcln.log
    C:\temp\Debug\UserMode\userenv.log
    C:\temp\Logs\DirectX.log
    C:\temp\Microsoft.NET\Framework\v2.0.50727\ngen.log

    Viola!

    Like

  10. Manuel Avatar
    Manuel

    Hi Brian, thanks for your help. The script its working ok…I´ve an error in the
    sintaxis of the xcopy command…but now is everything OK

    Regards

    Like

  11. Manuel Avatar
    Manuel

    Hi Brian, a need your help one more time

    Supose that I got a file “filelist.txt” with this content (names without extensions)

    test
    test1
    test2
    test3
    test4

    ….etc

    I have a folder with a lot of files named like this form (test.txt, test.xls, test.doc, etc…) see example below

    c:\Temp\test.txt
    \test.xls
    \test.doc
    \test1.txt
    \test1.xls
    \test1.doc
    .
    .
    .

    etc.

    I need copy all of the contents in the “Temp” folder, to another folder “Final”, with subfolders named like
    the name of files and including the files with same name inside of the several folder see below

    c:\Final\ \this folder are empty at first or don´t exist at first

    This is that i will to result after execute the batch file

    c:\Final\test\test.txt
    \test.xls
    \test.doc

    c:\Final\test1\test1.txt
    \test1.xls
    \test1.doc

    c:\Final\test2\test2.txt
    \test2.xls
    \test3.doc

    Ps sorry for my enligish, I hope I had explained correctly

    Thanks and regards

    MM

    Like

  12. brian Avatar
    brian

    Manuel,

    I’d love to help out, but your requests are getting very specialized, and getting close to consulting services. I’ll try to point you in the right direction…

    First, you’re going to need to a combination of a FOR loop to parse the filenames in the text file. Next, you need to create the directory, then copy files to it.

    Try creating a batch file with this:

    for /f "delims=" %%i in (filelist.txt) do (
    md c:\Final\%%i
    xcopy c:\temp\%%i.* c:\Final\%%i /i /z /y /s
    )

    I didn’t test that but it looks right. 🙂

    If you need further services, you can consider hiring me as a consultant. 😀

    Like

    1. Ren Avatar
      Ren

      Hello Brian,
      If i want to move/cut the files and not copy, what command should i use.
      PS. i tried, “move” but didn’t work.

      Thanks,
      Ren

      Like

  13. Josh Avatar
    Josh

    I appear to be having the same issue as Joe.

    Before running this in my live environment, I decided to test it first.

    My bat file states:
    for /f “delims=” %%I in (test.txt) do echo D| xcopy “C:\Documents and Settings\e1000721\Desktop\New\%%I” “C:\Copy\%%I”

    My test.txt file states
    Level\test\File1.txt
    Level\test\File2.txt
    test\File3.txt
    test\File4.txt

    And it does copy over fine, only when it finishes, the file structure appears as
    C:\Copy\Level\test\File1.txt\File2.txt
    C:\Copy\Level\test\File2.txt\File2.txt
    C:\Copy\test\File3.txt\File3.txt
    C:\Copy\test\File4.txt\File4.txt

    Any thoughts on how I can get it to not make the last subfolder the name of the file?

    Thanks.

    Like

  14. brian Avatar
    brian

    Try replacing xcopy with a regular copy command — it might do the trick. (For some reason xcopy is defaulting to behavior assuming that the destination is a directory, not a file. Usually this only happens when the /I switch is present.)

    Like

  15. Josh Avatar
    Josh

    Tried copy and it just bombed out. I then put a pause at the end of the batch file so I could see exactly what it was doing. The issue with the xcopy wound up being that it was set for Directory instead of File so I changed D| to F| before the xcopy thus making the script:

    for /f “delims=” %%I in (test.txt) do echo F| xcopy “C:\Documents and Settings\e1000721\Desktop\New\%%I” “C:\Copy\%%I”

    Thanks again for the great help.

    Like

  16. Sari Avatar
    Sari

    Thanks Brian! This was exactly what I was trying to do. Years ago I did a lot with DOS batch files and now I can not remember how to do it since dragging and dropping everything.
    I modified your example a bit to create a text file listing files, which were not found.

    Like

  17. David Lim Avatar
    David Lim

    Brian,

    You are a genius! I have been trying to do this all night without success and am glad that I found this site. Thank you!!!

    Keep up the good work.

    David

    Like

  18. Joel Eade Avatar
    Joel Eade

    You’ve saved me sooo much time! “Thank You” can’t begin to explain my gratitude. You’re awesome 🙂

    Like

  19. Lester Avatar
    Lester

    Hi,

    How I have a problem, the filelist.txt that i’ve created constains different locations (server locations),
    \server1\temp1
    \server2\temp2
    \server3\temp3
    Can this be done using your batch file?

    thanks,

    Like

  20. brian Avatar
    brian

    If your filelist.txt has the full path to the files, you need to do two things:

    Remove any path prefix to the source parameter of the xcopy command.
    Remove the first slash in the UNC path in the destination parameter of the xcopy command.

    Fortunately the command shell in Windows allows you to specify a substring of characters from a variable (called string indexing, see http://www.dostips.com/DtTipsStringManipulation.php). If the variable %i was your full UNC path, and you want to drop the first character (so you don’t have a double-slash in your path), you can specify %i:~1 — the :~1 says “start from the second character” (strings are zero-indexed, so the first character is at position 0, the second at position 1, etc.).

    Your command line can then be:

    for /f “delims=” %%i in (filelist.txt) do ( set fil=%%i
    echo D|xcopy “%%i” “c:\temp\%fil:~1%” /i /z /y )

    You have to set a temporary variable (“fil” in the above), since as far as I can tell string indexing doesn’t work with the %%i for loop variable.

    Like

  21. PatrickMc Avatar
    PatrickMc

    Nice problem and solution. Can a scripting langugage such as biterscripting make the task easy ? Here is a script.

    Script CopyFiles.txt

    var str src, dest, list, file
    cat “/path/to/list file.txt” > $list
    lex “1” $list > $file
    while ($file “”)
    do
    system copy (“\””+$src+”/”+$file) (“\””+$dest+”/”+$file)
    lex “1” $list > $file
    done

    This script is in biterscripting ( http://www.biterscripting.com ). Save the script in file C:/Scripts/CopyFiles.txt. Call the script using the following command.

    script “C:/Scripts/CopyFiles.txt” src(“/path/to/source folder”) dest(“/path/to/destination folder”)

    Like

  22. Adrian Avatar
    Adrian

    Thank you very much for such a useful script, i’ve been looking for it for 2 days, but i have a problem

    in my filelist.txt i have listed a lot of files with special characters, like á é í ó ú so it keeps telling

    me file not found because it cannot read any of those characters, is there any way to fix this problem?

    i really need to get over this, i have like 10 files listing me 200 files each one, it would take me

    years to do it manually :S

    Like

  23. Gayatri Prabakar Avatar
    Gayatri Prabakar

    Thanks a lot, brian!!! 🙂

    Like

  24. Sony Avatar
    Sony

    Hi Brian,
    I need ur help very urgently.

    I have a text file which contains list of folders and files I want to copy to another location. Those folders r in different drives like C: and D:.
    My text file content gos something like this.
    C:\Folder1
    D:\Test2
    C:\mytext.txt

    Can u please give me the script that copies these folders along with their subfolders and contents to another location??

    I have tried many. One of those is :
    for /f “delims=” %i in (D:\includelist.txt) do xcopy “%i\” “U:\BACKUP\” /i /z /y /s /e

    Like

  25. brian Avatar
    brian

    First, I would specify U:\BACKUP\%i
    Second, if you are running the script from the command line, you need to double the percent signs.
    Third, if some of the source files are folders, and some are files, you need to be careful with your backslashes.
    Fourth, what error are you getting?

    Try this:
    for /f “delims=” %%i in (D:\includelist.txt) do xcopy “%%i” “U:\BACKUP\%%i” /i /z /y /s /e

    Like

    1. Sony Avatar
      Sony

      The script that u mentioned copies only subfolders. My includelist is something like this :
      D:\Sample\Test
      C:\NewFolder\Test1

      When i run the script it copies only Test and Test1 in U:\BackUp. The Entire folder structure is not created. Please help me with this.
      I tried the following : for /f “delims=” %i in (D:\includelist.txt) do echo D|xcopy “D:\%i” “U:\BACKUP\%i:~1%” /i /z /y. This works well.
      But this has D:\ in the source. I do not want that. I read the complete path in %%i.

      Like

  26. Damian Stalls Avatar
    Damian Stalls

    This is AWESOME!! But I am having a similar problem as Sony. My list.txt file contains the full source path (as there could be multiple drive sources). Is there a way to make the destination of the batch file ignore the drive letter for the destination?

    EXAMPLE OF FILELIST.TXT
    c:\users\damian.home\appdata\local\microsoft\outlook\damian@m.com.pst
    c:\users\damian.home\appdata\local\microsoft\outlook\MailStore Offline.pst

    BATCH FILE

    for /f “delims=” %%i in (filelist.txt) do echo D|xcopy “%%i” “D:\PSTs\%%i” /i /z /y

    The source works fine but the destination tries to copy to “D:\PST\C:\USERS…”. Actually if the destination could just drop the “:” that would be PERFECT!

    Like

    1. Damian Stalls Avatar
      Damian Stalls

      Nevermind… i finally figured it out. The example you provided needed some extra quotes when setting the fil variable, and the substrings needed to be updated:

      for /f “delims=” %%i in (filelist.txt) do ( set “fil=%%i”
      echo D|xcopy “%%i” “D:\DESTINATION\%fil:~0,1%%fil:~2%” /i /z /y )

      This copy the source file as follows:
      C:\Folder1\file.nam –> D:\DESTINATION\C\Folder1\file.nam
      E:\Folder3\file1.nam –> D:\DESTINATION\E\Folder3\file1.nam

      YEAH!

      Like

      1. Sony Avatar
        Sony

        Heyy Damian,

        I tried ur script. But it doesnt work. it created a folder named “~0,1%fil” in U:\Backup. It copied only 2 files from the entire folder.

        I just need to truncate the drive D:\ name from destination path. Can u please look into that?

        Like

  27. Damian Stalls Avatar
    Damian Stalls

    Spoke too soon… if you execute the batch file from a command line it works. If you double click on the batch file it does not! Any ideas?

    Like

  28. W Noel Avatar
    W Noel

    Hey Brian,

    Thanks for such amazing information!

    I want to create the scenario Manuel was asking about:

    Some files are in multiple SubDirectories and I have the file names in a text file (file1.pdf file2.pdf file3.pdf)

    I want all locations of the same file recreated when applicable.

    Ex:

    e:\SourceFolder\SubDir1\SubDir1A\file1.pdf
    e:\SourceFolder\SubDir2\SubDir2A\file2.pdf
    e:\SourceFolder\SibDir3\SubDir3A\file3.pdf
    e:\SourceFolder\SubDir4\SubDir4A\file3.pdf

    becomes

    e:\DestFolder\SubDir1\SubDir1A\file1.pdf
    e:\DestFolder\SubDir2\SubDir2A\file2.pdf
    e:\DestFolder\SibDir3\SubDir3A\file3.pdf
    e:\DestFolder\SubDir4\SubDir4A\file3.pdf

    …Where file3.pdf was found in two locations and placed in both trees.

    So far, I keep getting a directory created with the file name like follows:

    e:\DestFolder\file1.pdf\SubDir1\SubDir1A\file1.pdf
    e:\DestFolder\file2.pdf\SubDir2\SubDir2A\file2.pdf
    e:\DestFolder\file3.pdf\SibDir3\SubDir3A\file3.pdf
    e:\DestFolder\file3.pdf\SubDir4\SubDir4A\file3.pdf

    …Where fileX.pdf folders are created and file3.pdf is in its appropriate tree but both under a file3.pdf subdirectory and

    I’ve tried:

    do echo D|xcopy
    do echo F|xcopy
    do xcopy

    and I’m not getting the result I want yet.

    I hope that all makes sense : )

    Any thoughts?

    Thanks, Brian!

    Like

  29. vinny Avatar
    vinny

    set src_folder=C:\Users\Vinny\Desktop\Scorpio\Catalog 2013
    set dst_folder=C:\Users\Vinny\Desktop\Scorpio\Catalog 2013\catalogimages\
    for /f “delims=” %%i in (filelist.txt) do echo D|xcopy “C:\Users\Vinny\Desktop\Scorpio\Catalog 2013\%%i” “C:\Users\Vinny\Desktop\Scorpio\Catalog 2013\catalogimages\%%i” /i /z /y

    i am using this and its putting the images into folders inside the catalog images folder i cannot figure out why. someone had this issue further up but didnt reply with his fix.

    Like

  30. brian Avatar
    brian

    You for command looks fine (and the set command aren’t needed as you are not using the variables you are setting).

    I just tried this by doing the following:

    Batch file:
    for /f “delims=” %%i in (c:\temp\filelist.txt) do echo D|xcopy “C:\temp\%%i” “C:\temp\output\%%i” /i /z /y

    FileList.txt file:
    restoredb.bat
    restoredb.sql

    The result was the restoredb.bat and restoredb.sql files, both of which originally existed in the source folder (c:\temp), were copied to the destination folder (c:\temp\output).

    Like

  31. vinny Avatar
    vinny

    ok i dont know anything about this stuff. so i guess i have to find a completely new way to do this because this command is not working. i cant have all of them moved into there own folders and thats whats happening.

    Like

  32. vinny Avatar
    vinny

    for /f “delims=” %%i in (filelist.txt) do echo D|xcopy “C:\Users\Vinny\Desktop\Scorpio\Catalog 2013\%%i” “C:\Users\Vinny\Desktop\Scorpio\Catalog 2013\catalogimages” /i /z /y

    thats what did the trick

    Like

  33. Bal Avatar
    Bal

    Hello,
    My filelist.txt has a list of files that are seperated into sections.

    filelist.txt e.g.
    Section 1
    file1
    file2
    file3

    section 2
    file4
    file5
    file6

    Is there a way to only copy the files in section 2?

    this is the command I am using but it copies all the files in listed in filelist.txt

    for /f “delims=” %%i in (filelist.txt) do echo D|xcopy “C:\test\%%i” “C:\test2” /i /z /y
    Pls can you help?

    Like

  34. Mike Avatar
    Mike

    What if I just want to copy the files in the filelist.txt source into a single destination directory, ignoring what subfolders they originated in?

    Like

  35. brian Avatar
    brian

    @Mike — the easiest way to do this is, assuming your filelist.txt has a list of files with path names:

    for /f “delims=” %%i in (c:\temp\filelist.txt) do echo D|xcopy “%%i” “C:\temp\output\%%i%~nxf” /i /z /y

    That may work, I didn’t try it. The tilde in the variable is part of ‘variable substitution’ in the command shell. See http://superuser.com/questions/489240/how-to-get-filename-only-without-path-in-windows-command-line

    Like

  36. Alex Avatar
    Alex

    Hi Brian,

    Can you please guide me for the below query –

    I want to move files from one folder location to a temp location with conditions as below
    1) All the files lesses than sysdate
    2. After files are copied to temp location it should be zipped.
    3. Again I have to copy these zip file to a different location and than unzip it.

    Please help … Appreciate ur swift response!

    Regards!

    Like

  37. Kirke Avatar
    Kirke

    Hi Brain,

    Are you getting payed for all this work?
    I just read through the whole tread, and it seems you are the only contributer to actual solutions…..

    What I read is very usfull, but I can’t figure out how to seach the files from the list in subfolders.
    Is this even possible?

    I used this:

    For /f “delims=” %%i in (filelist.txt) do xcopy “c:\Temp\%%i?” “c:\Final” /i /z /y /s

    and it woorks great if all files are located in c:\temp\

    But the files I am looking for is in subfolders to c:\temp\

    Hope you yet again can come with a solution 🙂

    Best regards

    Like

  38. Kirke Avatar
    Kirke

    jesus just tried it out another time and it works…..you really are the man Brain, you should get payed for this….

    Like

  39. brian Avatar
    brian

    @Kirke — thanks for the kudos, and glad you got it working. Batch files are a labor of love… 🙂

    Like

  40. Adeya Avatar
    Adeya

    Hello, Brian!
    1. I’m beginner. Though cases discussed on your site are very similar to that what I need, still I can’t get the solution.
    2. My case:
    2.1. I have a folder C:\temp with, say, 3 Excel files Name1.xlsx, Name2.xlsx, Name3.xlsx
    2.2. I have FileList.txt file in C:\temp folder. File contains:
    Name2.xlsx
    Name3.xlsx
    2.3. I need to move files Name2.xlsx and Name3.xlsx, as defined in FileList.txt, from the folder C:\temp into the folder C:\temp\new

    PS. An advantage of vehicle with manual gear compare to the one with automatic is that in case of manual gear I can start the engine from the push. I am looking you to push me. Thank You.

    Like

  41. Adeya Avatar
    Adeya

    My OS is WINDOWS 7.

    Like

  42. brian Avatar
    brian

    Use the same command line, just change the source path and use MOVE instead of XCOPY.

    for /f “delims=” %i in (filelist.txt) do move /y “c:\temp\%i” “c:\temp\new\%i”

    Important note — if you put this in a batch file, you need to use %%i; if you run it directly from a command prompt, you use %i.

    Like

  43. Adeya Avatar
    Adeya

    This batch file:
    for /f “delims=” %%i in (C:\temp_ad\filelist.txt) do echo D|xcopy “C:\temp_ad\%%i” “C:\temp_ad\output\%%i” /i /z /y

    works neither in Win XP nor WINDOWS 7. Folder \output\ remains empty.
    Batch file resides in temp_ad folder.
    filelist.txt looks as follows:
    Name2.xlsx
    Name3.xlsx
    Hope on your help.
    Thanks.

    Like

  44. brian Avatar
    brian

    What is the output when you run just this from the console (not a batch file)?

    for /f "delims=" %i in (C:\temp_ad\filelist.txt) do echo xcopy "C:\temp_ad\%i" "C:\temp_ad\output\%i" /i /z /y

    Remember to make quotes normal quotes; I think WordPress is replacing them with curly quotes when the comment is posed.

    Like

  45. Adeya Avatar
    Adeya

    The problem was the quotes: they were not normal.

    Here is working version.

    @ECHO OFF
    cd C:\temp_ad
    for /f “delims=” %%i in (filelist.txt) do move /y “C:\temp_ad\%%i” “C:\temp_ad\output\%%i”
    @ECHO OFF

    Brian, you gave me a push, thank You, though I am not sure how long my ‘engine’ will work, maybe I will need another push:) Thanks a lot.

    Another small question: is it possible to WinZip the files? I wrote batch to unzip it, but how to zip?
    Highly appreciate your help.
    Adeya
    Thank you.

    Like

  46. brian Avatar
    brian

    @Adeya — funny you ask about zipping the files, I recently wrote a batch file to zip (using 7zip) a folder. Expect a blog post soon.

    Like

  47. andy Avatar
    andy

    Hi Brian!

    i’m in need of a little help here please, been going round and round with this to no avail!! 😦

    The goal of this batch is to copy files from a server to a local PC, scheduled to run every 30 minutes, to copy each project’s image folder and copy to a local PC.

    I have a set of text files, all with random names i.e. 154799.txt in a single folder, the contents of each file is a single line with a UNC path \server_name\folder\user\jobname (each of these paths contain image files)
    I need to use the contents of each text file as a path for a robocopy command
    There is an unknown number of *.txt files each with random name, so this needs to run once for each ‘new’ textfile.

    I have this batch that generates a list of the filenames and outputs into a single text file called files_list.txt file

    for %%a in (*.txt) do echo %%a >> \server_name\folder\user\file_list\files_list.txt

    for /F “tokens=*” %%g in (files_list.txt) DO @echo %%g

    That produces a file_list.txt with contents:
    154799.txt
    123456.txt
    254874.txt

    Where I’m stumped is getting the next batch to take each line of the files_list.txt as a source path

    for /f “delims=” %%i in (file_list.txt) do robocopy “%%i” c:\copy\server_footage\ /s /e

    Any help greatly appreciated! Thanks, Andy!

    Like

  48. andy Avatar
    andy

    last line has a typo it should be files_list.txt not file_list.txt!
    for /f “delims=” %%i in (files_list.txt) do robocopy “%%i” c:\copy\server_footage\ /s /e
    😐

    Like

  49. brian Avatar
    brian

    Two things that could be throwing you off:

    Use “delims= ” — that is, use a space as a delimiter. This solved a problem for me in eliminating white space at the end of the filename.
    You need three loops. Think of it this way:

    LOOP 1 — get the filename of all text files (*.txt) and put it in a new file (files_list.txt)
    LOOP 2 — go through files_list.txt, and for each line (i.e. each file, do LOOP 3
    LOOP 3 — called by loop 2, this reads each line in the file and does the robocopy

    Your LOOP2 and LOOP3 have to be in the same command, something like this:

    for /f “delims= ” %%i in (c:\temp\files_list.txt) do for /f “delims= ” %%j in (%i) do robocopy “%%j” c:\temp
    \destination\ /s /e

    Hope that helps!

    Like

  50. andy Avatar
    andy

    Yep, that’s got it! the for / do needed to be in the same command!

    Thanks Brian! 🙂

    Like

    1. andy Avatar
      andy

      Hi Brian,

      I have another change I would like to implement:
      Deleting the 154799.txt file if there is no new data (changes/updated/new files) within the source folder

      using the errorlevel check from Robocopy
      errorlevel 0 echo No Change & goto remove
      remove then deletes 154799.txt and all other *.txt files but I can’t seem to find a way to split this out of the original variable in Loop 3.
      It acts only on the last *.txt file it reads, so if there is no change to that, it deletes all *.txt, even if the preceding files had new or changed content.

      Section of script contains:

      call :REPORT_ERRORLEVEL
      goto :EOF

      :REPORT_ERRORLEVEL
      echo.

      if errorlevel 16 echo FATAL ERROR & goto end
      if errorlevel 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto end
      if errorlevel 14 echo FAIL + MISMATCHES + XTRA & goto end
      if errorlevel 13 echo OKCOPY + FAIL + MISMATCHES & goto end
      if errorlevel 12 echo FAIL + MISMATCHES& goto end
      if errorlevel 11 echo OKCOPY + FAIL + XTRA & goto end
      if errorlevel 10 echo FAIL + XTRA & goto end
      if errorlevel 9 echo OKCOPY + FAIL & goto end
      if errorlevel 8 echo FAIL & goto end
      if errorlevel 7 echo OKCOPY + MISMATCHES + XTRA & goto end
      if errorlevel 6 echo MISMATCHES + XTRA & goto end
      if errorlevel 5 echo OKCOPY + MISMATCHES & goto end
      if errorlevel 4 echo MISMATCHES & goto end
      if errorlevel 3 echo OKCOPY + XTRA & goto end
      if errorlevel 2 echo XTRA & goto end
      if errorlevel 1 echo OKCOPY & goto end
      if errorlevel 0 echo No Change & goto remove

      :remove
      for /f %%a in (\server_name\folder\copy_dir\file_list\job_list.txt) do del %%a
      :end

      Any suggestions please? Thanks!

      Like

Leave a comment