Prev: Retreaving a Page from a Master Web Site
Next: ANN: VBScript add-ons for (Microsoft) SysInternals BgInfo
From: obyapka on 16 Jun 2010 15:15 I'm writing some VBScript to convert one text file in to another format. If I hard code the names input = FSO.OpenTextFile("file1.txt", 1) output = FSO.OpenTextFile("file2.txt",2,true) Then everything works fine, and file1 is converted in to file2 successfully. However if I utilise arguments, and drag and drop a filename on to the vbscript... myFile = wscript.arguments(0) input = FSO.OpenTextFile(myFile,1) output = FSO.OpenTextFile("file2.txt",2,true) my code successfully opens the input file, but fails to open file2.txt for writing... I receive "Permission denied, code 800A0046". If I run this from a command prompt cscript myscript.vbs file1.txt I get the same error on the file that I want write to - and this file does not exist! Can someone please advise? I do need to utilise drag and drop of a filename on to the script rather than hard code a file name, as the input filename will differ.
From: Pegasus [MVP] on 16 Jun 2010 15:23 "obyapka" <obyapka(a)discussions.microsoft.com> wrote in message news:AB931AB3-07BE-4009-900B-5FD8E478AE63(a)microsoft.com... > I'm writing some VBScript to convert one text file in to another format. > If I > hard code the names > input = FSO.OpenTextFile("file1.txt", 1) > output = FSO.OpenTextFile("file2.txt",2,true) > > Then everything works fine, and file1 is converted in to file2 > successfully. > > However if I utilise arguments, and drag and drop a filename on to the > vbscript... > > myFile = wscript.arguments(0) > input = FSO.OpenTextFile(myFile,1) > output = FSO.OpenTextFile("file2.txt",2,true) > > my code successfully opens the input file, but fails to open file2.txt for > writing... > I receive "Permission denied, code 800A0046". > > If I run this from a command prompt > cscript myscript.vbs file1.txt > > I get the same error on the file that I want write to - and this file does > not exist! > > Can someone please advise? > > I do need to utilise drag and drop of a filename on to the script rather > than hard code a file name, as the input filename will differ. What folder will file2.txt be in? Do you have read/write access to it? What happens when you replace oOutput = FSO.OpenTextFile("file2.txt",2,true) with set oOutput = FSO.CreateTextFile("c:\file2.txt",true)
From: obyapka on 16 Jun 2010 16:08 Sorry, I should have included the Set in my example code, it came from memory not copy and paste. Surely I have permissions if I can run this in the first example successfully? It's only when I pass a filename to open as an argument, that the file for output fails on permissions. I'm reading and writing files to the same folder in both examples. Does it matter than I'm writing and testing this on Windows 7? It will be running on Win7, but I also want to give this to a colleague to run on WinXP. "Pegasus [MVP]" wrote: > > > "obyapka" <obyapka(a)discussions.microsoft.com> wrote in message > news:AB931AB3-07BE-4009-900B-5FD8E478AE63(a)microsoft.com... > > I'm writing some VBScript to convert one text file in to another format. > > If I > > hard code the names > > input = FSO.OpenTextFile("file1.txt", 1) > > output = FSO.OpenTextFile("file2.txt",2,true) > > > > Then everything works fine, and file1 is converted in to file2 > > successfully. > > > > However if I utilise arguments, and drag and drop a filename on to the > > vbscript... > > > > myFile = wscript.arguments(0) > > input = FSO.OpenTextFile(myFile,1) > > output = FSO.OpenTextFile("file2.txt",2,true) > > > > my code successfully opens the input file, but fails to open file2.txt for > > writing... > > I receive "Permission denied, code 800A0046". > > > > If I run this from a command prompt > > cscript myscript.vbs file1.txt > > > > I get the same error on the file that I want write to - and this file does > > not exist! > > > > Can someone please advise? > > > > I do need to utilise drag and drop of a filename on to the script rather > > than hard code a file name, as the input filename will differ. > > What folder will file2.txt be in? Do you have read/write access to it? > > What happens when you replace > oOutput = FSO.OpenTextFile("file2.txt",2,true) > with > set oOutput = FSO.CreateTextFile("c:\file2.txt",true) > > > . >
From: Pegasus [MVP] on 16 Jun 2010 16:19 "obyapka" <obyapka(a)discussions.microsoft.com> wrote in message news:EB7C3A34-F229-40D4-BB17-94656EA9D718(a)microsoft.com... > Sorry, I should have included the Set in my example code, it came from > memory > not copy and paste. > > Surely I have permissions if I can run this in the first example > successfully? It's only when I pass a filename to open as an argument, > that > the file for output fails on permissions. I'm reading and writing files > to > the same folder in both examples. > > Does it matter than I'm writing and testing this on Windows 7? It will be > running on Win7, but I also want to give this to a colleague to run on > WinXP. Windows 7 matters only with respect to folder access rights: Many folders that were fully accessible under previous versions have severe restrictions under Win7. Note that my code line did more than just add the word "set". Furthermore, I suggested you should run some tests with slightly different settings. Posting code from memory is bad news. Anything could be wrong. I won't make any further comments on your code - please use the standard technique of copying and pasting your code.
From: Todd Vargo on 16 Jun 2010 23:47
obyapka wrote: > I'm writing some VBScript to convert one text file in to another format. > If I > hard code the names > input = FSO.OpenTextFile("file1.txt", 1) > output = FSO.OpenTextFile("file2.txt",2,true) > > Then everything works fine, and file1 is converted in to file2 > successfully. > > However if I utilise arguments, and drag and drop a filename on to the > vbscript... > > myFile = wscript.arguments(0) > input = FSO.OpenTextFile(myFile,1) > output = FSO.OpenTextFile("file2.txt",2,true) > > my code successfully opens the input file, but fails to open file2.txt for > writing... > I receive "Permission denied, code 800A0046". > > If I run this from a command prompt > cscript myscript.vbs file1.txt > > I get the same error on the file that I want write to - and this file does > not exist! > > Can someone please advise? > > I do need to utilise drag and drop of a filename on to the script rather > than hard code a file name, as the input filename will differ. Note, when you drop a file, the system takes whatever folder it last used as the current directory. Sometimes that is the desktop, often it is not. And usually always, it is never the folder that the file was dragged from or the folder that the script or executable was located in. So what can you do? Well, since you want the new file created in the same folder the first file was located in, you simply extract the full path from the first file, modify it, and then save. Set fso = CreateObject("Scripting.FileSystemObject") myFile1 = wscript.arguments(0) extLength = Len(fso.GetExtensionName(myFile1)) If extLength > 0 Then extLength = extLength + 1 myFile2 = Left(myFile1, Len(myFile1) - extLength) _ & "(2)" & Right(myFile1, extLength) Set input = FSO.OpenTextFile(myFile1, 1) Set output = FSO.OpenTextFile(myFile2, 2, true) HTH -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) |