The Batch file below use a method that should be the fastest one to achieve this process with a large number of files: it first get a list of the folders to search in, and then search each file name in that list using the fastest way to do that. This way, the total number of files have not any effect in the processing time, just the number of folders (that is
much lower).
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem source = Path to the file that contain the names list "names without the extension"
rem location = The top folder to search in
rem destination = Path to the destination folder (even if it not exist)
set "source=C:\Users\Evan\Desktop\breaks filenames.txt"
set "location=C:\Users\Evan\Desktop\location folder"
set "destination=C:\Users\Evan\Desktop\Collating folder"
if not exist "%destination%" (
md "%destination%"
) else (
del "%destination%\NotFound.txt" 2>NUL
)
rem Get a list of existent folders in location folder
set folders=
for /R "%location%" /D %%d in (*) do set folders=!folders!%%d;
rem Process each file name in the list
set n=1000
for /F "usebackq delims=" %%n in ("%source%") do (
set /A n+=1
set num=!n:~-3!
rem Search this file name in the folders
for %%f in ("%%n.wav") do set "filePath=%%~dp$folders:f"
if defined filePath (
echo Copying: !num!- %%n.wav
copy /b "!filePath!%%n.wav" "%destination%\!num! %%n.wav" >NUL
) else (
echo Not found: !num!- %%n.wav
echo %%n.wav>> "%destination%\NotFound.txt"
rem Create an empty file in the destination folder
cd . > "%destination%\!num! %%n.wav --- NOT FOUND"
)
)
if exist "%destination%\NotFound.txt" echo Check NotFound.txt for filenames that were missing
echo/
pause
This Batch file:
- Does NOT search the .wav files directly in the location folder, but only in sub-folders beneath it (to speed up the process). If this is required, a small modification is needed.
- Copy
the first file found in the folders, taking they in alphabetical order.
- Will not find a .wav file if its name include an exclamation mark! This problem may be fixed, if required.
- Create empty files in the destination folder when a file name is not found, to preserve the order of the original list.
Please, try it and report the results!
Antonio