3

可能重复:
使用批处理脚本删除子文件夹中的文件

我必须从子文件夹(同名)中删除 .txt 文件。我的文件路径如下。

d:\test\test1\archive*.txt d:\test\try\archive*.txt d:\test\model\archive*.txt

我尝试使用“del”命令删除上述路径中的“.txt”文件。但是“test”文件夹中有100多个文件夹。所以很难对每条路径都使用“del”。

除了“存档”文件夹的父文件夹名称外,所有路径的所有内容都保持不变。所以我想可能有一些简单的方法可以使用批处理脚本删除文件。

谁能指导我是否有任何简单的方法可以使用批处理脚本删除 .txt 文件或者我必须为所有 100 个文件夹重复“del”?

4

3 回答 3

5
del /s *.txt

希望它有帮助。所有最好的伙伴

于 2012-12-10T13:14:14.620 回答
2

del /s *.txt将删除当前工作目录的所有子文件夹中的所有 TXT 文件。

(但请小心使用该命令 - 错误的父目录,您将丢弃计算机上的所有文本文件 :))

于 2012-12-10T13:08:51.573 回答
0

已编辑

del /s d:\test\archive\*.txt

这应该让你所有的文本文件

或者,

我修改了一个我已经写过的脚本来寻找某些文件来移动它们,这个应该去查找文件并删除它们。它允许您通过选择屏幕选择到哪个文件夹。

不过,请在使用之前在您的系统上进行测试。

@echo off
Title DeleteFilesInSubfolderList
color 0A
SETLOCAL ENABLEDELAYEDEXPANSION

REM ---------------------------
REM   *** EDIT VARIABLES BELOW ***
REM ---------------------------

set targetFolder=
REM targetFolder is the location you want to delete from    
REM ---------------------------
REM  *** DO NOT EDIT BELOW ***
REM ---------------------------

IF NOT DEFINED targetFolder echo.Please type in the full BASE Symform Offline Folder (I.E. U:\targetFolder)
IF NOT DEFINED targetFolder set /p targetFolder=:
cls
echo.Listing folders for: %targetFolder%\^*
echo.-------------------------------
set Index=1
for /d %%D in (%targetFolder%\*) do (
  set "Subfolders[!Index!]=%%D"
  set /a Index+=1
)
set /a UBound=Index-1
for /l %%i in (1,1,%UBound%) do echo. %%i. !Subfolders[%%i]!

:choiceloop
echo.-------------------------------
set /p Choice=Search for ERRORS in: 
if "%Choice%"=="" goto chioceloop
if %Choice% LSS 1 goto choiceloop
if %Choice% GTR %UBound% goto choiceloop
set Subfolder=!Subfolders[%Choice%]!
goto start

:start
TITLE Delete Text Files - %Subfolder%
IF NOT EXIST %ERRPATH% goto notExist
IF EXIST %ERRPATH% echo.%ERRPATH% Exists - Beginning to test-delete files...
echo.Searching for .txt files...
pushd %ERRPATH%
for /r %%a in (*.txt) do (
echo "%%a" "%Subfolder%\%%~nxa"
)
popd
echo.
echo.
verIFy >nul
echo.Execute^?
choice /C:YNX /N /M "(Y)Yes or (N)No:"
IF '%ERRORLEVEL%'=='1' set question1=Y
IF '%ERRORLEVEL%'=='2' set question1=N
IF /I '%question1%'=='Y' goto execute
IF /I '%question1%'=='N' goto end

:execute
echo.%ERRPATH% Exists - Beginning to delete files...
echo.Searching for .txt files...
pushd %ERRPATH%
for /r %%a in (*.txt) do (
del "%%a" "%Subfolder%\%%~nxa"
)
popd
goto end

:end
echo.
echo.
echo.Finished deleting files from %subfolder%
pause
goto choiceloop
ENDLOCAL
exit


REM Created by Trevor Giannetti
REM An unpublished work
REM (October 2012)

如果你改变

set targetFolder= 

到您想要的文件夹,不会提示您输入该文件夹。*请记住,在放入基本路径时,格式末尾不包含“\”。例如 d:\test c:\temp

希望这可以帮助

于 2012-12-10T13:33:10.730 回答