# Solved: script to locate file, and record the directory to txt



## pure_evil020 (Jul 31, 2008)

Hi there,
I am writing a little tool that helps you to remove specific malicious firefox toolbars, and addons.

I have come up with script that can safely modify certain entries in the prefs file, and another script that creates a backup of the firefox prefs file (with a restore function), if the user happens to make a change that they did not want to make.

I want to make this tool compatible with XP, Vista, and windows 7 users, but I have one problem...

The directory is always going to be different for every user.
Let me explain why:

This is the directory for the prefs file on my windows 7 pc:


> C:\Users\Tom\AppData\Roaming\Mozilla\Firefox\Profiles\byc0puwc.default\


The numbers/letters used for the "byc0puwc.default" folder name, is going to be different for each user, as I'm guessing the numbers/letters in that folder name are randomly generated for each user.

My solution, would be a silent wscript that will search the "CURRENTUSER" directory/sub-directories for that ".defult\" folder (including hidden folders), and write the exact directory, to a text file, for my software to use.

I'm sure it wouldn't be hard to achieve this effect with a wscript.
It could be just as easily achieved with batch, but its harder to automate a batch file to launch silently.

Could someone please show me a sample script that would do what I described above?


----------



## Ent (Apr 11, 2009)

What about using "GetFolder" on .../profiles/, and then using
For Each to check all the subfolders?

Incidentally, why not build all the scripts into a single unit instead of having them writing notes to one another in text files?


----------



## pure_evil020 (Jul 31, 2008)

Ent said:


> What about using "GetFolder" on .../profiles/, and then using
> For Each to check all the subfolders?
> 
> Incidentally, why not build all the scripts into a single unit instead of having them writing notes to one another in text files?


Hi Ent!

I'm writing the software in a different language (GML).
I would include the function in my GML script, but to get the same effect, I'm still going to have to call a batch script from the GML code to carry out this type of search.
Calling a batch file from your software looks rather ugly, because of the black window popping up.

A wscript can be executed silently, therefore it doesn't create the undesired ugly black window look.
I'm rather terrible at wscript though, so I have come here to get a sample script for this function.

Could you please show me a full example code, with comments, explaining each function (This way I learn something from the script, rather than simply copying and pasting)?


----------



## Ent (Apr 11, 2009)

This echos (doesn't store to a file) all the subfolders. It should be enough to show you where you're headed.
Be sure to test that it works on XP too.


```
' Created by Ent on Techguy.org
' 20-07-2012
' Identifies the name of the Firefox profile folder.

' The FileSystemObject is called to deal with files and folders.
Set FileSystem = CreateObject("Scripting.FileSystemObject")

' Gives the Path of the User Profile ("C:\Users\Ent"), constructed from two Environment Variables.
Set EnvShell = CreateObject( "WScript.Shell" )
Dim HomePath 
HomePath = EnvShell.ExpandEnvironmentStrings("%Homedrive%") & EnvShell.ExpandEnvironmentStrings("%Homepath%")

' Gives the path of the Firefox Profile
Dim ProfPath
ProfPath = HomePath & "\AppData\Roaming\Mozilla\Firefox\Profiles\"

' Gets the Folder Object associated with ProfPath
Set FFoxFolder = FileSystem.GetFolder(ProfPath)
' Gets all subFolders in that Folder Object
Set ProfFolders = FFoxFolder.SubFolders
For each Prof In ProfFolders
    ' Prints out each of the subfolders
    WScript.Echo(Prof.name)
Next
```


----------



## pure_evil020 (Jul 31, 2008)

hmmm, based on this information from "Mozillazine", I'd say that your script would only work on vista/windows 7.
The paths are all different:


> *Windows*
> 
> *On Windows 95, 98, and ME,* profile folders are in one of these locations, by default:
> 
> ...


----------



## Ent (Apr 11, 2009)

Is this better? 

```
' Created by Ent on Techguy.org
' 20-07-2012
' Identifies the name of the Firefox profile folder.

' The FileSystemObject is called to deal with files and folders.
Set FileSystem = CreateObject("Scripting.FileSystemObject")

' Gives the Path of the User Profile ("C:\Users\Ent"), constructed from two Environment Variables.
Set EnvShell = CreateObject( "WScript.Shell" )

Dim HomePath 
HomePath = EnvShell.ExpandEnvironmentStrings("%UserProfile%")

' Gives the path of the Firefox Profile
Dim ProfPath
IF FileSystem.FolderExists(HomePath & "\AppData") THEN
ProfPath = HomePath & "\AppData\Roaming\Mozilla\Firefox\Profiles\"
Else 
IF FileSystem.FolderExists(HomePath & "\Application Data") THEN
ProfPath = HomePath & "\AppData\Roaming\Mozilla\Firefox\Profiles\"
Else
WScript.echo("Something went wrong.")
End if 
End if

' Gets the Folder Object associated with ProfPath
Set FFoxFolder = FileSystem.GetFolder(ProfPath)
' Gets all subFolders in that Folder Object
Set ProfFolders = FFoxFolder.SubFolders
For each Prof In ProfFolders
    ' Prints out each of the subfolders
    WScript.Echo(Prof.name)
Next
```


----------



## Squashman (Apr 4, 2003)

When are you going to post the solution to the last thread you started and mark the thread solved?


----------



## pure_evil020 (Jul 31, 2008)

Ent said:


> Is this better?
> 
> ```
> ' Created by Ent on Techguy.org
> ...


That looks fantastic Ent!

While I was waiting for your reply, I actually came up with a GML code that used almost exactly the same method as your script.
I hadn't realized the existance of a couple of functions GML had available, so I was able to utilize them to piece together the directories the same way your wscript does.

I will admit though... your script looks allot cleaner than mine!
AND I have still learned from your script 



> When are you going to post the solution to the last thread you started and mark the thread solved?


I didn't post the solution, because I didn't think anyone reading on these forums would be after a GML script.
I have marked as solved now, and posted my gml script solution anyway, just in case.

This thread will now also be marked as solved.


----------



## Squashman (Apr 4, 2003)

It has been my experience that most programming languages can pretty much be used to program very similar concepts. Especially when it comes to what you are trying to do which is find files and edit them.


----------



## pure_evil020 (Jul 31, 2008)

Hey Ent, I have a quick question about your script...
How would I change it so that it writes the foldername to a text file, rather than simply show the message with the variable string?


----------



## Ent (Apr 11, 2009)

Replace this

```
' Gets the Folder Object associated with ProfPath
Set FFoxFolder = FileSystem.GetFolder(ProfPath)
' Gets all subFolders in that Folder Object
Set ProfFolders = FFoxFolder.SubFolders
For each Prof In ProfFolders
    ' Prints out each of the subfolders
    WScript.Echo(Prof.name)
Next
```
with this

```
' Gets the Folder Object associated with ProfPath
Set FFoxFolder = FileSystem.GetFolder(ProfPath)
' Gets all subFolders in that Folder Object
Set ProfFolders = FFoxFolder.SubFolders

Set ProfFoldersFile = FileSystem.CreateTextFile("FFoxFolders.txt", True)
For each Prof In ProfFolders
  ' Write a line.
    ProfFoldersFile.Write (Prof.name&VbCrLf)
Next
ProfFoldersFile.Close
```


----------

