Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

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


(!)

pure_evil020's Avatar
pure_evil020 pure_evil020 is offline
Member with 92 posts.
THREAD STARTER
 
Join Date: Jul 2008
20-Jul-2012, 01:38 AM #1
Solved: script to locate file, and record the directory to txt
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:
Quote:
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?

Last edited by pure_evil020; 20-Jul-2012 at 01:46 AM..
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,278 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
20-Jul-2012, 02:27 AM #2
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's Avatar
pure_evil020 pure_evil020 is offline
Member with 92 posts.
THREAD STARTER
 
Join Date: Jul 2008
20-Jul-2012, 03:05 AM #3
Quote:
Originally Posted by Ent View Post
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)?

Last edited by pure_evil020; 20-Jul-2012 at 03:12 AM..
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,278 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
20-Jul-2012, 03:47 AM #4
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.

Code:
' 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's Avatar
pure_evil020 pure_evil020 is offline
Member with 92 posts.
THREAD STARTER
 
Join Date: Jul 2008
20-Jul-2012, 06:22 AM #5
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:
Quote:
Windows

On Windows 95, 98, and ME, profile folders are in one of these locations, by default:
  • C:\Windows\Application Data\Mozilla\Firefox\Profiles\<profile folder>
  • C:\Windows\Profiles\<Windows login/user name>\Application Data\Mozilla\Firefox\Profiles\<profile folder> .
On Windows 2000 and Windows XP, profile folders are in this location, by default:
  • C:\Documents and Settings\<Windows login/user name>\Application Data\Mozilla\Firefox\Profiles\<profile folder>
The Application Data folder is a hidden folder; to show hidden folders, open Windows Explorer and choose "Tools → Folder Options → View (tab) → Show hidden files and folders".
On Windows Vista and Windows 7, profile folders are in this location, by default:
  • C:\Users\<Windows login/user name>\AppData\Roaming\Mozilla\Firefox\Profiles\<profile folder>.
The AppData folder is a hidden folder; to show hidden folders, open a Windows Explorer window and choose "Organize → Folder and Search Options → Folder Options → View (tab) → Show hidden files and folders".
On Windows 2000, XP, Vista, and Windows 7, you can also use this path to find the profile folder, even when it is hidden:
  • %APPDATA%\Mozilla\Firefox\Profiles\<profile folder>
%APPDATA% is a variable represents the C:\Documents and Settings\<user name>Application Data folder on Windows 2000/XP and the C:\Users\<user name>\AppData\Roaming folder on Windows vista and Windows 7.
To find a profile folder in the default location on Windows 2000, XP, Vista, or Windows 7:
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,278 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
20-Jul-2012, 06:36 AM #6
Is this better?
Code:
' 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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Jul-2012, 06:45 AM #7
When are you going to post the solution to the last thread you started and mark the thread solved?
pure_evil020's Avatar
pure_evil020 pure_evil020 is offline
Member with 92 posts.
THREAD STARTER
 
Join Date: Jul 2008
20-Jul-2012, 10:00 AM #8
Quote:
Originally Posted by Ent View Post
Is this better?
Code:
' 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
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

Quote:
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's Avatar
Trusted Advisor with 19,660 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
20-Jul-2012, 10:27 AM #9
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's Avatar
pure_evil020 pure_evil020 is offline
Member with 92 posts.
THREAD STARTER
 
Join Date: Jul 2008
20-Jul-2012, 11:44 AM #10
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's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,278 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
20-Jul-2012, 11:53 AM #11
Replace this
Code:
' 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
Code:
' 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
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