search & remove files & folders

June 12, 2010 at 09:18:42
Specs: server 2003
i need a *.bat that can do as below:
files store in c:\test\new1
i need to delete all files & folders in new1 but
keeping new1 folder.
in c:\test contain others folders & files.
like i.e. c:\test\new1...
I.e. c:\test\new2.. & so on
i need to remain all new1 & new2 & so on...
but delete all others above this level (mean this
*.bat should able to search all folders in
c:\test\*.* and delete all others with remain all
1st sub_folder like c:\test\new1 and the others

so far this is what i have
FOR /R c:\test\ %%G IN (/S) DO del /F /Q
%%G
:: to delete all files in c:\test & its subfolders
FOR /D %%j IN (c:\test\new\*.*) DO RD /S /Q
"%%j"
:: to delete all folder & subfolders in c:\test\NEW


See More: search & remove files & folders

Report •


#1
June 12, 2010 at 18:00:28
not sure, but:
pushd c:\test
for /f "tokens=*" %%a in ('dir /b /ad') do (
pushd "%%a"
rd /s /q *.*
popd
)
::------ TEST FIRST! ---- end

Report •

#2
June 12, 2010 at 21:36:01
hi, nbrane thanks for ur help... BUT not working loh syntax error

Report •

#3
June 13, 2010 at 02:57:11
The problem statement is a little shakey, but maybe this:

rd /s /q c:\test\new1


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
June 13, 2010 at 03:54:24
hi Mechanix2Go
rd /s /q c:\test\new1 only remove folders in new1.. but what i
need is this *.bat able to search what ever folders in c:\test\*.*
exp: c:\test\new1......
c:\test\new2....
c:\test\new3...
so today maybe c:\test\*.* may have 3 folders. but tmr can
increase from new1.....to new10 everyday change. but i need
to remain what ever folders in c:\test\level 2 folders\level 3
folders\..
(means: today have new1,new2,new3.... but tmr can increase
to many more) but the command need to remain new1 or
new2 or new3.....and the others in the same level with auto
search & remove what ever start from level 3 folders.....tq

Report •

#5
June 13, 2010 at 08:02:18
I couldn't do it without two FOR loops:

@ECHO OFF
PUSHD c:\test
TREE /A
ECHO.
FOR /F "tokens=*" %%a IN ('DIR /b /ad') DO (
    PUSHD "%%a"
    FOR /F "delims=" %%b IN ('DIR /b /ad *.*') DO (
        RD  /S /Q "%%b"
        ECHO Deleted directory - %%b
    )
    FOR /F "delims=" %%b IN ('DIR /b /a-d *.*') DO (
        DEL /S /Q "%%b"
    )
    POPD
)
ECHO.
TREE /A
POPD
PAUSE
EXIT /B


Report •

#6
June 13, 2010 at 08:22:51
To orangeboy..
U r the best.. it working fine with me many thanks for ur help.

cheers;......:)


Report •

#7
June 13, 2010 at 15:07:25
No, M2 and nbrane are the clever ones :-D
I just work off their ideas ;-)

Report •

#8
June 13, 2010 at 18:04:14
Lol! don't look at ME, mine didn't work! thanks for the moral support at any rate. :-)

Report •

Ask Question