首页 > 解决方案 > 覆盖Visual Basic脚本中的文件夹?

问题描述

如果文件夹已经存在,我正在研究一种覆盖文件夹的方法,并确认。这是我的代码(以及我坚持的部分):

Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
Set network = CreateObject("WScript.Network")
username = network.userName
userprofile = shell.ExpandEnvironmentStrings("%userprofile%")

If fso.FolderExists(userprofile + "\foldername") = False Then
    fso.CreateFolder(userprofile & "\foldername")
    End If
If fso.FolderExists(userprofile + "\foldername") = True Then
    overwrite = msgbox("The directory that foldername uses(" & userprofile + "\foldername) is unavailable. Overwrite?",4+16+4096,"")
        if overwrite = vbYes then
            overwrite2 = msgbox("THIS IS YOUR LAST WARNING. Any files in " & userprofile + "\foldername will be PERMANENTLY LOST. Continue?",4+16+4096,"")
                if overwrite2 = vbYes then
                    'overwriting folder goes here
                    End If
                if overwrite2 = vbNo then
                    End If
        if overwrite = vbNo then
        End If

overwriting folder goes here”是我需要帮助的地方。谢谢!

标签: vbscriptwshfso

解决方案


当您说覆盖文件夹时,是打算删除所有现有内容吗?如果是这样,您可以先删除该文件夹然后重新创建它吗?

if overwrite2 = vbYes then
    strFolderPath = userprofile & "\foldername"
    fso.DeleteFolder strFolderPath, true
    fso.CreateFolder(strFolderPath)
    End If

推荐阅读