I have a pretty extensive directory structure:


I want to move (not just copy) all files with .txt extention to a another directory c:\textfiles.

I'd like to do that via command line (the regular one, not Powershell), possibly in a batch file.

I've tried the move command but it only seems to work on files in the current directory. I've also tried xcopy, but it doesn't delete files on the source after the copy is complete.


From: How to move all files with specific extension from all subdirectories to their parent using CMD?

This command will copy recursively al pdf files from source to target directory using cmd in windows 7 - tested and works

for /r "c:\source_directory\" %%x in (*.pdf) do move "%%x" "c:\target_directory\"

See also:

windows - Move all files from multiple subfolders into the parent folder - Super User

Copy Multiple Files from Subfolders to Single Folder - Microsoft Community



rsync is a tools available for windows too, normally used to remote synchronize directory... but it works fine on local directory too... as stated in its own description

a fast, versatile, remote (and local) file-copying tool

From this answer on unix.stackexchange...

rsync -a --prune-empty-dirs --include '*/' --include '*.txt' --exclude '*' source/ target/

Note that it exists an option to delete the files from the origin that you can include in the command line, I think the one you search for is --remove-source-files, but check it carefully. Another option you may find useful is -n

    -n, --dry-run               perform a trial run with no changes made
    --remove-source-files       sender removes synchronized files (non-dir)


find is another tool you can find in your bash shell under windows as well as a compiled executable(gnu findutils)... The following line works under Linux but you can modify the cp statement to be compliant with your system specifications. From another answer...

 find . -name '*.txt' -exec cp --parents \{\} /target \; 

Windows equivalent of find

From another quesiton/answer for your batch script you may use this hints...

 dir <drive: [drive:]> /s | findstr /i <pattern>

... of course if they felt the need to do a powershell it is reasonable to expect that in that environment there should be a more simple way to do it...

but maybe the following one is an enough simple starting point for your script:

dir c:\foo /s *.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.