[英]How to delete all files from “Output” folder using batch script?
I have a folder structure as below: 我的文件夹结构如下:
L:\MyProject\Extract\
Activity Input
**output**
SSIS
Results
Headers
Account Input
**output**
SSIS
Results
Headers
Balance Input
**output**
SSIS
Results
.
.
.
Here, I need to find output folder and delete all files present inside each "output" folder. 在这里,我需要找到输出文件夹并删除每个“输出”文件夹中存在的所有文件。 I am using following script:
我正在使用以下脚本:
SET FolderPath=%1
for /f "usebackq" %%a in (`"dir %FolderPath% /ad/b/s output"`) do SET Folder="%%a"
del %Folder%\*.* /Q
But using above script, I am able to delete files which are present in "%%a"(last path of output folder). 但是使用上面的脚本,我可以删除“ %% a”(输出文件夹的最后路径)中存在的文件。 How to do it recursively...?
如何递归执行...? Or is there any array concept to store all paths where output folder is present?
还是有任何数组概念来存储存在输出文件夹的所有路径?
I see three problems in your code. 我在您的代码中看到了三个问题。
First, the for
loop does not find the output
subdirs under FolderPath
, but iterates over all the subdirs of FolderPath
. 第一,
for
循环没有找到output
下的子目录FolderPath
过的所有子目录,但迭代FolderPath
。
One way to solve this is to specify 解决此问题的一种方法是指定
for /f "usebackq" %%a in (`dir "%FolderPath%\output" /ad/b/s`) do
Note that I have moved the quotes, just to prepare for the case of names with spaces, which was your second problem. 请注意,我已经移了引号,只是为了准备带空格的名称,这是您的第二个问题。 You might further simplify it with
您可能会进一步简化它
for /f "usebackq delims=" %%a in (`dir "%~1\output" /ad/b/s`) do
note that delims
avoids spaces work as delimiters and %~1
removes the quotes of the parameter 请注意,
delims
避免空格作为分隔符, %~1
删除参数的引号
And finally, the for
loop, in each iteration, just set
s the Folder
variable, it does not invoke the del
command, that is placed outside the loop. 最后,在每次迭代中,
for
循环仅set
Folder
变量,它不会调用放置在循环外部的del
命令。
One easy way to solve this problem is to invoke the del
command on %%a
without setting the variable folder
解决此问题的一种简单方法是在
%%a
上调用del
命令,而无需设置变量folder
for /f "usebackq delims=" %%a in (`dir "%~1\output" /ad/b/s`) do del %%a\* /q
You could use the find
command to find all subdirectories of the current directory. 您可以使用
find
命令查找当前目录的所有子目录。
To list files in all directories named Output
: 要列出所有名为
Output
目录中的文件:
find . -name Output -type d|find -type f -exec ls {} \;
To delete all files in directories named Output
: 要删除名为
Output
目录中的所有文件:
find . -name Output -type d|find -type f -exec rm {} \;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.