Lico_w
asked on
VBScript Permission Problem
I am trying to write some VBScript to cycle through all of the directories on my machine, find any files which have been modified in the last hour and output the full filename to a text file in it's current location. The file works fine when sat in a folder on my machine but as soon as I drop it on the root dir I get permission errors. I think this must be because I don't have read permissions on certain directories, is there a way around this? My code is below:
Dim sCurPath
sCurPath = CreateObject("Scripting.Fi leSystemOb ject").Get AbsolutePa thName("." )
'createDirectory = "C:\Documents and Settings\T104ahe\Desktop\M enu\TestSc ript\myNew Folder"
createFile = "\Modified_Files_List.txt"
' Create the File System Object
Set createobjFSO = CreateObject("Scripting.Fi leSystemOb ject")
'Set createobjFolder = createobjFSO.CreateFolder( createDire ctory)
Set createobjFile = createobjFSO.CreateTextFil e(sCurPath & createFile)
Wscript.Echo "File: " & createDirectory & createFile & " successfully created."
'------------------------- ---------- ---------- ---------- -
hourAgo = DateAdd("h", -1, Now)
'destDir = "C:\Documents and Settings\T104ahe\Desktop\M enu\TestSc ript\files Modified"
strFolder = sCurPath
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objFolder = objFSO.GetFolder(strFolder )
'WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
If objFile.DateLastModified > hourAgo Then _
createobjFile.WriteLine(ob jFile.path )
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If objFile.DateLastModified > hourAgo Then _
createobjFile.WriteLine(ob jFile.path )
Next
ShowSubFolders(objSubFolde r)
Next
End Sub
Dim sCurPath
sCurPath = CreateObject("Scripting.Fi
'createDirectory = "C:\Documents and Settings\T104ahe\Desktop\M
createFile = "\Modified_Files_List.txt"
' Create the File System Object
Set createobjFSO = CreateObject("Scripting.Fi
'Set createobjFolder = createobjFSO.CreateFolder(
Set createobjFile = createobjFSO.CreateTextFil
Wscript.Echo "File: " & createDirectory & createFile & " successfully created."
'-------------------------
hourAgo = DateAdd("h", -1, Now)
'destDir = "C:\Documents and Settings\T104ahe\Desktop\M
strFolder = sCurPath
Set objFSO = CreateObject("Scripting.Fi
Set objFolder = objFSO.GetFolder(strFolder
'WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
If objFile.DateLastModified > hourAgo Then _
createobjFile.WriteLine(ob
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If objFile.DateLastModified > hourAgo Then _
createobjFile.WriteLine(ob
Next
ShowSubFolders(objSubFolde
Next
End Sub
ASKER
Hi Rob, how would I do the error trapping as I'm not fussed to much about the ones I can't access but I want the script to continue to cycle through. At present it dies as soon as it hits an error.
Lico
Lico
Something like this should do. I've just added a simple error check to the ShowSubFolders routine.
Rob.
Rob.
Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
'createDirectory = "C:\Documents and Settings\T104ahe\Desktop\Menu\TestScript\myNewFolder"
createFile = "\Modified_Files_List.txt"
' Create the File System Object
Set createobjFSO = CreateObject("Scripting.FileSystemObject")
'Set createobjFolder = createobjFSO.CreateFolder(createDirectory)
Set createobjFile = createobjFSO.CreateTextFile(sCurPath & createFile)
Wscript.Echo "File: " & createDirectory & createFile & " successfully created."
'--------------------------------------------------------
hourAgo = DateAdd("h", -1, Now)
'destDir = "C:\Documents and Settings\T104ahe\Desktop\Menu\TestScript\filesModified"
strFolder = sCurPath
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
'WScript.Echo objFolder.Path
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
On Error Resume Next
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If objFile.DateLastModified > hourAgo Then _
createobjFile.WriteLine(objFile.path)
Next
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
ShowSubFolders(objSubFolder)
Next
If Err.number <> 0 Then WScript.Echo "Error reading " & objFolder.Path
Err.Clear
On Error GoTo 0
End Sub
ASKER
This script no longer works, it just tells me the errors and doesn't write the required data to the file. Can you have another look please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Rob, that's great, thanks. 2 minor points though when you get 5 then I'll close this thread....
1. I don't want it to echo the error, as there will be lots, can I just comment out these lines?
If Err.number <> 0 Then WScript.Echo "Error reading " & objFolder.Path
Err.Clear
2. What is this part doing?
If Right(sCurPath, 1) = "\" Then sCurPath = Left(sCurPath, Len(sCurPath) - 1)
createFile = "\Modified_Files_List.txt"
1. I don't want it to echo the error, as there will be lots, can I just comment out these lines?
If Err.number <> 0 Then WScript.Echo "Error reading " & objFolder.Path
Err.Clear
2. What is this part doing?
If Right(sCurPath, 1) = "\" Then sCurPath = Left(sCurPath, Len(sCurPath) - 1)
createFile = "\Modified_Files_List.txt"
Hi,
1. Yes, but you can leave in Err.Clear...that only clears the error state
2. The first line there ensures that the value in sCurPath *does not* end in a backslash. This is done so that we *know* in the next line, we must put backslash then the file name. I just do this as a matter of practice because from experience, a lot of people will have, say
sCurPath = "C:\MyFolder\"
createFile = "\MyFile.txt"
and then, when those two are put together, you end up with two backslashes together, which results in an error.
Regards,
Rob.
1. Yes, but you can leave in Err.Clear...that only clears the error state
2. The first line there ensures that the value in sCurPath *does not* end in a backslash. This is done so that we *know* in the next line, we must put backslash then the file name. I just do this as a matter of practice because from experience, a lot of people will have, say
sCurPath = "C:\MyFolder\"
createFile = "\MyFile.txt"
and then, when those two are put together, you end up with two backslashes together, which results in an error.
Regards,
Rob.
ASKER
Very helpful with the solution, truly deserves his Genius ranking.
sCurPath = CreateObject("Scripting.Fi
if you need the script in the root folder, but want to start at a subfolder, then use
sCurPath = "C:\MyFolder"
But, ultimately, if you don't have read permissions, you will need to use an account that does.
You could add error trapping so that the script would continue by skipping any file or folder it couldn't read, but it still relies on the permission your current account has.
Regards,
Rob.