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 files referenced in a text to a location  (Read 6530 times)

0 Members and 1 Guest are viewing this topic.

sp_key

    Topic Starter


    Greenhorn

    Batch file to copy files referenced in a text to a location
    « on: August 21, 2009, 08:35:01 AM »
    Hi all,

    I have a text file that contains a number of files and their location. (C:\MyFolder\myfile1.txt)

    I need to create a batch file that reads the filenames and their location of the text file and copies the actual files to a destination folder.

    So far I've found how to copy the entire folder but the problem is my list contains around 600 files while the entire folder contains thousands of files.

    Any thoughts?

    Many thanks in advance

    Salmon Trout

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #1 on: August 21, 2009, 01:14:19 PM »
    Please state the format of the text file.

    billrich

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #2 on: August 21, 2009, 08:23:46 PM »
    Please state the format of the text file.


    Does a plain text (.txt )  file have more than  the plain ascii format that opens with notepad?

    smeezekitty

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #3 on: August 21, 2009, 08:44:57 PM »
    i dont think you can do that in batch
    without an external program

    Salmon Trout

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #4 on: August 22, 2009, 01:40:54 AM »
    Does a plain text (.txt )  file have more than  the plain ascii format that opens with notepad?

    They mean the same thing.

    By "format", I meant, does the file contain just path and filenames, like this:

    C:\path to\folder\file1
    C:\path to\folder\file2
    C:\path to\folder\file3
    C:\path to\folder\file4

    etc ?





    smeezekitty

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #5 on: August 22, 2009, 01:44:08 AM »
    They mean the same thing.

    By "format", I meant, does the file contain just path and filenames, like this:

    C:\path to\folder\file1
    C:\path to\folder\file2
    C:\path to\folder\file3
    C:\path to\folder\file4

    etc ?





    i think thats what the orig poster means
    but i dont think you can read files in batch much less parse them

    Salmon Trout

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #6 on: August 22, 2009, 01:49:01 AM »
    but i dont think you can read files in batch much less parse them

    Where on earth did you get that idea?

    smeezekitty

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #7 on: August 22, 2009, 01:51:28 AM »
    batch is very unpowerful as a programming language
    its good for starting to program
    but not for complex programs and games

    Salmon Trout

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #8 on: August 22, 2009, 01:54:38 AM »
    batch is very unpowerful as a programming language
    its good for starting to program
    but not for complex programs and games

    smeezekitty, I respectfully suggest that you should think before you post... also you have not answered the specific question: Why did you write this:

    Quote
    but i dont think you can read files in batch much less parse them


    Salmon Trout

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #9 on: August 22, 2009, 03:08:59 AM »
    The point I was trying to make is that in a help forum like this, a "good" post is one where you have

    1. Read and understood the original question.
    2. Know the answer.

    A "bad" post is one where one or neither of these is true, for example where you just post of the top of your head stuff like "I don't think you can do xyz in batch", especially when you actually can do it very easily.

    Batch language is very good at simple tasks like reading text files and parsing the contents. That is what it was designed for. It is not so good at detecting sprite collisions or doing 3-D graphics. We know this.

    Here is how to read a text file and do something with each line it contains:

    1. Let us assume that this is the text file contents:

    Code: [Select]
    C:\path to\folder\file1
    C:\path to\folder\file2
    C:\path to\folder\file3
    C:\path to\folder\file4

    You can see it is a list of paths and filenames, one to a line

    2. Let us further assume that it is called My test file.txt, and it is in the folder C:\Myfolder.

    3. Assume that each line in the file refers to an actual file and that we want to copy each one to another folder, called C:\destination folder.

    4. Here is a batch file that will do that by reading each line of the file:

    Code: [Select]
    @echo off
    set textfile=C:\MyFolder\My test file.txt
    set destfolder=C:\destination folder
    for /f "delims=" %%F in ( ' type "%textfile%" ' ) do (
         echo Copying %%F
         copy "%%F" "%destfolder%"
    )



    smeezekitty

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #10 on: August 22, 2009, 11:20:48 AM »
    ???
    you can use a command in a control string of a for loop????
    ----edit----
    it sure cant parse C and thats a text file

    Salmon Trout

    • Guest
    Re: Batch file to copy files referenced in a text to a location
    « Reply #11 on: August 22, 2009, 11:49:17 AM »
    you can use a command in a control string of a for loop?

    (deleted annoying multiple question marks.)

    You can use the FOR /F command to parse the output of a command.

    FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    e.g.

    FOR /F "delims=" %%A in ( ' dir /b ' ) do echo %%A

    Quote
    it sure cant parse C and thats a text file

    You are getting two different uses of the word 'parse' confused.

    cmd.exe is not a C interpreter. Nobody said it was.

    Smeexekitty, you display an astonishing attitude:knowledge ratio which makes me wonder if you are aged about 11? Have you actually ever tried researching some of these things?

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Batch file to copy files referenced in a text to a location
    « Reply #12 on: August 23, 2009, 07:53:18 PM »
    Quote from: Salmon Trout
    ...if you are aged about 11?

    Judging by a reaction to this, an estimation can be made on the age.

    Back on topic:
    Until we see a sample of the file we are using no one can really help you. Only a SAMPLE, as we don't need 600 lines, 6 should be fine  :)
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    sp_key

      Topic Starter


      Greenhorn

      Re: Batch file to copy files referenced in a text to a location
      « Reply #13 on: August 24, 2009, 02:47:20 AM »
      Hey guys, chill out it's good to debate :)

      Salmon Trout, your code works fine! You can't even imagine how extremely useful this is to me. Greatly appreciated!

      The reason I did not give you guys a sample of my list is because I decide how to do it. It doesn't really matter however this was exactly what I had in mind. A long list of filenames with their location.

      One more question though I'm going to test this thoroughly. The files I'm copying are way larger than 2GB each, can you foresee any issues with that?

      Also, could you please explain me the for /f "delims=" %%F line? It's one thing to do something and another to understand it :)

      Many thanks again!
      Sp.

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: Batch file to copy files referenced in a text to a location
      « Reply #14 on: August 24, 2009, 05:24:48 AM »
      Over 2gb? Depending on your computer's speed, could take a while.

      The command FOR is used in many ways to parse things.

      I'm tired and am not going to take hours explaining how FOR works, but another member may.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.