0

I am getting a 'Permission denied' error when trying to run this VBScript after dragging another file or folder icon onto the script's icon.

I have tried running it on two Windows 10 machines and if I run it with arguments if objects at the file creation line, otherwise the scripts completes normally.

Any ideas?

Option Explicit

Dim objFSO
Dim objShell

Dim fldTest
Dim fileTest
Dim strPath
Dim txsOutput

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txsOutput = objFSO.CreateTextFile("Test.txt")

For Each strPath In WScript.Arguments
    fldTest = objFSO.GetFolder(strPath)

    For Each fleTest In fldTest.Files
        ...
    Next
Next

txsOutput.Close()

Set txsOutput = Nothing
Set objFSO = Nothing

WScript.Quit
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    Its not clear what are you trying to achieve? Why are you dragging the file or folder over the vbs script? – Pankaj Jaju Sep 12 '17 at 22:47
  • 1
    Which line is the error in? `.GetFolder` returns an object, use `Set fldTest = objFSO.GetFolder(strPath)`, also specify full path within `.CreateTextFile`, try to accumulate a data in a variable and then write it content into file at once (take a look at `Sub WriteTextFile()` from [this](https://stackoverflow.com/a/43265363/2165759) answer). – omegastripes Sep 12 '17 at 23:12
  • If you're actually getting a permission denied error in the line `Set txsOutput = objFSO.CreateTextFile("Test.txt")` you're probably dropping another item on the script while it's still processing the first item or group of items. That invokes a *new* instance of the script, which tries to create the same file that the already running instance still has opened, thus causing the error. – Ansgar Wiechers Sep 12 '17 at 23:22
  • the code which you have given is working fine for me and creating new file.But what is your requirement ? – SaiPawan Sep 13 '17 at 13:32
  • I eventually traced the fault to strPath being incorrectly constructed in unshown code, found quickly after posting my plea for help. – Neil Mudford Sep 14 '17 at 23:12

0 Answers0