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. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|