简体   繁体   English

如何使用批处理脚本从“输出”文件夹中删除所有文件?

[英]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.

相关问题 使用ftp批处理脚本删除FTP中文件夹中的所有文件 - delete all files in a folder at FTP using ftp batch script 如何使用批处理删除FTP文件夹中的所有文件 - How to delete all files of a FTP folder using batch 批处理-删除文件夹和所有文件 - Batch - Delete folder and all files 使用批处理删除文件夹中除列表中的文件以外的所有文件 - Delete all files in folder except file in list using batch 使用批处理脚本将所有文件从所有子文件夹移动到父文件夹 - Move All Files from all Subfolders to Parent Folder using batch script 批处理文件以删除该文件夹中3天之前的所有文件 - batch file to delete all files from the folder which are 3 days older 批处理文件以计算文件夹中的所有文件和子文件夹文件,并使用批处理文件以文件夹名称写入输出文件 - batch file to count all files in a folder and subfolder files and write output file in folder name using batch file 如何删除批处理文件以及文件夹中的所有其他文件? - How to delete a batch file along with all other files in the folder? 使用批处理或从批处理运行的Powershell重命名文件夹中的所有文件 - Rename all files in folder using batch or powershell run from batch 批处理:如何为文件夹中的所有文件运行程序以及如何使用原始文件名为结果创建输出文件? - Batch : how to run a program for all files in a folder and how to create an output file for the result using the original filename?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM