Link to home
Start Free TrialLog in
Avatar of herbro
herbroFlag for United States of America

asked on

Is there an XP-based DOS command that will copy only files that do NOT exist in the destination folder?

Is there an XP-based DOS command that will copy only files that do NOT exist in the destination folder?
Avatar of mikainz
mikainz
Flag of Austria image

why don't you use robocopy.
it's part of the freely available resource kit http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en
hth
SOLUTION
Avatar of Gary Case
Gary Case
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Like this:

    if not exist "%destination%\%file%" copy "%source%\%file%" "%destination%\"

or like this:

    if not exist "%destination%\%file%" (
        copy "%source%\%file%" "%destination%\"
    )


you will need to set your variables to point to the source folder of the file, the filename itself and the destination folder. for example, if you're copying a file from c:\ to c:\temp. suppose the file you're moving is named readme.txt. you would then do:

    set file=readme.txt
    set source=c:\
    set destination=c:\temp
    if not exist "%destination%\%file%" copy "%source%\%file%" "%destination%\"

or  you could just hard-code the data into the command like this:

    if not exist "c:\temp\readmen.txt" copy "c:\readme.txt" "c:\temp\"


if you want to copy multiple files of any type that do not exist then you need the following:

   for /f "tokens=*" %a in ('dir /a-d /b "c:\*.*"') do if not exist "c:\temp\%a" @copy /y "c:\%a" "c:\temp\"

if you don't want output to appear on the screen (ie, silent mode) then add '>nul' to the end like this:

   for /f "tokens=*" %a in ('dir /a-d-h /b "c:\*.*"') do @if not exist "c:\temp\%a" @copy /y "c:\%a" "c:\temp\" >nul
@ garycase
'Robocopy is indeed quite powerful, but I can't find a "copy only lonely files" switch'
there is no switch, because robocopy skips existing files by default.

find enclosed a log file robocopy generated during a little test copying the same 3 images 2 times.

used switches   /V /S /E /SEC /NP /R:2 /W:2

hth
 

testcopy.txt
I don't use robocopy these days ... so don't have a convenient install to check this ==> does that command line back up ONLY "lonely files" ??  ... which I think is what herbro is asking for here.

... or would it copy the file again if it had been modified since the original copy??  [The way the question is phrased here, it should NOT copy the file in that case -- but if I recall correctly, Robocopy would copy it if it had changed (although this may very well depend on the switch settings)]

Robocopy does have the advantage of being free ... but XXCopy is a far more powerful copy utility.   If the useage intended qualifieds for the freeware license, it's worth switching.   If you have to buy it; then it depends on whether the added features are needed here.
The disadvantage of using third-party programs such as Robocopy and Xxcopy is that batch files which depend on them are not as portable as those that rely solely on standard DOS commands.

With a little effort, the standard set of DOS and Batch File commands can accomplish even the most challenging of I/O tasks.

As stated previously, the construct for accomplishing this in DOS is:

    FOR /F "TOKENS=*" %A IN ('DIR /A-D-H /B "source\*.*"') DO @IF NOT EXIST "destination\%A" @COPY /Y "source\%A" "destination\"

where source is the source path, destination is the destination path and *.* is the filespec. NOTE: The elements in capital letters are DOS and Batch file commands and options. The lowercase elements are the data you supply to the DOS construct.

Simple.... and COMPATIBLE!!

 

 

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note that the XCopy command above will copy files that already exist in the destination if they are newer ==> what was asked was for a way to ONLY copy files that don't already exist.
Avatar of AmazingTech
AmazingTech

The asker may not know of this capability with xcopy /d and it might actually be what he/she is after.

We'll let the asker make a comment.
hiya amazingtech

With the greatest of respect, i'm not compfortable with xcopy. i think it's about time someone wrote an all-purpose generic exclude-copy batch file named something like EXCOPY.BAT

The code below is an example how this might work.

herbro
Why not make your own command? Copy-&_Paste the following code into Notepad and save it in your Windows folder as EXCOPY.BAT. Then, for example, to replace all occurances of the text "hello world" with "HELLO WORLD" in all text files in the C:\TEMP\ folder and it's sub-folders, enter the following command:

   EXCOPY "hello world" "HELLO WORLD" c:\emp\*.txt /s

EXAMPLE 2:

Replace Adam with Eve in all (TXT) text files in the current folder (only) whose filenames start with the letters FILE, enter the following command:

   EXCOPY Adam Eve file*.txt

If you also want to process similar files in sub-folders, add '/s' to the command as in:

   EXCOPY Adam Eve file*.txt /s

The syntax for the batch file is:

   EXCOPY "search string" "replacement string" "filespec" [/s]

Where 'search string', 'replacement string' and 'filespec' are enclosed in double-quotes if they contain spaces.

PLEASE NOT: During the process of replacing text in files, the original files are renamed (for safe-keeping) with a '.$av' file extension should you need to reverse the process. ALSO: Be sure to clear up (move or delete) any '.$av' files between repeated runs otherwise you'll get errors as code to avoid this has not been added however, it can be should you want it.
.
::EXCOPY.BAT - Written by t0t0
@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /a-d /b %4 %3') do (
   if not "%%~nxa"=="%~nx0" (
      set /a count=0
      for /f "tokens=* delims=" %%b in ('findstr /i /v /n "th15_text_d0e5_n0t_ex15t"  "%%a"') do (
         set /a count+=1
         set line=%%b
         
         if !count! lss 10     (set line=!line:~2!) else (
         if !count! lss 100    (set line=!line:~3!) else (
         if !count! lss 1000   (set line=!line:~4!) else (
         if !count! lss 10000  (set line=!line:~5!) else (
         if !count! lss 100000 (set line=!line:~6!)))))
         
         if "!line!"=="" (
            echo.>>"%%~dpa_%%~na.tmp"
         ) else (
            echo !line:%1=%2!>>"%%~dpa_%%~na.tmp"
         )
      )
      ren "%%~dpnxa" "%%~na.$av"
      ren "%%~dpa_%%~na.tmp" "%%~nxa"
   )
)

Open in new window