Move files of particular pattern in batch to another folder
Move files of particular pattern in batch to another folder
I am trying to write a Windows batch file with commands for moving files with a pattern the file name from one folder to another folder, and others to others.
For example:
vision.xls
should go to a particular~\vision
folder.
The below examples should work just as you explain for your need. Just plug in your paths for the source directory and the copy folder, and also put in your file pattern string as well. I tested and confirmed it works just as expected and as you described.
One example does a delete after the copy and the other does not and just does the copy. If you are expecting to copy the files to the new folder and then delete from the old folder, then you will want to use the one WITH DELETE.
As usual, you should test this to confirm it works for your particular needs before running against any critical files just in case.
EXAMPLE BATCH SCRIPT NO DELETE
@ECHO ON
SET SourceDir=C:\Path\Source
SET CopyDir=C:\Path\Vision
SET FilePatterName=*vision*.xls
FOR %%A IN ("%SourceDir%\%FilePatterName%") DO ECHO F | XCOPY /Y /F "%%~A" "%CopyDir%\"
GOTO EOF
EXAMPLE BATCH SCRIPT WITH DELETE
@ECHO ON
SET SourceDir=C:\Path\Source
SET CopyDir=C:\Path\Vision
SET FilePatterName=*vision*.xls
FOR %%A IN ("%SourceDir%\%FilePatterName%") DO (
ECHO F | XCOPY /Y /F "%%~A" "%CopyDir%\"
DEL /Q /F "%%~A"
)
GOTO EOF
Related videos on Youtube
Jyeta Nagpal
Updated on September 18, 2022Comments
-
Jyeta Nagpal almost 2 years
I am trying to write a Windows batch file with commands for moving files with a pattern the file name from one folder to another folder, and others to others.
For example:
vision.xls
should go to a particular~\vision
folder. Any help is appreciated. -
Jyeta Nagpal over 8 yearsThanks for replying. this is copying everything .I want it to copy only the file which contains vision in name of file. I see that you have set FilePatterName=*vision*.xls but not used not used in for loop.
-
Vomit IT - Chunky Mess Style over 8 years@JyetaNagpal Sorry about that, I replaced the
%FilePatternName%
from the loop with . when I was testing some stuff on my side. I've fixed this and sorry about that again. I've gotten both examples fixed so let me know how it goes. My total oversight on that and I should have double-checked that logic before I saved it in my answer.