I need to create a batch file that can search a source directory and all its subfolders for files with "MSDS" in their name (around 1500-2000 files), and copy them to a destination directory without copying the folder structure, so that the destination contains all the files at the same level. I also need to make the batch run on a regular basis (which I'm sure I can do with task scheduler) to check the source for new/updated files and copy those to the destination as well. Below is what I have so far, but I have a feeling it can be a bit more efficient since it's copying all the MSDS files every time, so any suggestions or advice would be appreciated.
Code: Select all
@echo off
::Copy files with "MSDS" in the name from origin to destination
ROBOCOPY <origin path> <destination path> *MSDS* /S /XO /PURGE /R:0
::Move all files to top level of the folder structure in destination
FOR /R <destination path> %%G IN (*MSDS*) DO move /y "%%G" "<destination path>"
::Delete all empty folders in destination
for /f "delims=" %%i in ('dir <destination path> /s /b /a:d ^| sort /r') do rd "%%i">NUL