0

I'm using Windows 10 and would like to create a batch file to move files in multiple folders to a different single folder. I need to move the files that are listed in a text file. Some questions that come up are: where does the text file have to be or doesn't it matter? Does the batch file need to be run from a certain folder? I don't have a lot of experience so any help would be appreciated.Thanks in advance!

Here is what I started with

@echo off
set src_folder=C:\Users\Brian\Desktop\production\15XXXX
set dst_folder=C:\Users\Brian\Desktop\preliminary\eco\170555
set file_list=@C:\Users\Brian\Desktop\preliminary\eco\170555\170555.txt
for /f "tokens=*" %%i in (%file_list%) DO (
   echo f | move /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i")

The text file has one line right now and the number is 150252.I would eventually want to move more files from C:\Users\Brian\Desktop\production\16XXXX, C:\Users\Brian\Desktop\production\17XXXX etc. I am running my batch file from C:\Users\Brian\Desktop. I hope I've added enough detail so I can get this figured out. Thanks again.

bnester
  • 1
  • 1
  • 3
  • 2
    Possible duplicate of [Batch: Copy files from txt file into one folder](https://stackoverflow.com/questions/9143018/batch-copy-files-from-txt-file-into-one-folder) –  Jun 24 '17 at 09:11
  • Some sort of idea of the content and layout of the text file would be appreciated. You can add it to your original post by using the **edit** facility. – Compo Jun 24 '17 at 10:56
  • I added some additional information. Thanks! – bnester Jun 24 '17 at 14:54
  • Does the text file only contain those numbers? What do they represent? A file in a folder or the entire folder? Do you want all files in 150252 moved? And when you say "16XXXX", does that represent wildcards (every folder that starts with "16") or a specific folder? – Regejok Jun 24 '17 at 16:11
  • The text file only contains one number in it right now and it represents a pdf file that is located in a folder that is named 15XXXX. After I get one file to move I will add additional numbers to the text file. For example, I may add number 166700 to the text file and would need the batch to move that pdf to the folder named 16XXXX. Thanks for the question. – bnester Jun 24 '17 at 16:41
  • Solved, see below. I had to specify file type in my text file otherwise it wouldn't work. echo off set src_folder=c:\Users\Brian\Desktop\production\15XXXX set src_folder1=c:\Users\Brian\Desktop\production\16XXXX set dst_folder=c:\Users\Brian\Desktop\preliminary\eco\170555 set file_list=c:\Users\Brian\Desktop\good_batch_files\170555.txt ::pause if not exist "%dst_folder%" mkdir "%dst_folder%" ::pause for /f "delims=" %%f in (%file_list%) do (move "%src_folder%\%%f" "%dst_folder%\") ::pause for /f "delims=" %%f in (%file_list%) do (move "%src_folder1%\%%f" "%dst_folder%\") – bnester Jun 25 '17 at 02:50

1 Answers1

0

You could loop through the .txt file and for each line, loop through the sub folders, though I believe doing the following is a more efficient method:

for /r "C:\Parent" %%# in (*) do findstr "%%~nx#" "list.txt"&&move "%%#" "Folder"

This script recursively loops through parent and if it finds one of the files in "list.txt", moves it to "Folder".

For each file whose path isn't specified, the script will look in its working directory. "list.txt" would have to be where the .bat resides.

Using findstr also allows you to put the filenames into the .txt in one line (e.g. file.txt bla.dat;example.mp4). If those files have spaces, use find or findstr /c instead.

Regejok
  • 436
  • 2
  • 5