1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by pure_evil020, Jul 20, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. pure_evil020

    pure_evil020 Thread Starter

    Joined:
    Jul 31, 2008
    Messages:
    92
    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:
    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?
     
  2. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    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?
     
  3. pure_evil020

    pure_evil020 Thread Starter

    Joined:
    Jul 31, 2008
    Messages:
    92
    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)?
     
  4. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    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
    
     
  5. pure_evil020

    pure_evil020 Thread Starter

    Joined:
    Jul 31, 2008
    Messages:
    92
    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:
     
  6. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    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
    
     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    When are you going to post the solution to the last thread you started and mark the thread solved?
     
  8. pure_evil020

    pure_evil020 Thread Starter

    Joined:
    Jul 31, 2008
    Messages:
    92
    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 :)

    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.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  10. pure_evil020

    pure_evil020 Thread Starter

    Joined:
    Jul 31, 2008
    Messages:
    92
    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?
     
  11. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    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
    
     
  12. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1061822