# Solved: How to delete files in a common folders and there sub folders.



## bgpreddy (Sep 4, 2008)

How to delete files under common folders name in different path.
OR

How to delete files under common leaf directory

Eg.

Folder structure.

1.sun
1.1.earth
1.1.1.moon
1.1.2.moon1
1.1.3.moon2
1.2mars
2.sun2
2.1.earth
2.1.1.moon
2.1.2.moon1
2.1.3.moon2
2.2.mars

I want to delete all the files (under earth, moon, moon1, moon2 in both the folders sun and sun2) but not folders, using single command thro command prompt.


----------



## Squashman (Apr 4, 2003)

So SUN and SUN2 are at the same folder level. Could you give us a better description of the folder structure. Could you give us a screen shot of explorer with all the folders expaned or from the cmd prompt do a *dir /ad /b /s*

You may have to do this with a batch file.


----------



## bgpreddy (Sep 4, 2008)

yeah of course i have to do with batch file.

yeah, sun and sun2 are two different folders at same folder level. Both contains same folder called earth, and again this earth will have sub folders(moon, moon1 and moon2 ). So now i want to delete all the files under earth, moon, moon1 and moon2 in both the folders i.e. from sun and sun2.

I think u may understand this time.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

If you can rename sun to sun1 you can use a For loop:
For %%I in (1 2) do del /f /s /q sun%%I\earth\*.*

Or if you can't rename:
For %%I in (sun sun2) do del /f /s /q %%I\earth\*.*

To do this from the command line instead of a batch file, use just one % sign, not two

you can also use 2 commands:
del /f /s /q sun\earth\*.*
del /f /s /q sun2\earth\*.*

Note that these will not delete any files that are under Sun or Sun2.

HTH

Jerry


----------



## bgpreddy (Sep 4, 2008)

Hi all,
i got my command, below is the one i was searching...

@for /f "tokens=*" %%a in ('dir /s /b /ad "earth" 2^>NUL') do del /s /q "%%a"

Thank u for giving reply..
Guru


----------



## bgpreddy (Sep 4, 2008)

Some more commands...

For is fun! Here are some simple tricks you can use FOR to perform very useful tasks!

*Recursive Find Text String In Files, Output Line Number:*
FOR /R c:\~kenneth %v IN (*.css) DO find /N /I "#banner" "%~fv" >> test.txt​Note the %v is a variable, could be any letter. The quoted "%~fv" expands to the quoted fully qualified pathname. With out quotes Directories with spaces are not processed.
*.css could be any wildcard or even * for all.
the >> concatenates the output to one file, in this case test.txt. Output looks like this: 
---------- C:\~KENNETH\STYLES-SITE-GEORGIA-BLUE.CSS
[16] #banner {
[29] #banner a,
[30] #banner a:link,
[31] #banner a:visited,
[32] #banner a:active,
[33] #banner a:hover {
[253] #banner-commentspop { 
---------- C:\~KENNETH\STYLES-SITE-GETTYSBURG.CSS
[16] #banner {
[28] #banner a,
[29] #banner a:link,
[30] #banner a:visited,
[31] #banner a:active,
[32] #banner a:hover {
[257] #banner-commentspop {​
*Copy files out of directory to backup folder:*
for /d /r %V IN (*) DO xcopy d:\conversion\borders\*.* %V​Note the case is important on your variable, in this case I used a capitalized V.

*Batch convert Microstation .DGN files to Autocad DWG:*
FOR /r %%i IN (*.dgn) DO msbatch dwgout createdwg input:%%i output:%%~di%%~pi%%~ni.dwg pausescreenff​This is so powerful if you even know what Microstation is memorize it.

*Recursively convert files:*
FOR /r %%i IN (*.dbf) DO perl dbf2csv.pl input:"%%i" output:"%%~di%%~pi%%~ni.xls"​Double percents are used in a CMD file when calling for from a batch file...

*Recursively delete:*
for /d /r %V IN (*) DO del %V\TNKSPEC.dwg​This recursively deletes the specific file through directory tree!
For Goodness sake use with caution. Testing with DIR is a very safe test!

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


----------



## TheOutcaste (Aug 8, 2007)

bgpreddy said:


> output:%%~di%%~pi%%~ni.dwg


The modifiers can be combined, so this will also work:
output: %%~dpni.dwg

Jerry


----------

