• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 827
  • Last Modified:

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?
0
herbro
Asked:
herbro
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
mikainzCommented:
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
0
 
garycaseCommented:
Robocopy is indeed quite powerful, but I can't find a "copy only lonely files" switch (it calls files such as you've described "lonely files").

You can, however, do as you've asked with the freeware version of XXCopy.

Download and install the free version of XXCopy  []www.xxcopy.com]

The just use this command:    XXCOPY <source>  <destination>  /BB
0
 
garycaseCommented:
.... Note:  You asked for a command-line solution, so that's why I suggested XXCopy.   But you can also do this very easily within Windows with SyncBack, which has many options for the various profiles you set to backup and/or synchronize folders.   [http://www.2brightsparks.com/freeware/freeware-hub.html ]
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
t0t0Commented:
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
0
 
mikainzCommented:
@ 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
0
 
garycaseCommented:
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.
0
 
t0t0Commented:
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!!

 

 

0
 
AmazingTechCommented:
for whatever it is worth

XCOPY /d /s <source> <destination>

Without specifying a date with "/d" will only copy files that are newer or exists in the source than the destination.
0
 
garycaseCommented:
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.
0
 
AmazingTechCommented:
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.
0
 
t0t0Commented:
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

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now