xcopy delete destination files not in source
Moderator: DosItHelp
xcopy delete destination files not in source
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?
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?
Re: xcopy delete destination files not in source
'
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:
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 !
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 !
Re: xcopy delete destination files not in source
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?
Re: xcopy delete destination files not in source
FOR /F will most likely require some form of 'DIR' command within the IN clause. A simple FOR would be easier and more efficientEd Dyreen wrote:Actually you don't even need dir, you can also use for /f !
@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
Robocopy is standard starting with Vista, and is available from Microsoft for XP as part of the Windows Server 2003 Resource Kit
Dave Benham
Re: xcopy delete destination files not in source
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
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,
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.
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.
Re: xcopy delete destination files not in source
Yes, you are storing the name of all the files indirectory %src% to the variable new, one after another, overwriting the prior setted filename.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.
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
Re: xcopy delete destination files not in source
That post was 2 years old. Just sayin'
Re: xcopy delete destination files not in source
NOW most i'm wondering, what possessed me... Sorry for that.
-
- Posts: 4
- Joined: 05 Jun 2013 18:22
- Location: Central Florida
Re: xcopy delete destination files not in source
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!
Automation is nice, but there's a lot to be said for "hands on" too.
Cheers Mates!
TechnoMage
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!
Automation is nice, but there's a lot to be said for "hands on" too.
Cheers Mates!
TechnoMage
Re: xcopy delete destination files not in source
Hello, this is an old post, I know
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!
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!
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
I hope this is usable for more people, good luck!