PC Review


Reply
Thread Tools Rate Thread

Deleting files from one directory that are not present in another

 
 
PhilHibbs
Guest
Posts: n/a
 
      5th Dec 2008
I'm trying to sync one directory with another. I've got an xcopy
command that makes sure that files on the source are present on the
target, but I also want to delete files on the target that are not
present on the source.

Currently my command looks like this:

for /R Z:\test1 %F in (*.*) do if not exist "X:%~pnxF" del "%F"

This works fine when I run it on the command prompt directly, but
doesn't work in a cmd file. I seem to remember something about having
to double up the % but I can't remember the details, I tried this in
various places but can't make it work. Any ideas?

Phil Hibbs.
 
Reply With Quote
 
 
 
 
Lutz Kruse
Guest
Posts: n/a
 
      5th Dec 2008
PhilHibbs schrieb:
> I'm trying to sync one directory with another. I've got an xcopy
> command that makes sure that files on the source are present on the
> target, but I also want to delete files on the target that are not
> present on the source.
>
> Currently my command looks like this:
>
> for /R Z:\test1 %F in (*.*) do if not exist "X:%~pnxF" del "%F"
>
> This works fine when I run it on the command prompt directly, but
> doesn't work in a cmd file. I seem to remember something about having
> to double up the % but I can't remember the details, I tried this in
> various places but can't make it work. Any ideas?
>
> Phil Hibbs.


Hi Phil,

you should take a look at robocopy from Windows 2003 Reskit. There is a
switch /MIR (Mirror).

Lutz
 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      5th Dec 2008

"PhilHibbs" <(E-Mail Removed)> wrote in message
news:a57cc660-82ad-4f2e-bea3-(E-Mail Removed)...
> I'm trying to sync one directory with another. I've got an xcopy
> command that makes sure that files on the source are present on the
> target, but I also want to delete files on the target that are not
> present on the source.
>
> Currently my command looks like this:
>
> for /R Z:\test1 %F in (*.*) do if not exist "X:%~pnxF" del "%F"
>
> This works fine when I run it on the command prompt directly, but
> doesn't work in a cmd file. I seem to remember something about having
> to double up the % but I can't remember the details, I tried this in
> various places but can't make it work. Any ideas?
>
> Phil Hibbs.


As you say, you must double your % characters when referring
to enumerating variables in batch files.

Robocopy.exe will do exactly what you want. Here is the relevant
line from its help file:
/PURGE :: delete dest files/dirs that no longer exist in source.
You can get it from
http://www.microsoft.com/downloads/d...displaylang=en.


 
Reply With Quote
 
PhilHibbs
Guest
Posts: n/a
 
      5th Dec 2008
Pegasus wrote:
> > This works fine when I run it on the command prompt directly, but
> > doesn't work in a cmd file. I seem to remember something about having
> > to double up the % but I can't remember the details, I tried this in
> > various places but can't make it work. Any ideas?

>
> As you say, you must double your % characters when referring
> to enumerating variables in batch files.


As I say, I tried that and it didn't work. Where do I need to double
it up? "%%F in", or "X:%%~pnxF"? Whichever I try, I get this:

The following usage of the path operator in batch-parameter
substitution is invalid: %~pnxF" del "%F"

Phil Hibbs.
 
Reply With Quote
 
PhilHibbs
Guest
Posts: n/a
 
      5th Dec 2008
Lutz Kruse wrote:
> you should take a look at robocopy from Windows 2003 Reskit. There is a
> switch /MIR (Mirror).


That looks like exactly what I want, thanks. I'm still curious as to
why my command wouldn't work in a batch file though.

Phil Hibbs.
 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      5th Dec 2008

"PhilHibbs" <(E-Mail Removed)> wrote in message
news:a23bcba7-c5aa-4038-9e9d-(E-Mail Removed)...
> Pegasus wrote:
>> > This works fine when I run it on the command prompt directly, but
>> > doesn't work in a cmd file. I seem to remember something about having
>> > to double up the % but I can't remember the details, I tried this in
>> > various places but can't make it work. Any ideas?

>>
>> As you say, you must double your % characters when referring
>> to enumerating variables in batch files.

>
> As I say, I tried that and it didn't work. Where do I need to double
> it up? "%%F in", or "X:%%~pnxF"? Whichever I try, I get this:
>
> The following usage of the path operator in batch-parameter
> substitution is invalid: %~pnxF" del "%F"
>
> Phil Hibbs.


I had no problem at all with the following batch file, which is based on
yours:
@echo off
for /R D:\Fri %%F in (*.*) do if not exist "E:%%~pnxF" del "%%F"

I note that the error message refers to the string
%~pnxF" del "%F"

Since the string includes the "del" command I suspect that you ran afoul of
some dreaded poison character. In other words, one of your file names
contains a character out of the following set:
'"`%^&()|<>
You can easily find out: Turn on your Echo in the batch file, then watch
which file name the program trips over. Alternatively you could run this
modified batch file:
@echo off
for /R D:\Fri %%F in (*.*) do (
echo File name = xx%%Fzz
if not exist "E:%%~pnxF" del "%%F"
)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files and folders in the C:\Windows directory B Windows XP General 3 12th Aug 2010 04:14 PM
Creating and Deleting files from Virtual Directory ankit.sri Microsoft C# .NET 1 12th Jun 2008 07:55 AM
Deleting files in a directory =?Utf-8?B?U2Ft?= Microsoft Excel Programming 3 8th Jun 2007 06:41 PM
Deleting directory path and files. VB.net 2003 Kimera.Kimera@gmail.com Microsoft VB .NET 3 30th Dec 2006 02:13 AM
deleting files from web server directory Simon Microsoft ASP .NET 1 10th Nov 2005 01:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:06 AM.