從通配符資料夾中刪除文件的批次文件

從通配符資料夾中刪除文件的批次文件

如何建立從多個目錄中刪除檔案的批次檔?

D:\L1\asdasda\L3\*.txt
D:\L1\dfghfghfh\L3\*.txt
D:\L1\tyutyugj\L3\*.txt
D:\L1\ytrtyrty\L3\*.txt

喜歡:

D:
del "d:\L1\*\L3\*.txt"

巴洛普的註釋-提問者補充說-

我大約有一百個這樣的目錄。我不想刪除資料夾,只想刪除檔案。它們都有一個 L3 資料夾,並且都包含一些具有相同副檔名的檔案。這些只是臨時文件,但不會自動刪除。

答案1

使用子資料夾名稱通配符動態地從具有不同名稱的子資料夾中刪除批次文件

從通配符資料夾中刪除文件的批次文件

這是一個簡單的批次腳本範例,我一直在使用它來完成此類任務,我插入了變數資料夾路徑以滿足您所描述的需求:

批次腳本範例

(將變數根資料夾和子資料夾設定在頂部,並且FOR /D 和循環相應地進行迭代,以按照邏輯指定並完成檔案命令FOR來完成遍歷目錄的其餘魔法)DEL /Q /F*.txt

@ECHO ON

SET SourceDir=D:\L1
SET SourceSubDir=L3

:: --// note the asterisk wildcard after SourceDir in the first FOR /D loop using X as the variable
:: --// note the X variable appended to the beginning of the second FOR (no /D switch here) loop in the SET part using Y as the variable
FOR /D %%X IN ("%SourceDir%\*") DO FOR %%Y IN ("%%~X\%SourceSubDir%\*.txt") DO DEL /Q /F "%%~Y"
GOTO EOF

筆記: 如果您打算透過在命令提示字元中手動複製並貼上來執行此命令,則 FOR 循環需要在所有部分中刪除百分號之一,因此如果您透過複製和貼上手動運行此部分,而不是在批次腳本中運行,並執行上面範例的工作方式,請使用下面的內容。

FOR /D %X IN ("%SourceDir%\*") DO FOR %Y IN ("%~X\%SourceSubDir%\*.txt") DO DEL /Q /F "%~Y"

進一步的細節和研究

(輸入 FOR /? 在 Windows 命令提示字元中查看此詳細資訊)

FOR(無開關)

Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

為/D

FOR /D %variable IN (set) DO command [command-parameters]

    If set contains wildcards, then specifies to match against directory
    names instead of file names.

答案2

解釋你的行 \L1\*\L3\*。如:對於給定的目錄L1,請尋找所有具有子目錄L3的子目錄,並刪除其中的所有檔案。

必須編寫腳本來遍歷目錄結構。作為批次文件,它並不容易實現,您可能必須使用 Perl 或 powershell,它們有方法獲取目錄或文件列表,然後對它們執行重複操作。

在偽代碼中

Get list directories
For each dir 
  Cd into it
  Check if The dir L3 exists
  If it does then del .\L3\*.* 
  Go back up a level
Next dir

我會考慮這一點並在批次檔程式碼中執行此操作,儘管我正在考慮在 powershell 中重做它只是為了看看差異。

好吧,我想了一下

此批次檔將從運行位置開始遍歷目錄結構,並刪除任何名為「L3」的目錄的內容。

@echo off
for /r %%d in (*) do call :process "%%d"
goto :eof

:process
pushd "%~dp1"
for /f "delims=" %%A in ('cd') do (
     set foldername=%%~nxA
    )
if "%foldername%"=="L3" del/q *.*
popd
goto :eof

也許可以做得更簡單,但僅獲取當前目錄名稱的操作很混亂(進程部分中的 for 循環)

如果目錄像這樣開始

└───L1
    │   1.dat
    ├───Alice
    │   │   9.dat
    │   │
    │   └───L3
    │           c.dat
    │           d.dat
    ├───Bob
    │       6.dat
    ├───Carole
    │   │   2.dat
    │   │
    │   └───L3
    │           a.dat
    │           b.dat
    └───Ted
            6.dat

運行 cleantree 應該看起來像這樣

└───L1
    │   1.dat
    ├───Alice
    │   │   9.dat
    │   │
    │   └───L3
    ├───Bob
    │       6.dat
    ├───Carole
    │   │   2.dat
    │   │
    │   └───L3
    └───Ted
            6.dat

答案3

for /f %G in ('dir /ad /b D:\L1\') do del D:\L1\%G\L3\*.txt

for /f對集中的所有檔案執行以下 do 命令

%G in ('dir /ad /b D:\L1\')變數設定為給定目錄中每個資料夾的裸格式

使用目錄的設定%G值和通配符刪除命令


先試運行

使用 echo 運行,將要運行的命令列印到控制台,以測試該命令,而無需實際運行它:

for /f %G in ('dir /ad /b D:\L1\') do echo del D:\L1\%G\L3\*.txt

相關內容