【问题标题】:overwrite folders in visual basic script?覆盖Visual Basic脚本中的文件夹?
【发布时间】:2021-07-04 23:22:13
【问题描述】:

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

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”是我需要帮助的地方。谢谢!

【问题讨论】:

  • 您是否尝试过查看the official documentation 而不仅仅是好奇? - “如果指定的文件夹已经存在则出错”.
  • 是的,实际上。我已经查了几十次这个问题,检查了任何官方文档,以及如何创建文件夹的示例,但我总是得到“CopyFolder”结果,它对我不起作用。
  • 当你说覆盖文件夹是打算删除所有现有的内容?
  • 是的。但是我找不到任何帖子。

标签: vbscript wsh fso


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-10
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多