Link to home
Start Free TrialLog in
Avatar of Indyrb
IndyrbFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Indyrb

ASKER

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"
  )
)
Avatar of Bill Prew
Bill Prew

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
Avatar of Indyrb

ASKER

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.
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
Avatar of Indyrb

ASKER

Can you you show me what the complete .bat file would look like, with the pushd and adding temporary internet files.
@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