xcopy delete destination files not in source

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

xcopy delete destination files not in source

#1 Post by tinfanide » 28 Oct 2011 23:39

Source:
file1

Destination:
file3

I want to copy file1 to the destination folder
no problem for that

But I wonder how I can after xcopying, delete file3 that does not exist in the Source folder
to do a thing like file comparison

I search online for solutions and people're saying there's no such function in BATCH
Is that true?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: xcopy delete destination files not in source

#2 Post by Ed Dyreen » 29 Oct 2011 00:03

'
It can be done in dos, you just need to use the dir command both on the destin and the source, this then can be compared.
All scripting languages I know support cross-language programming, so there really are no limits..

something like:

Code: Select all

dir /b source >sourcecompare
dir /b destin >destincompare

comparing can be done in several ways using findstr, fc, comp, or preferably for
Actually you don't even need dir, you can also use for /f !

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: xcopy delete destination files not in source

#3 Post by tinfanide » 29 Oct 2011 01:13

Ed Dyreen wrote:'
It can be done in dos, you just need to use the dir command both on the destin and the source, this then can be compared.
All scripting languages I know support cross-language programming, so there really are no limits..

something like:

Code: Select all

dir /b source >sourcecompare
dir /b destin >destincompare

comparing can be done in several ways using findstr, fc, comp, or preferably for
Actually you don't even need dir, you can also use for /f !


How to use it?
Is ">" for comparison?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: xcopy delete destination files not in source

#4 Post by dbenham » 29 Oct 2011 09:06

Ed Dyreen wrote:Actually you don't even need dir, you can also use for /f !
FOR /F will most likely require some form of 'DIR' command within the IN clause. A simple FOR would be easier and more efficient :wink:


@tinfanide - I think this is what you are looking for

Code: Select all

for %%F in ("destination\*") do if not exist "source\%%~nxF" del "%%~fF"

If you have ROBOCOPY, then it is even easier:

Code: Select all

robocopy source destination * /nocopy /purge
In fact, with robocopy you might be able to combine the copy and delete into one operation, depending on your requirements.

Robocopy is standard starting with Vista, and is available from Microsoft for XP as part of the Windows Server 2003 Resource Kit


Dave Benham

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: xcopy delete destination files not in source

#5 Post by tinfanide » 29 Oct 2011 09:31

Thanks for ya reply. I'm gonna study them as well. Yet,

While ya're replying to my question, I'm doing some research online and am half successful in doing what I wanna do

In my case now,

Source:
file1
file2

Destination:
file3

I want to copy file1 and file2 to Destination and delete all files excluding the original file in Destination
I use attrib +h to temporarily turn those files that I don't want to delete as hidden files and let the command delete the rest non-hidden file (file3)
and restore their attributes (from hidden to not hidden for file1 and file2)
But I'm stuck halfway thru in the loop

Code: Select all

@echo on

set src=c:\test
set dst=d:\test

xcopy %src% %dst%

for /F "TOKENS=*" %%G in ('dir/b ^%src%\*.*^') DO set new=%%G

attrib +h "%dst%\%new%"

del "%dst%"
attrib -h "%dst%\%new%"

pause


In CMD, I just see file2 (the last file) being set hidden instead of file1. I bet it's something to do with the for loop.

Although I tweak a bit of the thinking and kinda achieve what I wanted,

Code: Select all

@echo on

set src=c:\test
set dst=d:\test

attrib +h %src%\*

xcopy /h %src% %dst%
del "%dst%"

attrib -h %dst%\*

pause


But I still want for ya advice on the use of for loop and know the answer to why my first code doesn't work as expected.
Many thanks for ya response.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: xcopy delete destination files not in source

#6 Post by penpen » 30 Aug 2013 12:10

tinfanide wrote:In CMD, I just see file2 (the last file) being set hidden instead of file1. I bet it's something to do with the for loop.
Yes, you are storing the name of all the files indirectory %src% to the variable new, one after another, overwriting the prior setted filename.
And so the result is, that new contains the last filename that is stored to new.
Btw: There is a flaw with the second ^ i assume it should be leading the second %, or did you wanted to use doublequotes to let it work on directories with spaces?

Code: Select all

for /F "TOKENS=*" %%G in ('dir/b "%src%\*.*"') DO set new=%%G


I'm wondering why you don't use one of dbenham's methods.

And even more i'm wondering why you want to do it in such a complicated way.
Why not just reverse the order of operations:
First delete all filles in destination folder and then copy all files from source to destination folder.
This would be much easier, but the result should be the same:

