Hope this helps!
I created C:\Files, C:\Folder1, C:\Folder2, C:\Folder3.
I made three text files: A.txt, B.txt, C.txt (First lines are "Folder1","...2","...3" respectively).
A.txt
B.txt
C.txt
Code: Select all
Folder3
aaaaaaaaaaaaaaaaaaaaaaabbbbb
bbbbbbbbcc
dddd
dddddddrrrr
I am quite lazy to write a good sample text files
A workaround code (sorry for its ugliness):
Code: Select all
@echo off
for /r "C:\Files\" %%a in (*) do call :head "%%~a"
pause
exit /b 0
:head
set "filename=%~1"
for %%F in (Folder1 Folder2 Folder3) do (
for /f "tokens=1,* delims=:" %%A in (
'type "%filename%"^|findstr /n "%%F"'
) do (
if %%A equ 1 move /Y "%filename%" "C:\%%F"
)
)
exit /b 0
Does the job, but because of the move of files, but the %%a is not updated, there are errors:
Output:
Code: Select all
1 file(s) moved.
The system cannot find the file specified.
The system cannot find the file specified.
1 file(s) moved.
The system cannot find the file specified.
1 file(s) moved.
Press any key to continue . . .
The error came from the 'type ......' part.
I am not good on "formal" error handling. However, since that is not really a "huge" error, we can just prevent the error message to be printed:
(Slightly edited...)
Code: Select all
@echo off
for /r "C:\Files\" %%a in (*) do (
for %%F in (Folder1 Folder2 Folder3) do (
for /f "tokens=1,* delims=:" %%A in (
'type "%%~a" 2^>nul^|findstr /n "%%F"'
) do (
if %%A equ 1 move /Y "%%~a" "C:\%%F"
)
)
)
pause
Output:
Code: Select all
1 file(s) moved.
1 file(s) moved.
1 file(s) moved.
Press any key to continue . . .
I think there are simplier techniques ('coz I am still learning...), but this is what I can provide.
Meerkat