Solved

VBScript Permission Problem

Posted on 2010-09-07
8
457 Views
Last Modified: 2012-08-14
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.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

Set colFiles = objFolder.Files

For Each objFile In colFiles
        If objFile.DateLastModified > hourAgo Then _
            createobjFile.WriteLine(objFile.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(objFile.path)
    Next
    ShowSubFolders(objSubFolder)
  Next
End Sub
0
Comment
Question by:Lico_w
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 33616103
The script will start at the current directory the script is in, given by this line:
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

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.
0
 

Author Comment

by:Lico_w
ID: 33616165
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33616225
Something like this should do.  I've just added a simple error check to the ShowSubFolders routine.

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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lico_w
ID: 33616314
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?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 33622841
This now works for me.  It might be the permissions issue on a folder that is preventing you from enumerating the rest of them.

Regards,

Rob.
Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
'createDirectory = "C:\Documents and Settings\T104ahe\Desktop\Menu\TestScript\myNewFolder"
If Right(sCurPath, 1) = "\" Then sCurPath = Left(sCurPath, Len(sCurPath) - 1)
createFile = "\Modified_Files_List.txt"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Set createobjFolder = createobjFSO.CreateFolder(createDirectory)
Set objOutput = objFSO.CreateTextFile(sCurPath & createFile, True)
Wscript.Echo "File: " & sCurPath & createFile & " successfully created."

'--------------------------------------------------------

hourAgo = DateAdd("h", -1, Now)
'destDir = "C:\Documents and Settings\T104ahe\Desktop\Menu\TestScript\filesModified"

Set objFolder = objFSO.GetFolder(sCurPath)
'WScript.Echo objFolder.Path

ShowSubFolders(objFolder)

objOutput.Close

Sub ShowSubFolders(objFolder)
    On Error Resume Next
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
         If objFile.DateLastModified > hourAgo Then _
            objOutput.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

Open in new window

0
 

Author Comment

by:Lico_w
ID: 33624894
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"
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33625326
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.
0
 

Author Closing Comment

by:Lico_w
ID: 33627639
Very helpful with the solution, truly deserves his Genius ranking.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vector, list Questions R 6 45
Records from Access to Excel to specific cells 5 36
Subtraction v Hex2Dec in vbscript 6 30
how to read the text file and convert it to csv. 5 35
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question