问题
I'm trying to delete some files in a WinRar Archive using the Command Line.
The Rar-File:
/testing.rar
/testing.rar/some-data.txt
/testing.rar/testing/some-data.txt
Here's my Code:
cd "C:\Program Files\WinRAR\" && rar d -cl -r "c:\full\path\testing.rar" some-data.txt
It only deletes the some-data.txt file in the root, not inside /testing/
When using
cd "C:\Program Files\WinRAR\" && rar d -cl -r "c:\full\path\testing.rar" some-data.*
(changed the extension to .*) it does delete both files.
Am I doing something wrong?
Thanks, André.
回答1:
While the d
command can not handle it, a simple pipe can deal with it
@echo off
setlocal enableextensions disabledelayedexpansion
set "rar=C:\Program Files\WinRar\rar.exe"
set "archive=c:\full\path\testing.rar"
(
%= List archive contents =%
"%rar%" lb -ed "%archive%"
)|(
%= filter the list for the file in any subfolder =%
findstr /i /e /l /c:"\somedata.txt"
%= and include the root file =%
echo somedata.txt
)|(
%= Delete from archive the list of files read from stdin =%
"%rar%" d -cl -n@ "%archive%"
)
The second step (filter the list of files in archive) is splited in the findstr
and the echo
just to prevent the case when the file to be deleted is not present in the output. Without a list of files the -n@
modifier (read files to delete from stdin
) will not read anything and all the archive contents will be removed.
回答2:
I looked on first version of great solution of MC ND and thought by myself what happens if there is by chance although very unlikely a directory with name some-data.txt
inside the archive?
The answer is that this directory with all files and subdirectories is also deleted because of also listed by Rar.exe
and findstr
does not filter out the list entry of the directory as it looks like exactly like a file name entry in bare list format.
The solution for code written by MC ND is to use the Rar command lb
with switch -ed
to filter out directory entries on list output. This small modification was applied to the command line by MC ND in his answer.
Then I thought about using the switches -ed
and -x"..."
to filter out directories and file names which should not be deleted on running Rar.exe
with command d
and a some-data.txt
wildcard pattern.
And here is the single line solution:
"C:\Program Files\WinRAR\Rar.exe" d -ed -x"some-data.txt?*" -- "C:\full\path\testing.rar" "some-data.txt*"
The asterisk at end of file name is necessary to delete all files with name some-data.txt
inside the archive including the files in subdirectories.
The switch -ed
excludes all directories in archive including those which by chance are named some-data.txt
. Directories in archive starting with some-data.txt
and with more characters appended like a directory with name some-data.txt_dir
are automatically ignored by Rar.exe
with used file name wildcard pattern.
The switch -x"some-data.txt?*"
results in excluding files starting with some-data.txt
and having more characters appended like some-data.txt1
and some-data.txt10
. File names like first_some-data.txt
are automatically ignored by Rar.exe
with used file name wildcard pattern.
The switch -cl
to convert file names to lower case is ignored by Rar on using command d
and therefore not used in the command line above. Rar interprets the file names case-insensitive like Windows.
Update:
Eugene Roshal, owner of win.rar GmbH, told me by email an easier method to delete a file in root archive folder and in all subfolders:
"C:\Program Files\WinRAR\Rar.exe" d "C:\full\path\testing.rar" "some-data.txt" "*\some-data.txt"
This command line deletes only some-data.txt
in root archive folder (last but one argument) and in all its subfolders (last argument). Files in a folder with name some-data.txt
are not deleted by this command line, but an empty folder with name some-data.txt
would be also removed from archive. The switch -ed
can be used additionally to prevent deletion of an empty folder some-data.txt
.
来源:https://stackoverflow.com/questions/52517671/winrar-command-line-not-deleting-recursive-when-not-using-wildcards