繁体   English   中英

批处理文件从所有文件夹中删除所有匹配文件,然后将其保存在 Log.txt

[英]Batch File to Delete all Matching Files from All Folders then Save it in a Log.txt

我想创建一个批处理文件,该文件将删除具有任何文件格式的两个名称中的任何一个的文件,例如“新”或“1234”。

这是我的文件.txt:

new
1234

这就是我想要发生的事情:

我要删除的文件

我已经尝试了一些我在网上找到的代码,但它删除了我的一些文件。 因此,我害怕重新运行具有此类通配符的代码,呵呵。

到目前为止,我有这个:

SET FILENAME=DeleteLog.txt

SET USERLOG=%USERPROFILE%\%FILENAME%
rem I just declared the Downloads for safety-purposes but it should be C:\
SET TARGET1=%USERPROFILE%\Downloads

echo STARTING THE FILE DELETION >>%USERLOG%

cd %TARGET1%
rem file.txt contains the string (new,1234)
for /f "delims=" %%a in ("file.txt") do echo del %TARGET1%*%%a* >>%USERLOG%

echo FINISHING THE FILE DELETION >>%USERLOG%

我的逻辑是:对于我的 C:\ Drive -> Delete -> Log 上的每个文件(new,1234).txt

我正在尝试遵循这一点,但它对我不起作用: 批处理文件删除文本列表中包含的相对名称的文件? 似乎是什么问题?

任何提示和建议将不胜感激:)

您可以尝试使用此批处理脚本,如果可以,您可以删除ECHO(Del命令之前:


@echo off
Title Delete some files with pattern *123* or *new*
Set "CurrentFolder=%~dp0"
Set "Pattern=*123* *new*"
cd /d "%CurrentFolder%"
::---------------------------------------------------------------------------------
:Main
cls
setlocal enableDelayedExpansion
REM Count total files and store into array
set /a "Count=0"
@for %%f in (%Pattern%) do (
    set /a "Count+=1"
    set "list[!Count!]=%%f"
)
REM disply files
@for /L %%a in (1,1,%Count%) do (
    echo( [%%a] - "!list[%%a]!"
)
Echo( -----------------------------------------------------------------------------------------
Echo( Total pattern files found are equal to %Count% on this folder : "%CurrentFolder%"
Echo( -----------------------------------------------------------------------------------------
echo(
If [%Count%] EQU [0] color 0C & echo( There are No Files that matches this %Pattern% in this Folder "%CurrentFolder%" & TimeOut /T 10 /NoBreak>nul & Exit /B 1
echo( Type "DELAll" in order to delete all files found here "%CurrentFolder%" with this "%Pattern%" ...
echo( Or Type only the number of file that you want to delete ...

set /p "Input="

@For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Cls & Call :DeleteFile "!list[%%i]!"
    )
    
    If /I "%Input%" == "Delall" (
        cls & Call :DeleteFile "!list[%%i]!"
    )
)
EndLocal & Goto Main
::---------------------------------------------------------------------------------
:DeleteFile <FilePath>
echo( ----------------------------------
Echo( Deleting "%~nx1"
ECHO( Del "%~1"
echo( ----------------------------------
Timeout /T 1 /NoBreak>nul
Exit /b
::----------------------------------------------------------------------------------

暂无
暂无

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

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