Find and delete folder

(imported topic written by sgreenwall91)

Has anyone written an action/relevance string to search for and delete a folder? I need to search for and delete a folder from C:\Documents and Settings and am trying to work with a combination of folder of and descendant of and have not been able to find a combination that works.

(imported comment written by sgreenwall91)

Additionally, I know that the folder will exist in the root of the profile folder (will be at C:\Documents and Settings\User1\My Folder and C:\Documents and Settings\User2\My folder) so if possible, the string would have limitations on it to prevent the search from recursing through lower level directories (in order to save system resources).

(imported comment written by brolly3391)

Hello sgreenwall,

I usually resort to BAT files or DOS commands when I want to recursively delete files in all the profiles on a machine. So to delete “c:\documents and settings\profile\my folder” and all of the subfolders and files in that folder I would use this as my action:

DOS for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\My Folder”

I did not test that dos command, give it a whirl and let me know if I missed the mark. also, some of the quote marks in that command line are actually the backquote (the one on the tilde key, near the escape key).

See here for more on using the FOR command:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true

Cheers,

Brolly

(imported comment written by brolly3391)

I only answered 1/2 of your question. To check if that folder to be deleted exists in any profile on the machine you could use this relevance:

q: exists folder whose (name of it = “My Folder”) of folders of folders of folder “c:\documents and settings”

A: False

T: 362.695 ms

I: singular boolean

It’s not very fast because it is checking 3 folders deep.

Cheers,

Brolly

(imported comment written by sgreenwall91)

The purpose of this is to find and delete any user settings folders for the Citrix ICA client prior to performing a new install.

Using the following will return the paths of all folders that I need to remove:

pathnames of folders whose (name of it = “ICAClient”) of folders of folders of folder “C:\Documents and Settings”

I am trying to use appendfile to create a .txt or .ini to reference while building a second appendfile that will be a command file that will actually perform the deletion. One problem is that using the above line in QandA or Relevance debugger returns the path to each folder on a separate line. In the append file, the paths are all returned as a single string:

C:\Documents and Settings\User1\Application Data\ICAClientC:\Documents and Settings\User2\Application Data\ICAClient

I assume that I can use concatenation to pull the paths to produce a command file that would look something like this:

rmdir /S /Q “C:\Documents and Settings\User1\Application Data\ICAClient”

rmdir /S /Q “C:\Documents and Settings\User2\Application Data\ICAClient”

How would I use concatenation to collect the path apply the preceding text, a carriage return (%0D) and then continue collecting until the end of the string?

(imported comment written by brolly3391)

sgreenwall,

I think I follow you. It sounds like your approach is a little more complicated than it needs to be. Try this on for size:

Relevance for your Fixlet will be:
exists folder whose (name of it = “ICAClient”) of folders of folders of folder “C:\Documents and Settings” and (it = “WinXP” or it=“Win2000”) of Name of Operating System

Action script will be:
DOS for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\ICAClient”

This will only work on Windows 2k forward.

That dos command actually pulls a directory of the documents and settings folder and then parses each line and puts it into the %a variable. The /b on the DIR command gives you just a simple list with no headers and the /a gets you hidden folders too. Then for each %a it performs rd /s /q “c:\documents and settings%a\ICAClient”. The beauty of that is that you don’t have to detect what profiles actually have the subdirectory. You just delete any that exist under any profile. Fast and efficient.

To better see how that DOS command works and how I built it, paste these commands into the command window on a test box.

First try this to pull subdirectories of c:\documents and settings:

dir /b /a “c:\documents and settings”

Then this to see the same thing but parsed out into individual echo commands:

for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do echo %a

Then this to echo the total path wrapped around each of those subdirectories:

for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do echo c:\documents and settings%a\ICAClient

And finally this to perform the actual deletion of each of the subdirectories returned above, whether or not they exist:

for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\ICAClient”

You will see some error when the directory does not exist, but it will run to completion and should sucessfully delete all of those folders from all of your profile directories.

For more on the concatenation question, these threads might help:

http://forum.bigfix.com/viewtopic.php?id=73

http://forum.bigfix.com/viewtopic.php?id=170

http://forum.bigfix.com/viewtopic.php?pid=843#p843

Cheers,

Brolly

(imported comment written by sgreenwall91)

