Search and destroy wihout knowing the location?

I was wanting to know if there is a way to find a folder or file with a specified name delete all its conents without knowing its full path or directory structure,,,
Can this be accomplished via a script or command line or powershell

In this example I am looking for any folder named temp.... These temp folders that are in question are in unknown locations.... very deeply nested/rooted. all over the place...

I was messing with the command dir, and findstr. and came up with some results, but how do you combine this with the delete or do something command.

dir /b /s /ad | findstr /i "\<Temp\>" |findstr /v " "\<exlude\>"

Also the findstr /I "\<Temp\>" does indeed find all the temp directories, however it also list all the child items and folder in the temp directory. (which I dont necessary want) is there a command to only print or list just the root folder with \\.\\.\\.\\.\\. Temp\
... without knowing where the orginal temp directory is....or how far down it is nested...
AKA: you don't know its abosulte path... just its name...
Also messing with the variable dir /b /s /ad | findstr "\<Temp"
Includes both the temporary Internet and the temp directory which is good. but it also list anything that starts with temp, example template.... in this example I would use the exlude template... But I also came across some other weird names that was listed in this group.. so perhaps just the constanit (temp) or (temporary) and then delete its contents


I was trying to use the findstr /v to exlude any of the results I didn't want shown, but these excluded results vary and are named various things. also can the dir /b /s /ad |findstr be used with a command to also delete all the files in the location and directories inside the temp...Again this is without knowing its true location... just the name of the directory...and its somewhere on the drive... may be 1000 folders deep or 10 deep.

Can all of these be accomplished via a powershell, vbs (vbscript) or cmd.exe (batch) .bat file?
LVL 5
IndyrbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Here's a fairly simple approach in a BAT script.  It could actually all be smooshed onto a single line of code, but I prefer to break it up for more readability and maintainability.  Save as a BAT file, adjust the base folder you want to search under and test it.  Currently it won't actually remove any folders, but will just echo the folders that would be removed.  If it looks good remove the ECHO before the RD command.

