简体   繁体   English

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

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

I want to create a Batch File that will delete a File that has any of the two names such as "new" or "1234" of any file format.我想创建一个批处理文件,该文件将删除具有任何文件格式的两个名称中的任何一个的文件,例如“新”或“1234”。

this is my file.txt:这是我的文件.txt:

new
1234

this is what I want to happen:这就是我想要发生的事情:

我要删除的文件

I already tried some of the codes I found online but It deleted some of my files.我已经尝试了一些我在网上找到的代码,但它删除了我的一些文件。 So I'm scared to re-run codes that has such WildCards hehe.因此,我害怕重新运行具有此类通配符的代码,呵呵。

So far I have this:到目前为止,我有这个:

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%

My Logic is: For every file(new,1234).txt on my C:\ Drive -> Delete -> Log我的逻辑是:对于我的 C:\ Drive -> Delete -> Log 上的每个文件(new,1234).txt

I'm trying to follow this but it does not work for me: Batch file to delete files with relative name contained in text list?我正在尝试遵循这一点,但它对我不起作用: 批处理文件删除文本列表中包含的相对名称的文件? What seems to be the problem?似乎是什么问题?

Any tips and suggestion will be appreciated:)任何提示和建议将不胜感激:)

You can give a try for this batch script and if it's OK you can remove the ECHO( before Del command:您可以尝试使用此批处理脚本,如果可以,您可以删除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.

相关问题 批处理文件以列出其他目录中的所有txt文件 - batch file to list all txt files from other directories 批处理文件以删除文件夹中的文件 - Batch File to Delete Files in Folders 如何使用Windows Batch Scripting从文件夹“A”中删除文件夹“A”中的所有文件/文件夹? - How to delete all files/folders from a folder 'A' which are not present in the folder 'B', using Windows Batch Scripting? 批处理文件从多个文件夹中的多个txt文件中提取文本 - Batch file to extract text from multiples txt files in multiples folders 如何使用此批处理文件将所有* .txt文件从子文件夹复制到批处理文件文件夹? - How to copy all *.txt files from subfolder to batch file folder using this batch-file? Windows批处理文件删除桌面上的所有文件 - Windows batch file to delete all files on desktop 删除目录中的所有文件和文件夹 - Delete all files and folders in a directory 使用批处理文件索引E:\\ data下的所有文件和文件夹 - Index all files and folders under E:\data using batch file 删除除“开头为”以外的所有文件夹的批处理脚本 - Batch script to delete all folders except of “Starts with” 使用模式匹配删除当前目录中的所有文件夹 - To delete all folders in the current directory with pattern matching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM