Regex 用于搜索要删除的特定文件的Windows批处理脚本
我需要帮助将以下Linux/Mac命令转换为Windows批处理脚本Regex 用于搜索要删除的特定文件的Windows批处理脚本,regex,windows,batch-file,Regex,Windows,Batch File,我需要帮助将以下Linux/Mac命令转换为Windows批处理脚本 find / -regex ``^.*/Excel_[a-z]*.xls'' -delete 2>/dev/null find / -regex ``^.*/presentation[0-9]*[a-z]*.ppt'' -delete 2>/dev/null 基本上使用正则表达式,我需要在给定的Windows框中找到任何.xls/.ppt文件(采用上述格式),然后将其删除 抱歉,我不熟悉Windows批处理文件。
find / -regex ``^.*/Excel_[a-z]*.xls'' -delete 2>/dev/null
find / -regex ``^.*/presentation[0-9]*[a-z]*.ppt'' -delete 2>/dev/null
基本上使用正则表达式,我需要在给定的Windows框中找到任何.xls/.ppt文件(采用上述格式),然后将其删除
抱歉,我不熟悉Windows批处理文件。普通批处理无法完成此任务。但是你可以使用像findstr这样的工具,它是Windows附带的并且支持正则表达式 该行可以从CMD执行,并删除当前文件夹中与正则表达式匹配的所有文件:
for /f "eol=: delims=" %F in ('findstr /r "MY_REGEX_HERE"') do del "%F"
因此,尝试通过使用此命令来获得预期的结果。如果您对输出/结果满意,可以将此行嵌入批处理脚本中。(小心,在batchscript中嵌入此行时,必须将百分号加倍!)
使用PowerShell
get-childitem | where-object { $_.Name -match '<put a regex here>' } | remove-item
你真的没有解释你的象形文字是什么意思 在批处理文件中
@echo off
setlocal
dir /s "c:\local root\Excel_*.xls"
将显示与扩展名为.xls
(如果这是您想要的,只需将dir
替换为del
即可删除它们;添加>nul
将抑制消息;添加2>nul
将抑制错误消息)
如果希望文件以Excel\uu
开头,然后后跟任何字母,则
for /f "delims=" %%a in ('dir /b /s /a-d Excel_*.xls ^| findstr /E /R "\\Excel_[a-z]*\.xls" ') do echo "%%a"
dir
以/b
(基本)形式(即仅文件名)/s
-生成一个目录列表,其中包含子目录(附加完整目录名),而/a-d
则不包含目录名。这通过管道传输到findstr
,以过滤出所需的文件名。结果被逐行分配给%%a
,delims=
表示“不标记数据”
应该向您显示符合该标准的所有文件-但它将区分大小写;将/i
添加到findstr
开关,使其不区分大小写。(/r表示findstr
的受限实现中的“regex”;/e表示“ends”-我倾向于在$
中使用它们)中的\
旨在实现转义\
以确保文件名匹配完整(即未找到“某些前缀xcel\u where.xls”)-我将留下\.
打算对你的想象力做的事情
(同样,将echo
更改为del
以删除并在需要时添加重定向命令)
另一个正则表达式-好的,跟着弹跳球。它看起来像是你想要.ppt
文件,文件名以presentation
开头,后面可能是一系列数字,然后是一系列字母。我建议
findstr /e /r "\\presentation[0-9]*[a-z]*\.ppt" for that task.
这里有一小部分内容可以满足您的需求:
@echo off
set /p dirpath=Where are your files ?
:: set dirpath=%~dp0 :: if you want to use the directory where the batch file is
set /p pattern=Which pattern do you wanna search (use regex: *.xml e.g.) :
:: combinason /s /b for fullpath+filename, /b for filename
for /f %%A in ('dir /s /b "%dirpath%\%pattern%" ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%
call :MsgBox "Do you want to delete all %pattern% in %dirpath%? %cnt% files found" "VBYesNo+VBQuestion" "Click yes to delete the %pattern%"
if errorlevel 7 (
echo NO - quit the batch file
) else if errorlevel 6 (
echo YES - delete the files
:: set you regex, %%i in batch file, % in cmd
for /f "delims=" %%a in ('dir /s /b "%dirpath%\%pattern%"') do del "%%a"
)
:: avoid little window to popup
exit /b
:: VBS code for the yesNo popup
:MsgBox prompt type title
setlocal enableextensions
set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
>"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
endlocal & exit /b %exitCode%
这是最初的想法,但我们遇到了“数字签名问题”。当您打开PowerShell命令窗口并键入上述命令时,不会出现数字签名问题。通过适当设置执行策略,可以轻松启用脚本。必须使用“-force Unrestricted”“。非常感谢@Bill_Stewartit的
-force
,而是-executionpolicy
。您可以通过设置一个合理的执行策略来解决这个问题,您可以在组策略中设置。由于用户是新用户…关于为什么答案有效的解释将是有用的。对于/f“eol=:delims=“%f in('findstr.*\Excel_[a-z]*.xls')do del“%f”似乎不起作用。findstr是否以不同的格式处理正则表达式?抱歉@joeguy555-我的错。我忘记了RegEx的/r标志。支持的元字符列表可以在这里找到:我还认为,因为我有一个未替换的正则表达式元字符“*”,它会导致问题吗?无论如何,非常感谢您的帮助和快速回复@netblognet,非常感谢!还请注意,bacth中的大多数特殊字符都是用插入符号(^)转义的,而不是反斜杠()。它正是我所需要的!我对findstr中的表达式有问题。我很欣赏添加的递归遍历所有子文件夹的方法。感谢您的详细解释,并花时间解释解决方案中的所有步骤。学识渊博!如果使用PowerShell,则无需对/f或findstr
进行或字符串解析。
findstr /e /r "\\presentation[0-9]*[a-z]*\.ppt" for that task.
@echo off
set /p dirpath=Where are your files ?
:: set dirpath=%~dp0 :: if you want to use the directory where the batch file is
set /p pattern=Which pattern do you wanna search (use regex: *.xml e.g.) :
:: combinason /s /b for fullpath+filename, /b for filename
for /f %%A in ('dir /s /b "%dirpath%\%pattern%" ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%
call :MsgBox "Do you want to delete all %pattern% in %dirpath%? %cnt% files found" "VBYesNo+VBQuestion" "Click yes to delete the %pattern%"
if errorlevel 7 (
echo NO - quit the batch file
) else if errorlevel 6 (
echo YES - delete the files
:: set you regex, %%i in batch file, % in cmd
for /f "delims=" %%a in ('dir /s /b "%dirpath%\%pattern%"') do del "%%a"
)
:: avoid little window to popup
exit /b
:: VBS code for the yesNo popup
:MsgBox prompt type title
setlocal enableextensions
set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
>"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
endlocal & exit /b %exitCode%