Batch move with exceptions? Or using xcopy and deleting original after? -


i have script extracts files sub-directories , deletes empty sub-directories afterwards, part extracts reads:

for /r %%a in (*.*) move "%%a" "%~dp0" 

is there way exception of sub-directories named "_unsorted"? know xcopy has /exclude option,

for /r %%a in (*.*) xcopy "%%a" "%~dp0" /exclude "\_unsorted\" 

would work, i'm not sure how delete original after it's copied have same result move

some batch-only options:

  1. add filter loop body:

    for /r %%a in (*.*) (   (echo %%~dpa | find /i "\_unsorted\" 1>nul) || move "%%a" "%~dp0" ) 

    alternatively:

    for /r %%a in (*.*) (   (echo %%~dpa | find /i /v "\_unsorted\" 1>nul) && move "%%a" "%~dp0" ) 

    in both versions, find command used match file's path against substring \_unsorted\. in first version, find returns success if there match , fail otherwise. move command called in case of fail, effect of || operator`.

    in second version, /v switch reverses result of find , success means no match. accordingly, && operator used call move in case of success.

  2. apply filter file list, loop never iterates on _unsorted entries.

    for /f "delims=" %%a in (   'dir /s /b ^| find /i /v "\_unsorted\"' ) move "%%a" "%~dp0" 

    this more essential change original script previous option, replaces for /r loop for /f one.

    basically, for /f loop used read/parse piece of text, text file or command's output. in case, dir command provides complete list of files in current directory's tree , find command filters out containing \_unsorted\ in paths. for /f loop reads list after it's been filtered down find, means never hits files stored in _unsorted (sub)folders.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -