如何在visual basic脚本上引用主文件夹 [英] how to reference home folder on visual basic script

查看:31
本文介绍了如何在visual basic脚本上引用主文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说我是一个 Linux 人,对 VBS 甚至 Windows 全局变量并不十分熟悉.

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

我被要求更新 VBS 脚本,该脚本基本上将最新版本的访问表单复制到计算机.目前,它将此访问表单放在 C:\MedMaint 中.问题是我们不在这个位置以管理员身份运行.所以当新用户尝试访问vbs脚本时,必须由原用户删除该文件夹.我需要将此脚本更改为 ~/MedMaint 的 linux 等效项,或C:\Documents and Settings\MyUserName\Application Data\MedMaint"

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"

这是代码示例

    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\" 

我想知道如何将 c:\MedMaint\ 引用更改为用户的主目录

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

推荐答案

获取用户配置文件文件夹的路径(例如C:\Documents and Settings\ 在 Windows XP 中或 C:\Users\ 在 Windows Vista 中),您可以执行以下任一操作:

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%")

  • 使用Shell.Namespace<检索文件夹路径/code> 方法:

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

  • 如果您需要应用程序数据文件夹的路径(例如 C:\Documents and Settings\\Application Data 在 Windows XP 或 C:\Users\\AppData\Roaming(在 Windows Vista 中),您可以使用类似的代码:

    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"
    

    或者,如果您的脚本包含许多路径串联,我建议使用 FileSystemObject.BuildPath 方法,因为它会为您处理路径分隔符 (\):

    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") 
    

    这篇关于如何在visual basic脚本上引用主文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