Yes I was making it too difficult. I was missing a directory in my original post. Should have been “C:\Documents and Settings\User1\Application Data\ICAClient”. The DOS command works as advertised when the full path is included. Thanks much!

(imported comment written by s_scicluna91)

how do i run the following commands in a batch file. I can paste them into a command prompt and they work any ideas???

sorry for posting on an old thread

FOR /f “usebackq” %a in (dir /b /a "c:\documents and settings\s3*") do rd /s /q “c:\documents and settings%a”

FOR /f “usebackq” %a in (dir /b /a "c:\documents and settings\reg*") do rd /s /q “c:\documents and settings%a”

FOR /f “usebackq” %a in (dir /b /a "c:\documents and settings\FPL*") do rd /s /q “c:\documents and settings%a”

(imported comment written by BenKus)

Hi scicluna,

You can simply create a batch file on-the-fly and then run it… here is an example actionscript:

// delete the __appendfile and batch file (just in case they exists already):
delete __appendfile
delete rmdirs.bat

// create the file using the appendfile command that writes to __appendfile
appendfile FOR /f “usebackq” %a in (dir /b /a "c:\documents and settings\s3*") do rd /s /q "c:\documents and settings%a"
appendfile FOR /f “usebackq” %a in (dir /b /a "c:\documents and settings\reg*") do rd /s /q "c:\documents and settings%a"
appendfile FOR /f “usebackq” %a in (dir /b /a "c:\documents and settings\FPL*") do rd /s /q “c:\documents and settings%a”

// rename and run the batch file (use “wait” instead of “waithidden” to see the command as it runs)
copy __appendfile rmdirs.bat
waithidden rmdirs.bat

I didn’t test this, but it should hopefully work or at least get you started.

Ben

(imported comment written by Harish91)

How to search in folder from folders by dos commands run as a bat file.

(imported comment written by Harish91)

how to create bat file from excel update and run that

(imported comment written by Snow12391)

You will see some error when the directory does not exist, but it will run to completion and should sucessfully delete all of those folders from all of your profile directories.

(imported comment written by chumplet91)

brolly33

Action script will be:
DOS for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\ICAClient”

this post has been a relative godsend as i’ve been working on something like this for weeks now.

i’m trying to delete the ‘temp’ and ‘temporary internet files’, etc folders from the user profiles of a slaved drive. the script has worked great except the variable doesn’t want to parse long file names (e.g it will delete ‘%a\local settings\temp’ when %a = admin but not when %a = admin 1)

i know that the variable tends to choke on long file names in the command section, but i thought the double quotes would take care of the issue.

any help is greatly appreciated.

~chumplet

(imported comment written by jessewk)

how long is the full pathname after expansion? I’ve run into command line length limitations before. I’m not sure what the actual limit is, but I believe in some cases it’s a little as 127 characters.

Jesse

(imported comment written by brolly3391)

Hey chumplet,

I think the error is creeping in at the

for /f “usebackq” %a in (dir /b /a "c:\documents and settings")

section. Because %a might be dropping everything after a space. Might need to add in the delims command. Checking http://technet.microsoft.com/en-us/library/bb491071.aspx

Yes,

Tested with:

for /f “usebackq delims=” %a in (dir /b /a "c:\documents and settings") do echo %a

suggested new command:

DOS for /f “usebackq delims=” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\ICAClient”

Your mileage may vary

:slight_smile:

Brolly

(imported comment written by kaushalw91)

I am looking for a script for finding a particular file in C:. I just want to find, no action to be taken. e.g. Find file abc.sys in C:\

How can i see the output if I run this task on all computers.

Second point, how do I search for particular file and then delete it e.g search for test.txt in C:, D:\and if found, delete it.

(imported comment written by BenKus)

You want to search the whole drive, correct? If so, see this thread (in particular, the last post):

http://forum.bigfix.com/viewtopic.php?id=362

Ben

(imported comment written by kaushalw91)

I want to search and delete the file. e.g search for abc.txt and delete it

I saw the post you mentioned but I am not able to get it

Let me explain you:

runhidden {pathname of system folder}\cmd.exe /C dir /s /b c:*.MP3 >"{pathname of parent folder of regapp “besclient.exe”}\searchresults_MP3.txt"

The above mentioned 2 lines would search for MP3 files and would redirect the output in a text file named “searchresult_MP3”

I would like to know the meaning for below mentioned lines

