参考Windows用户/用户名文件夹

时间:2017-07-26 19:53:00

标签: windows vbscript

我需要将个人Excel工作簿文件从网络驱动器复制到大约20台不同PC上的C:\ Users \ username \ AppData \ Roaming \ Microsoft \ Excel \ XLSTART。我想简化这一点,因为它可能会成为一项更常见的任务。

如果我在DestinationFile声明中实际使用了硬编码的用户名,那么我的当前代码是有效的。

Const DestinationFile = "C:\Users\username\AppData\Roaming\Microsoft\Excel\XLSTART\Personal.xlam"
Const SourceFile = "H:\Folder\Folder\Folder\Personal.xlam"

Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(DestinationFile) Then
    'Check to see if the file is read-only
    If Not fso.GetFile(DestinationFile).Attributes And 1 Then 
        'The file exists and is not read-only.  Safe to replace the file.
        fso.CopyFile SourceFile, "H:\Folder\Folder\Folder\Folder\", True
    Else 
        'The file exists and is read-only.
        'Remove the read-only attribute
        fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
        'Replace the file
        fso.CopyFile SourceFile, "H:\Folder\Folder\Folder\Folder\", True
        'Reapply the read-only attribute
        fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
    End If
Else
    'The file does not exist in the destination folder.  Safe to copy file to this folder.
    fso.CopyFile SourceFile, "H:\Folder\Folder\Folder\Folder\", True
End If
Set fso = Nothing

1 个答案:

答案 0 :(得分:2)

以下是确定AppData文件夹的方法,该文件夹看起来是您的主要问题:

Dim shell
Set shell = CreateObject("WScript.Shell")
MsgBox shell.ExpandEnvironmentStrings("%APPDATA%")