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?
-
Possible duplicate of [Move a file to archive folder in cmd using wildcards](https://superuser.com/q/517486/173513) and [How does the Windows RENAME command interpret wildcards?](https://superuser.com/q/475874/173513) – jww Oct 07 '18 at 21:00
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,300
- 3
- 32
- 48
-
Fantastic, works like a charm in a batch file, thank you very much! – A Box Of Cheese Oct 05 '18 at 22:16
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\
- 590
- 2
- 15
-
Thanks, but again the problem is the wildcards (* in this case). I need something that would filter out everything besides exactly ".xml" because with this the .xml_ files get in – A Box Of Cheese Oct 05 '18 at 19:14
-
That is strange, I made an folder source and destination. In the folder source I had an file test.xml and a file test.xml_ and the command I posted only moved the .xml and and left the .xml_ alone. However https://superuser.com/questions/825615/windows-wildcards-with-files-having-more-than-3-characters-extensions mentioned by Scott may apply in your case. My test was done in Windows 10. – User025 Oct 05 '18 at 19:24
-
I see, I'm using windows 7 so that might be the cause of the issue, can findstr be of any use here in a script that finds only file names ending with exactly .xml and moving them? – A Box Of Cheese Oct 05 '18 at 19:27
-
I'm not sure how you could accomplish that with findstr however there is an Powershell cmdlet "Move-Item" so if Powershell is an option that could work. The command would be `Move-Item -Path C:\source\folder\\*.xml -Destination C:\destination\folder\\` – User025 Oct 05 '18 at 19:35