Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reference home folder on visual basic script

Tags:

vbscript

Let me start by saying that I'm a linux guy and not really familiar with VBS or even windows global variables.

I'm being called upon to update a VBS script which basically copies the latest version of a access form to the computer. Currently it puts this access form in C:\MedMaint. The problem is that we do not run as administrators in this location. So when a new user tries to access the vbs script, the folder must be deleted by the original user. I need to change this script to the linux equivalant of ~/MedMaint, or "C:\Documents and Settings\MyUserName\Application Data\MedMaint"

Here is a sample of the code

    If Not FSO.FileExists("c:\MedMaint\" & File.Name) Then
        FSO.CopyFile File.Path, "c:\MedMaint\"          ' copy the missing file
    Else 
        Set RPFile = FSO.GetFile("c:\MedMaint\" & File.Name)    ' Get the file object from the local object
        If (File.DateLastModified >= RPFile.DateLastModified) Then
            FSO.CopyFile File.Path, "c:\MedMaint\" 

I would like to know how to change the c:\MedMaint\ reference to the user's home dir

like image 926
Adam Outler Avatar asked Oct 05 '10 16:10

Adam Outler


People also ask

How do I change the current directory in VBScript?

You can use WScript. ScriptFullName which will return the full path of the executing script. Since OP asked about VBScript, I would like to caution readers on Jakob's first code line, which is an JScript example.

What is FileSystemObject in VBScript?

With the FSO model's File object, you can manipulate files in VBScript applications. The File object represents a file in a Windows file system. After you access the File object, you can use the object's methods and properties to work with it.

What is WScript echo in VBScript?

You can use WScript. Echo to see exactly what VBScript passes out; if the string you see in the result of WScript. Echo works in a command-prompt window, it will work as the first argument for the Run method.


2 Answers

To get the path of the user's profile folder (e.g. C:\Documents and Settings\<username> in Windows XP or C:\Users\<username> in Windows Vista), you can do any of the following:

  • Evaluate the USERPROFILE environment variable using the WshShell.ExpandEnvironmentStrings method:

    Set oShell = CreateObject("WScript.Shell")
    strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
    
  • Retrieve the folder path using the Shell.Namespace method:

    Const ssfPROFILE = &H28
    Set oShell = CreateObject("Shell.Application")
    strHomeFolder = oShell.NameSpace(ssfPROFILE).Self.Path
    

If you need the path of the application data folder (e.g. C:\Documents and Settings\<username>\Application Data in Windows XP or C:\Users\<username>\AppData\Roaming in Windows Vista), you can use similar code:

Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%APPDATA%")

''# or

Const ssfAPPDATA = &H1A
Set oShell = CreateObject("Shell.Application")
strHomeFolder = oShell.NameSpace(ssfAPPDATA).Self.Path


To append a folder name to a path, you could simply use string concatenation like this:

strMedMaintFolder = strHomeFolder & "\MedMaint"

Alernatively, if your script contains many path concatenations, I suggest using the FileSystemObject.BuildPath method, because it takes care of path separators (\) for you:

Set oFSO = CreateObject("Scripting.FileSystemObject")
strMedMaintFolder = fso.BuildPath(strHomeFolder, "MedMaint") 
like image 147
Helen Avatar answered Nov 24 '22 05:11

Helen


You can use the Shell Object.

Have a look at this link.

like image 37
Raj Avatar answered Nov 24 '22 05:11

Raj