批处理命令从一个文件夹中删除除一个文件外的所有内容(子文件夹和文件) [英] Batch command to delete everything (sub folders and files) from a folder except one file
问题描述
首先,过去已经回答了类似的问题,但并非完全是我所回答的问题.在其他一些解决方案中,建议隐藏文件夹/文件和更改属性,除非没有更简单的方法,否则我不希望这样做.另外,我尝试了这里(和其他几个)建议的解决方案:
First of all, similar questions have been answered in past but not exactly the one I have. In some other solutions, hiding folders/files and changing attributes were suggested and I do not want this, unless there is no simpler way available. Also, I have tried the solution suggested here (and couple of others): MS-DOS command to delete all files except one.
但是由于两个原因无法满足我的需求:
But did not work for my need due to two reasons:
- 我也不想删除文件.
- 这不会删除子文件夹,而只会删除文件.
因此,我有文件夹c:/users/data
,在其中有5个文件夹和6个文件,我想删除除web.config
以外的所有内容,并按如下方式运行批处理文件:
So, I have folder c:/users/data
and in there, I have 5 folders and 6 files I want to delete everything except one which is web.config
and to do that I run the batch file like this:
@echo off
for %%j in (C:\Users\data\*) do if not %%j == Web.config del %%j
pause
但是当我运行批处理文件时,这将删除包括web.config
在内的所有文件,并且也不会删除任何子文件夹,如果使用/d
开关,则只会删除文件夹而不是文件.如何删除文件和文件夹?
But when I run the batch file, this deletes all the files including web.config
and also, does not delete any of the sub-folders and if I use the /d
switch then it only deletes folders not files. How can I delete both files and folders?
推荐答案
这是我的处理方式:
pushd "C:\Users\data" || exit /B 1
for /D %%D in ("*") do (
rd /S /Q "%%~D"
)
for %%F in ("*") do (
if /I not "%%~nxF"=="web.config" del "%%~F"
)
popd
基本代码摘自我对问题删除文件夹内容(而不是文件夹)的答案,但使用del
命令替换为第二个for
循环,该循环遍历所有文件并仅删除未命名为web.config
的那些文件.
The basic code is taken from my answer to the question Deleting Folder Contents but not the folder but with the del
command replaced by a second for
loop that walks through all files and deletes only those not named web.config
.
请注意,此方法不会触摸原始的父目录,也不会触摸原始文件web.config
.这个事实的最大优势在于,不会丢失任何属性(例如,创建日期和所有者).
Note that this approach does not touch the original parent directory, neither does it touch the original file web.config
. The great advantage of this fact is that no attributes are going to be lost (for instance, creation date and owner).
- 通过
pushd
更改为根目录;如果失败,请跳过脚本的其余部分; - 通过
for /D
循环遍历根的所有直接子文件夹,并通过rd /S
递归删除它们的所有内容; - 通过标准的
for
循环遍历位于根目录中的文件;使用循环变量的~nx
修饰符扩展每个文件的基本名称(n
)和扩展名(x
),以不区分大小写的方式将其与给定的文件名(if
)比较(/I
),并仅在不匹配的情况下通过del
删除文件(not
); - 最后通过
popd
恢复以前的工作目录;
- change to root directory by
pushd
; in case of failure, skip the rest of the script; - iterate through all the immediate sub-folders of the root by a
for /D
loop and delete them recursively byrd /S
with all their contents; - iterate through the files located in the root by a standard
for
loop; use the~nx
modifier of the loop variable to expand base name (n
) and extension (x
) of each file, compare it with the given file name (if
) in a case-insensitive manner (/I
) and delete the file bydel
only in case it does not match (not
); - restore former working directory by
popd
finally;
您的代码中的主要问题是:
The main problems in your code are:
- 您将纯文件名与预设名称进行了比较,因此不会找到任何匹配项.
- 您需要使用不区分大小写的比较,因为Windows并不关心搜索文件名的大小写.
- 您没有在比较表达式两边加上引号,因此您可能会收到语法错误消息,具体取决于文件名中出现的特殊字符.
- 您仅使用
del
命令删除文件;要删除目录,还需要使用rd
.
- that you compare more that the pure file name with the prefedined name, so there is not going to be found any match.
- you needed to use case-insensitive comparison, because Windows does not care about the case of file names for searching.
- you did not put quotation marks around the comparison expressions, so you are likely going to receive syntax error messages, depending on what special characters occur in the file names.
- that you are using the
del
command only which just deletes files; to remove directories, you needed to userd
also.
这篇关于批处理命令从一个文件夹中删除除一个文件外的所有内容(子文件夹和文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!