Delete files in subfolder using batch script

后端 未结 5 1655
小鲜肉
小鲜肉 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: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.

提交回复
热议问题