I have a directory with hundreds of files with descriptive, different names but always the same extensions (example):
LetItBe(TheBeatles).mid
LetItBe(TheBeatles).jpg
LetItBe(TheBeatles).ran
LetItBe(TheBeatles).zip
LetItBe(TheBeatles)Scan.mid
Scan would always be at end of filename and always the same word)*
Next group:
HeyJude(TheBeatles).mid
HeyJude(TheBeatles).jpg
HeyJude(TheBeatles).ran
HeyJude(TheBeatles).zip
HeyJude(TheBeatles)Scan.mid
I want the batch to
Randomly select a number of files (say 10) and copy ALL of the associated files (i.e. the two MID, the jpg, the ran, the zip) with the same filename to a different directory.
I don't want the batch to stop if, for example, one of the groups of files is missing the jpg file or the mid file, but just to copy the existing files to the new folder and move on to the next randomly selected group.
I've found a batch code that works in moving one MIDI file randomly, but I'm not sure how to insert the code that specifies all files with the same filename but different extensions should also be moved...
@echo off
set folder=C:\Test1
set destfolder=C:\Test2
for /f "delims=" %%C in ('dir /b /a-d "%folder%\*.mid" ^| find /c /v ""') do set /A num=%random% %% %%C
for /f "delims=" %%F in ('dir /b /a-d "%folder%\*.mid" ^| more +%num%') do set name=%%F & goto next
:next
echo (By the way, I chose %name% )
move "%folder%\%name%" "%destfolder%\%name%"
User Mofi kindly offered this help..
The command to copy all files starting with same string is copy "%FileName%." "C:\Destination Folder" with environment variable FileName being defined by the code selecting randomly one of the file groups, for example, with set "FileName=HeyJude(TheBeatles)". More safe would be using two COPY command lines: copy "%FileName%.*" "C:\Destination Folder" and copy "%FileName%Scan.mid" "C:\Destination Folder" 2>nul in case of there are the file sets HeyJude(TheBeatles) and HeyJude(TheBeatles) Live and file set HeyJude(TheBeatles) should be copied to destination folder.
But I can't figure out how to insert it into the existing code. I tried editing the last line to be
move "%folder%\%name%*.*
But then no files at all are moved and the system can't find any matching filename.
Help appreciated!
For randomly selecting files, you can use %random%
and iterate 10 times. This will randomly select 10 file names, then copy the file name `.* meaning any file with the given name, with any extension will be copied.
@echo off & setlocal enabledelayedexpansion
set "folder=C:\Test1"
set "destfolder=C:\Test2"
set num=0
pushd "%folder%"
for %%a in (*.*) do (
set /a num+=1
set "name[!num!]=%%~na"
)
for /l %%i in (1,1,10) do (
call :randm
)
popd
goto :eof
:randm
set /a "rnd=(num*%random%)/32768+1"
copy "!name[%rnd%]!*" "%destfolder%"