6 Replies - 5916 Views - Last Post: 09 January 2012 - 02:21 AM Rate Topic: -----

#1 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Delete files in destination that don't exist in source MS-DOS

Posted 30 November 2011 - 07:27 AM

I'm using xcopy to copy across a fairly large file structure from A to B. Whenever I run the batch script I have (see below), it copies all the files from A to B that are newer than the destination files. Trouble is, when I delete a file in A, it does not delete the file in B when I run the script. I would like for this to happen.

xcopy "C:\a" "C:\b" /d /e /y

There is a command called /u that copies files from Source that exist on Destination only. I want the total opposite of this, where you delete files from Destination where they do not exist on Source.

Basically, I want A and B to be exact copies after I run the script without having to actually delete B and copy everything.

Any tips?

This post has been edited by MrSooty: 30 November 2011 - 07:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Delete files in destination that don't exist in source MS-DOS

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,910
  • Joined: 02-June 10

Re: Delete files in destination that don't exist in source MS-DOS

Posted 30 November 2011 - 07:52 AM

You're talking about synchronization. And its not going to be just one command.

You need to loop through all your source files, compare them to your destination files, and determine what is left on the destination that isn't on the source, then issue a delete for each one.

You need to write code logic, not just issue a command that already exists.
Was This Post Helpful? 1
  • +
  • -

#3 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: Delete files in destination that don't exist in source MS-DOS

Posted 30 November 2011 - 07:56 AM

Makes sense, means I can stop wasting my time looking for a pre-existing solution then and get cracking. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: Delete files in destination that don't exist in source MS-DOS

Posted 30 November 2011 - 09:14 AM

Ok I've tried looping through one of the files and checking if the file is equal to xasdas.txt, which I know is there, and if it finds it, print out the file name.

FOR /R C:/b %%i IN (*.*) DO (
 IF /I %%nxi EQU XASDAS.TXT (
	 ECHO %%nxi
 )
)


The output I am getting is hundreds of lines of the if loop repeated constantly, and I mean it is literally being printed out as you see it:
Spoiler


Even if I place an echo before the IF, it actually prints echo on screen too rather than actually doing it, so I feel as if I'm using something wrong... :(

Just FYI this is my first time really using batch files/MS-DOS properly, so bear with me :)

Any tips?

This post has been edited by MrSooty: 30 November 2011 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,910
  • Joined: 02-June 10

Re: Delete files in destination that don't exist in source MS-DOS

Posted 30 November 2011 - 09:34 AM

I'm not help to you with batch files. Sorry.
I would just write a little application in C# to do this.
Was This Post Helpful? 0
  • +
  • -

#6 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: Delete files in destination that don't exist in source MS-DOS

Posted 30 November 2011 - 09:35 AM

I would too but now I'm committed! Thanks anyway :)
Was This Post Helpful? 0
  • +
  • -

#7 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: Delete files in destination that don't exist in source MS-DOS

Posted 09 January 2012 - 02:21 AM

Slight necro, but the solution is to use Robocopy!

http://en.wikipedia.org/wiki/Robocopy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1