Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permission Denied Error on VBScript 2

Status
Not open for further replies.

Bakeyman

Technical User
Mar 3, 2008
8
0
0
I'm trying to write a script to drill through a specified folder path and return the last modified date of all files it sees out to a text file. I've got everything working, except the part where I create the text file and then try to open it. If I run the script twice in a row (so it doesn't have to create the file and then open it) the script runs fine. There seems to be some problem centered around creating the file and opening it during the same script.

Disclaimer: I am not a programmer, so don't laugh too hard at my code! ;)

Here's my code:

Code:
Option Explicit

'on error resume next

Dim objFSO 
Dim objFolder 
Dim colFiles 
Dim objSubFolder 
Dim objSubFile 
Dim strFolder 
Dim File 
Dim objFile 
Dim colFolders
Dim path
Dim filecreate
Dim filetxt
Dim strLine
Dim input 
Dim WriteStuff

Input = InputBox("Enter folder path") 

  strFolder = "c:\"& Input
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFSO.GetFolder(strFolder)
  Set colFiles = objFolder.Files
  filetxt= "c:\FileReports\"& Input &".txt"
  

If not objFSO.FileExists(filetxt) Then
  set filecreate = objFSO.CreateTextFile(filetxt, True)
  Set WriteStuff = objFSO.OpenTextFile(filetxt, 8, True)
Else
  Set WriteStuff = objFSO.OpenTextFile(filetxt, 8, True)
End if

For Each File in colFiles
  set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
  WriteStuff.WriteLine(objFile.Path & " - " & "Last accessed: " & objFile.DateLastAccessed)
Next

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders

For Each objSubFolder In colFolders
Set colFiles = objSubFolder.Files

For Each objFile in Colfiles
   WriteStuff.WriteLine(objFile.Path & " - " & "Last accessed: " & objFile.DateLastAccessed)
Next
ScanSubFolders(objSubFolder)
Next
End Sub

set objFSO = Nothing
set objFolder = Nothing
set colFiles = Nothing
set objSubFolder = Nothing
set objSubFile = Nothing
set strFolder = Nothing 
set File = Nothing 
set objFile = Nothing 
set colFolders = Nothing
set path = Nothing
set filecreate = Nothing
set filetxt = Nothing
set strLine = Nothing
set input = Nothing
set WriteStuff = Nothing

wscript.Quit
 
Replace this:
set filecreate = objFSO.CreateTextFile(filetxt, True)
Set WriteStuff = objFSO.OpenTextFile(filetxt, 8, True)
with this:
set WriteStuff = objFSO.CreateTextFile(filetxt, True)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You can't create, then instantly 'open' the same text file like u did.

the OPEN command already has an argument to create it if not there and you're already using it!
object.OpenTextFile(filename[, iomode[, create[, format]]])

Otherwise you need to CLOSE the txt file after the create. As well as always closing it when you're done.

[yinyang] Tranpkp [pc2]
 
>If not objFSO.FileExists(filetxt) Then
> set filecreate = objFSO.CreateTextFile(filetxt, True)
> Set WriteStuff = objFSO.OpenTextFile(filetxt, 8, True)
>Else
> Set WriteStuff = objFSO.OpenTextFile(filetxt, 8, True)
>End if


Replace those lines by this line.
[tt]
Set WriteStuff = objFSO.OpenTextFile(filetxt, 8, True)
[/tt]
 
Thanks guys and especially tsuji for the explanation along with the code fix. It works beautifully now, so feel free to use it. :)
 
Correction, Tranpkp gets the nod for the explanation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top