简体   繁体   English

批量删除路径中带有通配符的文件夹中的所有文件

[英]Batch to delete all files in a folder with a wildcard in the path

So what I'm trying to do is go into every folder in the following directory 所以我想做的是进入以下目录中的每个文件夹

"C:\Documents and Settings\"

and for every folder in it, regardless of the name, check if this path exists 对于其中的每个文件夹,无论其名称如何,请检查此路径是否存在

"C:\Documents and Settings\*\Local Settings\Application Data\CSMRpt\"

if it exists then delete all txt files inside that director, if the path doesn't exists then do nothing and move on to the next folder inside "C:\\Documents and Settings\\" 如果存在,则删除该目录中的所有txt文件;如果该路径不存在,则不执行任何操作,然后移至“ C:\\ Documents and Settings \\”中的下一个文件夹

This is what I came up with so far: 到目前为止,这是我想出的:

set PATH = "\Local Settings\Application Data\CSMRpt\"
set FILETYPE = "*.txt"
for /d %%g in ("C:\Documents and Settings\*") do if exist %%g%PATH% goto pathexists
:pathexists
del %%g%PATH%%FILETYPE%

Try this. 尝试这个。

@echo off
setlocal
set cwd=%CD%
set p=Local Settings\Application Data\CSMRpt
cd /d "c:\Documents and Settings\"
for /d %%I in (*) do (
    if exist "%%I\%p%\" (
        pushd "%%I\%p%\"
        del /q *.txt
        popd
    )
)
:: (change back to original directory)
cd /d "%cwd%"

A couple things wrong here, you can't have spaces around the = in the set command, using goto wouldn't have passed the variable (you could however use call instead and pass it as a argument) you don't need quotes around every variable, %PATH% although you can reset it, you shouldn't for something like this as it is a environment variable. 这里有几处错误,您不能在set命令中的=周围有空格,使用goto不会传递变量(但是您可以使用call并将其作为参数传递),您不需要在引号周围尽管每个变量%PATH%都可以重置,但您不应对其进行设置,因为它是环境变量。

Corrected code: 更正的代码:

set THEPATH=\Local Settings\Application Data\CSMRpt\
set FILETYPE=*.txt
for /d %%g in ("C:\Documents and Settings\*") do if exist "%%g%THEPATH%." del "%%g%THEPATH%%FILETYPE%"

If you really didn't want the for loop to be one line you could do this as well 如果您真的不希望for循环成为一行,那么也可以这样做

set THEPATH=\Local Settings\Application Data\CSMRpt\
set FILETYPE=*.txt
for /d %%g in ("C:\Documents and Settings\*") do if exist "%%g%THEPATH%." call :deltxtfiles "%%~g"

exit /B

:deltxtfiles
del "%~1%THEPATH%%FILETYPE%"
goto:eof

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM