Question:
VBScript opentextfile "permission denied"?
anonymous
2011-04-26 12:20:55 UTC
If I try to create and/or open a text file without specifying a directory, it creates it with no problem in whatever directory I'm running the .vbs file out of. If i specify a path, I get a "Permission denied" error. Here is the code snippet (commented out section is the nonworking path):

savestring = "file's contents"

'filename = "C:\mytextfile.txt"
filename = "mytextfile.txt"

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set myWrite = myFSO.OpenTextFile(filename, 2, True)
myWrite.WriteLine(savestring)
myWrite.Close
SET myWrite = NOTHING
SET myFSO = NOTHING
Four answers:
The Phlebob
2011-04-26 12:50:42 UTC
There seems to be Administrator protection on the root (C:\) now (Vista, for me). I just tried to copy a file in the directory via Windows Explorer and got a UAC-type box.



Try another directory you're sure you can write in to see if that's the problem.



Hope that helps.
seabolt
2016-12-12 14:34:37 UTC
Opentextfile Vbscript
?
2016-09-30 13:31:07 UTC
Vbscript Opentextfile
Jeremy
2011-04-26 12:33:46 UTC
Here is something similar to what you seem t be trying to do. Need to run it with cscript at command prompt, Can also change variables to hold specific paths , but this way is dynamic and forces you to type a path every time



option explicit

dim objfso , objfile, file , path, fname



set objfso = createobject("Scripting.filesystemobject")

wscript.stdout.write " Enter path to create file in ( ex. C:\Users\Student\Desktop)"

path = wscript.stdin.readline

wscript.stdout.write " Enter filename and extension ( ex. File.txt) "

fname = wscript.stdin.readline

file = path & "\" & fname

set objfile = objfso.createtextfile(file)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...