I want to move my .xml files from a directory (let's call it "from") to another (we'll call it "to") but using wildcards moves unwanted files like .xml_. How can I move them while being specific about the ".xml" extension?
2 Answers
As @Scott's link discusses, *.xml matches *.xml_, because the 8.3 name ends in .XML (use dir /x to show).
Fortunately, the ForFiles command is not subject to this eccentricity, so you can use:
forfiles /m *.xml /c "cmd /c move @path TargetDir\"
Notes:
- Because
moveis an internal command, a separatecmdis needed to invoke it (this would not be needed with an external command (such asxcopy), as inforfiles /m *.xml /c "xcopy @path TargetDir\"). There is a
/soption, which will recurse through subdirectories, but it will not recurse the target directory: if you want the source tree to be matched at the target, you will need to parse the source path in order to find the correct target directory, which is probably best done in a batch file:forfiles /m *.xml /c "cmd /c call mover.cmd @relpath TargetDir\"Should you need
mover.cmd, I'll leave writing it as a scripting exercise for you.- I haven't tested what happens when there are spaces in the file or directory names, but I would expect complications.
- 17,958
You should be able to only move .xml files by using *.xml as file name. This would result in the following command:
move C:\source\folder\*.xml C:\destination\folder\
- 742