Search code examples
listfunctiondirectorymove

batch/bat - Move function with as input a text file with list of directories


I'm writing a bat file and would like to add a move function. The idea is that I have a .txt file with on each line a relative path\file.

So for instance - and this is just a random example:

Data\English\caust00.tga

Data\English\Specific\caust01.tga

Data\caust00.tga

Data\caust00

At the moment I only have a delete function, which is simple because it doesn't require an additional set with variables. (Obviously, there are more files inside the "Data" otherwise I'd just move the entire folder).

Say the 4 lines above are saved in Test.txt, it would only be this line:

for /F "tokens=*" %%A in (Test.txt) do del "%%A"

to delete all 4 files. Now this is nice, but I want to implement a move function: i.e. that it creates a new folder, say "Temp1" with inside it - this depends on the content of "Test.txt":

Data\English\caust00.tga

Data\English\Specific\caust01.tga

Data\caust00.tga

Data\caust00

I hope you didn't get lost already. I'd love to receive some advice, because I'm pretty much clueless how to go about it.

EDIT: So, I have a folder called "Data" with a lot of subfolders and files inside it. I also have a text file with this content:

Data\Movie\caust00.tga

Data\Movie\caust01.tga

Data\caust02.tga

Data\caust03.tga

Data\WaterPlane\SCCSpyDrone.ani

Data\WaterPlane\SCCStop.ani

Data\WaterPlane\SCCTimedChg.ani

Now I want to implement a move function that moves these files to a new folder "Test" so that we get this:

Test\Data\Movie\caust00.tga

Test\Data\Movie\caust01.tga

Test\Data\caust02.tga

Test\Data\caust03.tga

Test\Data\WaterPlane\SCCSpyDrone.ani

Test\Data\WaterPlane\SCCStop.ani

Test\Data\WaterPlane\SCCTimedChg.ani


Solution

  • Try this:

    mkdir C:\DestFolder\
    move C:\SourceFolder\*.* C:\DestFolder\
    

    This will move all files from SourceFolder to DestFolder

    EDIT:

    What if you do something like this instead:

    If you have the following folder structure:

        SourceFolder
         |-move.bat
         |-FilesToMove.txt
         |-1.txt
         |-2
           |-2.txt
    
    

    Where FilesToMove.txt contains:

    1.txt 
    2\2.txt
    

    and move.bat looks like this:

    for /F "tokens=*" %%A in (FilesToMove.txt) do call:moveFunc %%A C:\DestFolder\
    
    :moveFunc
    md %2\%1\..\ 2> nul
    move %1 %2\%1
    

    When you now run move.bat, 1.txt and 2\2.txt will be moved to C:\DestFolder. Does this do what you need?