To test with echo in one-liner (it should work with subfolder if you add /s in dir command)
for /f "delims=" %a in ('dir /b /a-d "?????.txt"^|findstr /R "\\.....\.txt$"') do @echo %~fa
one-liner copy command
for /f "delims=" %a in (
'dir /b /a-d "?????.txt"^|findstr /R "\\.....\.txt$"'
) do @copy "%~fa" "destination-dir"
Batch file
for /f "delims=" %%a in (
'dir /b /a-d "?????.txt"^|findstr /R "\\.....\.txt$"'
) do (
copy "%%~fa" "destination-dir"
)
Edit:
/a-d scan files
/ad scan folders
Without /a[d|-d] it will scan both
see dir /? for further reading
If I added "?????.txt" in command "dir" is to reduce the scope of scan and thereby reduce the time to scan.
You can always expand the scope of the scan in the command "dir" but the most important is the pattern in command "findstr"
I changed "^.....\.txt" to "\\.....\.txt$" to be able to scan folder name.
. Wildcard: any character
ˆ Line position: beginning of line
$ Line position: end of line
\. Escape: literal use of metacharacter .
See findstr /? for further reading
Why it doesn't work in some case? Because there is no universal solution, you have to adapt the command on your case. If you use /b without /s the dir command will produce an output without trailing slash \ therefore the findstr pattern with \\ will fail.
This will fail: dir /b "*.txt"|findstr /R "\\........$"
When this will success: dir /b "*.txt"|findstr /R "^........$"
Just as with a dir command that produces an output when the search pattern word is in the end of the line separated by a trailing slash will also fail.
As this will fail: dir /b /s "*.txt"|findstr /R "^........$"
but this will success: dir /b /s "*.txt"|findstr /R "\\........$"