It uses the DIR command to get a list of all directories matching temp.*.  The FOR command gathers up the output from the DIR, and processes it.  We check that the extension of the matching temp.* folders is actually blank (we don't want to delete temp.bak folder say) and if so then we remove that folder.

@echo off

set BaseDir=c:

for /f "tokens=*" %%D in ('dir /ad /s /b "%BaseDir%\temp.*"') do (
  if "%%~xD" == "" (
    ECHO rd /s /q "%%~D"
  )
)

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IndyrbAuthor Commented:
I will give this a try...
But I am trying to learn along the way...
In your example

for /f "tokens=*"  

(1) does it matter if its for /f or for /R
(2) does it matter if its "tokens=*" or *delims=* not sure which each does.

I assume the %%D could be any letter of choice, and it is %% cause its used in a bacth file?

I understand the ('dir /ad /s /b

But can the "%BaseDir% query any folder name Temp as long as its on the base image dir: or will it search in, baseimage\Temp which in this case is C:\ so any Temp folders in c:\Temp will be removed...

would the temp.* remove filenames named temp.(whatever) since * was specified?
I ran dir /s /ad /b |findstr "\temp.*" and nothing was listed.
I tried "\Temp*" "\<Temp\" which all returned no results.

The  "\<Temp\>" showed the results. for just Temp
the  "\<Temp"  showed Templates, temporary internet files, temp and every starting with temp

I also want to be careful with using Temp* as folders with the name Template could also be deleted... so is the a way to use the exact name Temp and also the exact name Temporary Internet Files\


Okay now this part: Can you explain each process... and why the %%~

do (
  if "%%~xD" == "" (
    ECHO rd /s /q "%%~D"
  )
)
0
Bill PrewCommented:
for /f "tokens=*"  
(1) does it matter if its for /f or for /R
Yes, the FOR command has a number of options and /F is very different from /R. We use the /F option when we are reading a file line by line and want to process it, or processing the output of a command, in this case the DIR command.

Further info here:
http://ss64.com/nt/for.html
http://www.robvanderwoude.com/for.php

(2) does it matter if its "tokens=*" or *delims=* not sure which each does.
The FOR /F command allows parsing up of each line that it processes, and so has some options to specify which parms you want to extract, and what delimiters to use, etc.  In our case the DIR /B command will only be returning a full path to a file on each line, so we don't want to do any parsing (the default is to parse on a space) so we specify an option that esentially takes the whole line and assigns it to the loop variable with no parsing.  Either of the options you mention will casue that to happen.

I assume the %%D could be any letter of choice, and it is %% cause its used in a bacth file?
Correct on both counts.  DOS commands occasionally have a few odd quirks and the single versus double percents is one of those, for command line versus batch script.  Keep in mind that the loop variable letters are case sensitive though, so %%A is different than %%a.

I understand the ('dir /ad /s /b
But can the "%BaseDir% query any folder name Temp as long as its on the base image dir: or will it search in, baseimage\Temp which in this case is C:\ so any Temp folders in c:\Temp will be removed...
Having a little trouble understanding your question, but the effect we are after is something like:

dir /ad /s /b "c:\temp.*"

or

dir /ad /s /b "c:\somefolder\temp\*"

In the first case the DIR is starting in C:\ and looking for all directories that match the specified pattern temp.*.  In the second case the DIR is starting in C:\somefolder\ and looking for all directories that match the specified pattern temp.* underneath that starting folder.

would the temp.* remove filenames named temp.(whatever) since * was specified?
The DIR will return all directories that have a name of TEMP, with blank or actual extensions.  But since we only want the directories with no extension I added:

if "%%~xD" == "" (

This will take the results of the DIR, and only remove the ones with no extension, so only the folders names TEMP.

I ran dir /s /ad /b |findstr "\temp.*" and nothing was listed.
No need for findstr, we don't need or want that.  Just do:

dir /ad /s /b "\temp.*"

I tried "\Temp*" "\<Temp\" which all returned no results.

The  "\<Temp\>" showed the results. for just Temp
the  "\<Temp"  showed Templates, temporary internet files, temp and every starting with temp
I also want to be careful with using Temp* as folders with the name Template could also be deleted... so is the a way to use the exact name Temp and also the exact name Temporary Internet Files\

DON'T use TEMP*, that would return other directories whose names start with TEMP, and that you don't want, like TEMPLATE.

Okay now this part: Can you explain each process... and why the %%~

do (
  if "%%~xD" == "" (
    ECHO rd /s /q "%%~D"
  )
)
The IF command checks the extension of the file path being processed (which came from the DIR output) and tests for the extension equal to "", meaning no extension.

We use the x modifier in the loop variable reference to get just the extension, so %%D returns the current loop variable value, %%~D will return it but strip any surrounding double quotes, and then %%~xD will return only the extension of the current filename.  If you do FOR /? at a command prompt, and spacebar twice to see the full listing, the last page lists the various modifiers for loop variables.

If the IF condition is met (extension is blank) then we want to remove that entire folder, so we use the RD command with the /S option to remove any and all subfolders of that TEMP folder, and the /Q to suppress and "are you sure" confirmation prompts, since we want it to just remove the folder, not prompt each time for confirmation.

I added the ECHO in front of the actual RD command so that rather than execute the RD command, it executes an ECHO command, and just displays everything after that to the console.  This let's us see exaclty what RD command would have been executed, which is great for debugging.

Hope this helps,
~bp
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

IndyrbAuthor Commented:
Thanks for the great explanation -- so if we want to delete all folders and files inside the Temp, but not the temp itself, what would we do..

How would I wirte it for Temporary Internet Files,,,

And finnaly I ran the script you provided, and I am getting the directory name is too long...


Will it list all the folders\files it will delete with the echo... taking forever, and only listing the ones with the name to long error.
0
Bill PrewCommented:
Thanks for the great explanation -- so if we want to delete all folders and files inside the Temp, but not the temp itself, what would we do..
To do that we would typically use a trick in a BAT script.  Once we find the folder to be deleted, we can PUSHD to the folder to make it the current folder, then do the RD against it.  DOS will remove all contents (files & folders) but when it tries to remove the actual TEMP folder it will fail since it is in use as the current directory.  Then we POPD away from the folder.  If needed I can show that code.
How would I write it for Temporary Internet Files,,,
There are a lot of variables here, like version of windows, versions of IE (assuming you mean IE), language, etc.  So it's hard to hardcode a specific name and search for that.

I think the Microsoft sanction approach is to execute the following command, which could be done from within the BAT script easy enough:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

Open in new window

And finally I ran the script you provided, and I am getting the directory name is too long...

Will it list all the folders\files it will delete with the echo... taking forever, and only listing the ones with the name to long error.
Long file names are very problematic. There isn't any good way to deal with those in a BAT script unfortunately.

~bp
0
IndyrbAuthor Commented:
Can you you show me what the complete .bat file would look like, with the pushd and adding temporary internet files.
0
Bill PrewCommented:
@echo off

REM Set base folder to start removing TEMP folders from 
set BaseDir=c:

REM Loop through all directories that match TEMP.*
for /f "tokens=*" %%D in ('dir /ad /s /b "%BaseDir%\temp.*"') do (
  REM Make sure there is no extension (so the folder name must be just TEMP)
  if "%%~xD" == "" (
    REM Make this the current working directory so that we don't delete the folder itself, just everything in it
    pushd "%%~D"
    REM Remove all files and subfolders in this one
    rd /s /q "%%~D" 2>NUL
    popd
  )
)

REM Remove temporary internet files
REM http://social.technet.microsoft.com/Forums/scriptcenter/en-US/5178ac53-9b0e-486f-b405-32c33a9756bf/internet-temporary-files-deletion-script?forum=ITCG
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.