Delete files in subfolder using batch script

后端 未结 5 1653
小鲜肉
小鲜肉 2020-12-30 14:06

I have to delete files from a sub folder with a same name. My file path is like as follows.

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


        
相关标签:
5条回答
  • 2020-12-30 14:35

    Use powershell inside your bat file

    PowerShell Remove-Item c:\scripts\* -include *.txt -exclude *test* -force -recurse
    

    You can also exclude from removing some specific folder or file:

    PowerShell Remove-Item C:/*  -Exclude WINDOWS,autoexec.bat -force -recurse
    
    0 讨论(0)
  • 2020-12-30 14:36

    You can use the /s switch for del to delete in subfolders as well.

    Example

    del D:\test\*.* /s
    

    Would delete all files under test including all files in all subfolders.

    To remove folders use rd, same switch applies.

    rd D:\test\folder /s /q
    

    rd doesn't support wildcards * though so if you want to recursively delete all subfolders under the test directory you can use a for loop.

    for /r /d D:\test %a in (*) do rd %a /s /q
    

    If you are using the for option in a batch file remember to use 2 %'s instead of 1.

    0 讨论(0)
  • 2020-12-30 14:43

    I had to complete the same task and I used a "for" loop and a "del" command as follows:

    @ECHO OFF
    
    set dir=%cd%
    
    FOR /d /r %dir% %%x in (archive\) do (
        if exist "%%x" del %%x\*.txt /f /q
    )
    

    You can set the dir variable with any start directory you want or used the current directory (%cd%) variable.

    These are the options for "for" command:

    • /d For directories
    • /r For recursive

    These are the options for "del" command:

    • /f Force deletes read-only files
    • /q Quiet mode; suppresses prompts for delete confirmations.
    0 讨论(0)
  • 2020-12-30 14:45

    del parentpath (or just place the .bat file inside parent folder) *.txt /s

    That will delete all .txt files in the parent and all sub folders. If you want to delete multiple file extensions just add a space and do the same thing. Ex. *.txt *.dll *.xml

    0 讨论(0)
  • 2020-12-30 15:01

    Moved from the closed topic

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

    This should get you all of your text files

    Alternatively,

    I modified a script I already wrote to look for certain files to move them, this one should go and find files and delete them. It allows you to just choose to which folder by a selection screen.

    Please test this on your system before using it though.

    @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)
    

    If you change the

    set targetFolder= 
    

    to the folder you want you won't get prompted for the folder. *Remember when putting the base path in, the format does not include a '\' on the end. e.g. d:\test c:\temp

    Hope this helps

    0 讨论(0)
提交回复
热议问题