Deleting files from one directory that are not present in another

P

PhilHibbs

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.
 
L

Lutz Kruse

PhilHibbs said:
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
 
P

Pegasus \(MVP\)

PhilHibbs said:
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/...69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en.
 
P

PhilHibbs

Pegasus said:
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.
 
P

PhilHibbs

Lutz said:
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.
 
P

Pegasus \(MVP\)

PhilHibbs said:
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"
)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top