A Windows XP help forum. PCbanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » PCbanter forum » Microsoft Windows XP » General XP issues or comments
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Deleting files from one directory that are not present in another



 
 
Thread Tools Display Modes
  #1  
Old December 5th 08, 01:38 PM posted to microsoft.public.windowsxp.general
PhilHibbs
external usenet poster
 
Posts: 7
Default Deleting files from one directory that are not present in another

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.
Ads
  #2  
Old December 5th 08, 03:08 PM posted to microsoft.public.windowsxp.general
Lutz Kruse
external usenet poster
 
Posts: 1
Default Deleting files from one directory that are not present in another

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
  #3  
Old December 5th 08, 03:10 PM posted to microsoft.public.windowsxp.general
Pegasus \(MVP\)[_2665_]
external usenet poster
 
Posts: 1
Default Deleting files from one directory that are not present in another


"PhilHibbs" wrote in message
...
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.


  #4  
Old December 5th 08, 04:00 PM posted to microsoft.public.windowsxp.general
PhilHibbs
external usenet poster
 
Posts: 7
Default Deleting files from one directory that are not present in another

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.
  #5  
Old December 5th 08, 04:19 PM posted to microsoft.public.windowsxp.general
PhilHibbs
external usenet poster
 
Posts: 7
Default Deleting files from one directory that are not present in another

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.
  #6  
Old December 5th 08, 04:34 PM posted to microsoft.public.windowsxp.general
Pegasus \(MVP\)[_2666_]
external usenet poster
 
Posts: 1
Default Deleting files from one directory that are not present in another


"PhilHibbs" wrote in message
...
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"
)


 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off






All times are GMT +1. The time now is 12:17 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 PCbanter.
The comments are property of their posters.