Batch delete certain files only certain file exist in subfolder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Batch delete certain files only certain file exist in subfolder

#1 Post by goodywp » 06 Sep 2017 11:35

I just want a batch file to delete all files with extension *.P3? in all subfolders on condition of only some file exist in these subfolders like *.pkg file

for example:

C:\test\abc\*.P3A
C:\test\abc\*.pkg

C:\test\def\*.P3L

C:\test\ghi\*.P3P
C:\test\ghi\*.pkg

So the batch run results should be as below: both the *.P3A file in abc folder and *.P3P file in ghi folder should be deleted. But the *.P3L file should not be deleted since it does not have *.pkg file in folder of def.

Thanks
Last edited by goodywp on 06 Sep 2017 12:11, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch delete certain files only certain file exist in subfolder

#2 Post by Squashman » 06 Sep 2017 11:38

You are going to need to be more specific then that. Please provide a true real world example with actual file names.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch delete certain files only certain file exist in subfolder

#3 Post by aGerman » 06 Sep 2017 11:59

So what I understood is that C:\test is the root folder. The .P3L files you missed to mention above would be deleted,
too.

Code: Select all

del /f /s /q "C:\test\*.p3?"

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Batch delete certain files only certain file exist in subfolder

#4 Post by goodywp » 06 Sep 2017 12:07

Hi Squashman,

Here is the example:


..\Customer Download\TETRA\TELIUM3\TSA

8295010512_TSA.M70
8295010512_TSA.P3P
8295010512_TSA.P3P.jsat
8295010512_TSA.P3P.pkg
Contents.lst
Schemes to Sign_for_829501.doc


..\Customer Download\TETRA\TELIUM3\KIA
8295000512_KIA.M70
8295000512_KIA.P3A
8295000512_KIA.P3A.jsat
8295000512_KIA.P3A.pkg
Contents.lst
Schemes to Sign_for_829500.doc


..\Customer Download\TETRA\TELIUM3\E2EE
8295522003_E2EE.M70
8295522003_E2EE.P3A
8295522003_E2EE.P3A.jsat
8295522003_E2EE.P3A.pkg
Contents.lst
Schemes to Sign_for_829552.doc


..\Customer Download\TETRA\ALL_TERMINALS_DLLSQLITE
9990044299020301_DLLSQLITE.P3L
9992048007111202_OPT_SQLI.P3P
ALL_TERMINALS_DLLSQLITE.M70
Contents.lst

Th batch run should delete all *.P3? (not *.P3A.jsat nor *.P3A.pkg) files in above except this folder "ALL_TERMINALS_DLLSQLITE" since this folder does not meet the condition which does not have *.pkg file in it. So after run the above should looks like this:

..\Customer Download\TETRA\TELIUM3\TSA

8295010512_TSA.M70

8295010512_TSA.P3P.jsat
8295010512_TSA.P3P.pkg
Contents.lst
Schemes to Sign_for_829501.doc


..\Customer Download\TETRA\TELIUM3\KIA
8295000512_KIA.M70

8295000512_KIA.P3A.jsat
8295000512_KIA.P3A.pkg
Contents.lst
Schemes to Sign_for_829500.doc


..\Customer Download\TETRA\TELIUM3\E2EE
8295522003_E2EE.M70

8295522003_E2EE.P3A.jsat
8295522003_E2EE.P3A.pkg
Contents.lst
Schemes to Sign_for_829552.doc


..\Customer Download\TETRA\ALL_TERMINALS_DLLSQLITE
9990044299020301_DLLSQLITE.P3L
9992048007111202_OPT_SQLI.P3P
ALL_TERMINALS_DLLSQLITE.M70
Contents.lst


I hope that I put the requirement clear :oops:

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch delete certain files only certain file exist in subfolder

#5 Post by aGerman » 06 Sep 2017 12:17

The line I posted above should work. Just customize the path (and backup the files before you try).
Also move folder ALL_TERMINALS_DLLSQLITE to another location (on the same drive) in order to exclude it. Don't worry, moving files on the same volume will not cause physical moving.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch delete certain files only certain file exist in subfolder

#6 Post by Squashman » 06 Sep 2017 12:29

Seems like in its simplest form you want to delete a corresponding file to the PKG file.

Code: Select all

FOR /R %%G IN (*.PKG) DO IF EXIST "%%~dpnG" DEL "%%~dpnG"

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Batch delete certain files only certain file exist in subfolder

#7 Post by goodywp » 06 Sep 2017 13:08

Hi Squashman,

It is indeed the simplest form I can look for so far! and best of all it works as expected!!

A big thanks to both of you and Steffen for quick replies....

I am blessed by this forum that I had genuine help...

Cheers!

Post Reply