JSI Tip 7684. How can I copy files to a destination folder only if the file does not exist in the destination folder?


I have scripted CopyNonExit.bat to copy files from a source folder tree to a destination folder tree, but only if the file does not exist in the destination path.

The syntax for using CopyNonExit.bat is:

CopyNonExit From To

where From is the source folder or URL, and To is the destination folder or URL.

Valid parameters include:

Format                         Examples
Drive:                      C:
Drive:\folder               c:\Folder  or "C:\Folder1\Folder2"
\\ComputerName\ShareName    \\JSI001\Users or "\\JSI001\Users\Jerry"
NOTE: If required, CopyNonExit.bat will add directories and sub-directories to the destination folder path.

CopyNonExit.bat contains:

@echo off
setlocal ENABLEEXTENSIONS
if \{%2\}==\{\} goto syntax
set from=%1
set to=%2
set from=%from:"=%#
set to=%to:"=%#
set from=%from:\#=%
set from=%from:#=%
set to=%to:\#=%
set to=%to:#=%
set work="%from%\*.*"
if not exist %work% goto syntax
for /r "%from%" %%f in (*.*) do set fromline=%%f&call :merge
endlocal
exit /b 0
:syntax
@echo Syntax: CopyNonExit From to
endlocal
exit /b 1
:merge
call set toline=%%fromline:%from%=%to%%%
if exist "%toline%" goto :EOF
call :parse "%toline%"
if not exist "%fp%\*.*" MD "%fp%"
call :copyit>>nul 2>>&1
goto :EOF
:parse
set fp=%~DP1#
set fp=%fp:\#=%
set fp=%fp:#=%
goto :EOF
:copyit
copy "%fromline%" "%toline%"



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish