簡體   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