0

I'm trying to create a line in VBS that will create a file path with the file name as a variable, but I'm getting a Permission Denied error.

This is what I have so far:

filename = WScript.Arguments.unnamed(0) 'this value is transfered from a batch file.
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = true

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load ("C:\Users\c1921\Ayla_Data\XMLFile.xml")

Set colNodes = xmlDoc.SelectNodes _

("/properties/property/(name)")

For Each objNode In colNodes

strSource = "C:\Users\c1921\Ayla_Data\AylaDatapoints\" & filename & ".csv"

Dim fso, f
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso.OpenTextFile(strSource, 2) 

I've also added a msgbox(filename) line to see what value I get and it is the correct value.

I've also tried something like this to see if it would help and it doesn't work either:

Set f = fso.OpenTextFile("C:\Users\c1921\Ayla_Data\AylaDatapoints\AC000N000004593.csv", 2)

This should be easy I don't know where I'm going wrong. Any help finding the right direction would be greatly appreciated.

I've also tried something like this (among other variations) if it's relevant:

Set f = fso.OpenTextFile("C:\Users\c1921\Ayla_Data\AylaDatapoints\" & Chr(34) & filename & Chr(34) & " " & ".csv", 2) & Chr(34)

Edit:

Tried making the file in VBs with this code:

strSource = "C:\Users\c1921\Ayla_Data\AylaDatapoints\" & filename & ".csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile(strSource, _ 
    ForWriting, True)

objLogFile.Writeline

I still get Permission Denied run time error. The file is created but this is the only thing written in it:

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Daniella
  • 171
  • 2
  • 3
  • 14
  • Its probably due to UAC settings. Ususally you will need to access the progtam with "Run as Administrator" option to access the profile folders. Are you able to access your csv file using a normal command prompt? and on elevated command prompt console? – gbabu Jul 25 '14 at 16:25
  • Check out this post which tells you how to use vbs to invoke an elevated command prompt. http://stackoverflow.com/questions/18755553/automatically-running-a-batch-file-as-an-administrator – gbabu Jul 25 '14 at 16:27
  • I can access it from the command prompt. I'm not sure what you mean by elevated command prompt. – Daniella Jul 25 '14 at 16:28
  • `elevatecmd.exe` gives an Object required error. Do I need to declare it? – Daniella Jul 25 '14 at 16:30
  • I may be wrong, but from the 'access denied' error message it appears to be UAC (User Access Control) permissions. Is it working if the path – gbabu Jul 25 '14 at 16:31
  • I'm sorry, I am not good at VBS. Just answered it as this was tagged with `batch-file` – gbabu Jul 25 '14 at 16:32
  • Thank you anyway. I believe my problem is that I haven't closed the reference to the file in batch. Since the batch file is still running I need to close the instance of the file before starting VBs. I'll see if that works. – Daniella Jul 25 '14 at 16:55
  • @Ella - what do you want to do with the .csv files? – Ekkehard.Horner Jul 25 '14 at 17:14
  • I have a line in my batch file that writes all the output of the VBs file to the .csv file: `cscript C:\Users\c1921\Ayla_Data\curltest.vbs "!$Unit!" > c:\Users\c1921\Ayla_Data\AylaDatapoints\!$Unit!.csv` – Daniella Jul 25 '14 at 17:24

1 Answers1

1

If your .bat file re-directs the (StdOut) output of curltest.vbs into a .csv file then it makes no sense to create (or write to) the .csv in the .vbs. Instead you should WScript.Echo the info you want to appear in the .csv.

In addition, you need to get rid of the logo - either by starting the .vbs with //NoLogo or 'burning' the //NoLogo switch into the user's c/wscript by using the //S option.

Evidence:

copy con curltest.vbs
WScript.Echo "WScript.Echoed into whatever.csv"
^Z

cscript //NoLogo curltest.vbs >whatever.csv

type whatever.csv
WScript.Echoed into whatever.csv
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96