Code: Select all

@echo on

set src=c:\test
set dst=d:\test

del /F /Q /A "%dst%\."
xcopy "%src%\*.*" "%dst%"

pause

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: xcopy delete destination files not in source

#7 Post by foxidrive » 31 Aug 2013 01:37

That post was 2 years old. Just sayin' :)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: xcopy delete destination files not in source

#8 Post by penpen » 31 Aug 2013 14:28

:shock: NOW most i'm wondering, what possessed me... :oops: Sorry for that.

TechnoMage
Posts: 4
Joined: 05 Jun 2013 18:22
Location: Central Florida

Re: xcopy delete destination files not in source

#9 Post by TechnoMage » 07 Sep 2013 06:50

Penpen, I've occasionally done exactly what you suggested, when my destination folders get clobbered up with OLD files.
Good suggestion!

I do essentially the same thing as the OP, but I am updating Flash Drives (at least 4) with info in my Utilities folder on my HD.
I take my Flash Drives with me on Service calls, so I need them to be up to date.

Sometimes that info changes daily with programs that constantly update themselves.
With no deletions, I wind up with several generations of a program on my Flash Drives.
That's not really BAD, but it just wastes space.

Finding no easy way to do the compare and delete thing, I just go into my flash drives, manually and delete all the old files.
It does take me a few minutes, but what the heck, I've nothing else to do anyway. I'm retired! :lol:

Automation is nice, but there's a lot to be said for "hands on" too.

Cheers Mates!
TechnoMage 8)

Denninger
Posts: 1
Joined: 28 Jan 2020 09:37

Re: xcopy delete destination files not in source

#10 Post by Denninger » 28 Jan 2020 11:55

Hello, this is an old post, I know :wink:

But I was looking for a solution doing with Xcopy what I normally do with the Robocopy /mir command. I had to since the ICT-guys at my work blocked Robocopy.
So, there are a couple of good suggestions here but I missed the selective deletion of extra files in a directory-tree as I don't like deleting and rewriting all files on my USB-stick.

So I came up with the following solution, using FOR, Xcopy and DEL to delete the extra files in the destination first and the Xcopying the newer files from the source to the destination if you want.
For detailed information see: Xcopy at Microsoft docs.

Simply copy the code below in Notepad and save it as: MirrorTo.cmd
Note: This example batch will not stop after you pressed the key to continue!

Code: Select all

@ECHO off
rem Syntaxis: MirrorTo.cmd <source> <destination>
CLS
ECHO Given Commandline: %~nx0 %*
rem - Set variables, including a quick check on the last two lines.
rem   Note: Using the ~ character here removes any qoutes from long filenames.
  SET str_SrcePath=%~1
  SET str_TrgtPath=%~2
  ECHO * Mirror the following directorys to each other.
  ECHO   Source      : "%str_SrcePath%"
  ECHO   Destination : "%str_TrgtPath%"
  IF "%str_TrgtPath%"=="" ECHO *** ERROR *** No destination directory given!&EXIT /b 3
  IF not exist "%str_SrcePath%\*.*" ECHO *** ERROR *** Source: '%1' is NOT a directory!&EXIT /b 3

ECHO Press any key to continue, Ctrl+C to quit.
  PAUSE > NUL

ECHO.
ECHO - Copy ONLY newer files from destination to the source.
rem   Note: If you DON'T want this simply remove this line OR replace /y with /-y (the /-y option will prompt you to confirm overwriting an existing file, you can also use this for testing).
  Xcopy "%str_TrgtPath%" "%str_SrcePath%" /d /u /r /v /y /h /i /s

ECHO.
ECHO - Delete ONLY extra files from destination.
rem   Note: You can replace 'DEL /q' with 'DEL /p' for testing.
  FOR /f "delims=*" %%F in ('Xcopy "%str_TrgtPath%" "%str_SrcePath%" /d /r /v /y /h /i /s /l') DO IF exist "%%~F" (DEL /q /f "%%~F") ELSE (ECHO %%F deleted.)

ECHO.
ECHO - Now copy only newer AND non-existing files from source to destination.
  Xcopy "%str_SrcePath%" "%str_TrgtPath%" /d /r /v /y /h /i /s

rem - Finally remove the previously created variables.
  SET str_SrcePath=
  SET str_TrgtPath=
EXIT /b 0
You can customize this code using options like Xcopy ... /l, then pause first etcetera but that will be to much here I guess.
I hope this is usable for more people, good luck!

Post Reply