Search and copy files from source to destination

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
danielmolinski
Posts: 1
Joined: 08 Oct 2015 10:52

Search and copy files from source to destination

#1 Post by danielmolinski » 08 Oct 2015 11:03

Hi there,

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

TheHunterManX
Posts: 54
Joined: 14 Aug 2015 05:59

Re: Search and copy files from source to destination

#2 Post by TheHunterManX » 09 Oct 2015 04:49

Hope this helps!

Code: Select all

@echo off
REM Set dir here...
set dir=
REM If it has MSDS in the file do...
for %%f in (*MSDS*.*) do (
REM Sets file as the file name...
set File=%%f
call :Copy
    )
REM If no new files were found exits...
No files were found!!
pause >nul
exit /b 0
:Copy
REM If file in dir exists...
if exist "%dir%\%File%" (
REM goto check...
goto :EOF) else (
REM If file in dir doesnt exist...
COPY "%File%" "%dir%"
goto :EOF)


To check when computer starts up refer to this page:
http://www.computerhope.com/issues/ch000322.htm



EDIT: sorry, next time I will
Last edited by Squashman on 09 Oct 2015 07:06, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Post Reply