Skip navigation

JSI Tip 7639. How can I copy selected NEW files from a source path to a destination path?


I have scripted CopyNew.bat to copy selected NEW files from a source path to a destination path.

The syntax for using CopyNew.bat is:

\[call\] CopyNew SourceFolder DestinationFolder \[/V\] \[Mask1\] \[Mask2\] \[Maskn\]

where:

SourceFolder      Can be expressed in the \[Drive:\]\\[Path\] or \\ComputerName\Share\\[Path\] format. 
                  NEW files in sub-folders are also copied.

DestinationFolder Can be expressed in the \[Drive:\]\\[Path\] or \\ComputerName\Share\\[Path\] format.

/V                Display the NEW files on the console as they are copied.

Maskx             If these optional parameters are omitted, all NEW files (*.*) are copied. 
                  Any mask that DIR considers valid can be used.

                  Sample valid masks:   *.*
                                        "*.txt"
                                        *.txt
                                        "k*.doc"
                                        k*.doc
                                        "Word Document*.doc"
                                        *.txt,*.doc

                  Sample invalid masks: "*.txt,*.doc"    since the comma (,) is NOT considered a 
                                                         separator when encapsulated in quote (") marks.
NOTE: A file is NEW if it DOES NOT EXIST in the destination path.

NOTE: CopyNew.bat will create any needed destination path folders and sub-folders.

NOTE: CopyNew.bat returns the following ERRORLEVELs:

     0  All NEW files were copied without errors.
     1  The parameter syntax was incorrect.
     2  The source path does NOT exist.
CopyNew.bat contains:
@echo off
setlocal
if \{%2\}

\{\} goto syntax set src=%1 set src=%src:"=% set dst=%2 set dst=%dst:"=% if not exist "%src%\*.*" goto nosrc set verbose=N if \{%3\}

\{\} set mask=*.* if /i \{%3\}

\{/v\} set verbose=Y&if \{%4\}

\{\} set mask=*.* if /i \{%3\}

\{/v\} shift shift :ml shift if \{%1\}

\{\} goto copy if not defined mask set mask=%1&goto ml set mask=%mask%,%1 goto ml :copy if not exist "%dst%\*.*" md "%dst%" for /R "%src%" %%a in (%mask%) do set file=%%a&call :isnew endlocal exit /b 0 :syntax @echo Syntax: \[call\] CopyNew SourceFolder DestinationFolder \[/V\] \[Mask1\] \[Mask2\] \[Maskn\] endlocal exit /b 1 :nosrc @echo CopyNew - SourceFolder "%src%" not found. endlocal exit /b 2 :isnew call set work=%%file:%src%=%% if exist "%dst%%work%" goto :EOF if "%verbose%" EQU "Y" @echo "%dst%%work%" call :dstfld "%dst%%work%" call :copyq>nul goto :EOF :dstfld set fld=%~DP1 if not exist "%fld%*.*" MD "%FLD%" goto :EOF :copyq copy "%file%" "%dst%%work%"



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