Some batch-only options:
Add a filter into the loop body:
for /r %%a in (*.*) do ( (echo %%~dpa | find /i "\_Unsorted\" 1>nul) || move "%%a" "%~dp0" )
Alternatively:
for /r %%a in (*.*) do ( (echo %%~dpa | find /i /v "\_Unsorted\" 1>nul) && move "%%a" "%~dp0" )
In both versions, the
find
command is used to match the file's path against the substring\_Unsorted\
. In the first version,find
returnssuccess
if there is a match andfail
otherwise. Themove
command is called only in case offail
, which is the effect of the||
operator`.In the second version, the
/v
switch reverses the result offind
andsuccess
now means no match. Accordingly, the&&
operator is used to callmove
in case ofsuccess
.Apply the filter to the file list, so that the loop never iterates over
_Unsorted
entries.for /f "delims=" %%a in ( 'dir /s /b ^| find /i /v "\_Unsorted\"' ) do move "%%a" "%~dp0"
This is a more essential change to the original script than the previous option, as this replaces the
for /r
loop with afor /f
one.Basically, a
for /f
loop is used to read/parse a piece of text, a text file or a command's output. In this case, thedir
command provides a complete list of files in the current directory's tree and thefind
command filters out those containing\_Unsorted\
in their paths. Thefor /f
loop reads the list after it's been filtered down byfind
, which means it never hits files stored in_Unsorted
(sub)folders.