0

I'm working on a way to overwrite a folder if it already exists, with confirmation. Here is my code(along with the part I'm stuck on):

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

The "overwriting folder goes here" is where I need help. Thanks!

Stylerr.
  • 33
  • 6
  • 1
    Have you tried looking at [the official documentation](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/7kby5ae3(v=vs.84)) instead of just being curious? - *"An error occurs if the specified folder already exists"*. – user692942 Apr 09 '21 at 07:31
  • Yes, actually. I've looked this question up dozens of times, checking any official documentation, and examples of how to create folders, but I always get the "CopyFolder" result, and it doesn't work for me. – Stylerr. Apr 09 '21 at 21:17
  • When you say overwrite folder is the intention to delete all existing content? – Moir Apr 13 '21 at 01:48
  • Yes. I can't find any posts of this however. – Stylerr. Apr 21 '21 at 15:56

1 Answers1

0

When you say overwrite folder is the intention to delete all existing content? If so could you delete the folder first and then recreate it?

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