setting “MyFileSearch_MP3”="{now}" on “{now}” for client

And what would be the syntax of below in my case where I want to search for a file “abc.txt” in whole drive and delete it

if (exists file “searchresults_MP3.txt” of parent folder of regapp “besclient.exe” ) then (lines of file “searchresults_MP3.txt” of parent folder of regapp “besclient.exe”) else (“MP3 Search result not present”)

It would be great if you can help me in that.

Thanks

(imported comment written by BenKus)

Hi kaushalw,

The line ‘setting “MyFileSearch_MP3”="{now}" on “{now}” for client’ is used to record the last time of the search. This is used both as a reference and so that the relevance in the Fixlet can see the search had completed and it wouldn’t need to re-run for a while.

To delete the files in this list, you can create a batch file based on the contents of the txt file… for instance, you might do something like:

// search for the file abc.txt and write its path to searchresults.txt
waithidden {pathname of system folder}\cmd.exe /C dir /s /b abc.txt >"{pathname of parent folder of regapp “besclient.exe”}\searchresults.txt"

// check if there are results in searchresults.txt and if there are results, then create a batch file to delete the files
if {exists (lines whose (it as lowercase contains “abc.txt”) of file “searchresults.txt” of parent folder of regapp “besclient.exe”)}
appendfile {(“delete %22” & it & “%22%0d%0a”) of file “searchresults.txt” of parent folder of regapp “besclient.exe” }
move __appendfile deletefiles.bat
waithidden cmd.exe /C deletefiles.bat
endif

I didn’t test that so please try it out and see if it works the way you want.

Ben

(imported comment written by troyboy91)

brolly33

sgreenwall,

I think I follow you. It sounds like your approach is a little more complicated than it needs to be. Try this on for size:

Relevance for your Fixlet will be:
exists folder whose (name of it = “ICAClient”) of folders of folders of folder “C:\Documents and Settings” and (it = “WinXP” or it=“Win2000”) of Name of Operating System

Action script will be:
DOS for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\ICAClient”

This will only work on Windows 2k forward.

That dos command actually pulls a directory of the documents and settings folder and then parses each line and puts it into the %a variable. The /b on the DIR command gives you just a simple list with no headers and the /a gets you hidden folders too. Then for each %a it performs rd /s /q “c:\documents and settings%a\ICAClient”. The beauty of that is that you don’t have to detect what profiles actually have the subdirectory. You just delete any that exist under any profile. Fast and efficient.

To better see how that DOS command works and how I built it, paste these commands into the command window on a test box.

First try this to pull subdirectories of c:\documents and settings:
dir /b /a “c:\documents and settings”

Then this to see the same thing but parsed out into individual echo commands:
for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do echo %a

Then this to echo the total path wrapped around each of those subdirectories:
for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do echo c:\documents and settings%a\ICAClient

And finally this to perform the actual deletion of each of the subdirectories returned above, whether or not they exist:
for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\ICAClient”

You will see some error when the directory does not exist, but it will run to completion and should sucessfully delete all of those folders from all of your profile directories.

For more on the concatenation question, these threads might help:
http://forum.bigfix.com/viewtopic.php?id=73
http://forum.bigfix.com/viewtopic.php?id=170
http://forum.bigfix.com/viewtopic.php?pid=843#p843
Cheers,

Brolly

Hey Guy’s

This is exactly what I have been trying to do…been looking everywhere.

I needed to delete a folder called “sessions” that could have been located under any profile under Documents and settings. I have modified it to work via command line but when i put it in a bat it does not work ? Why is that ?

I have the following modified :

dir /b /a “c:\documents and settings”

for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do echo %a

for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do echo c:\documents and settings%a\desktop\sessions

for /f “usebackq” %a in (dir /b /a "c:\documents and settings") do rd /s /q “c:\documents and settings%a\desktop\sessions”

Firstly I tried renaming folders to short names EG: Document and Settings to DOCUME~1 … But No Go…

When I run it line by line within a DOS window it works , when i add it to my batch file that also deletes the registry entry and folder of the application, It does not work and I get the following error:

\DOCUME~1"`) do rd /s /q “C:\DOCUME~1\a\desktop\sessions” was unexpected at this time.

Cant work it out…At the end of the day I want a batch file to search all user profiles and then search and delete a folder called SESSIONS…Appreciate any assistance…

Cheers