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!

monitor a specific directory for files

Status
Not open for further replies.

moogeboo

MIS
Aug 7, 2003
28
0
0
US
hi,

i'm trying to write a script that monitors a specific directory under a parent folder.i.e, have a users folder, underneath the users folder i'll have user1, user2, user3, etc..and within each of these user folders i'll have folders called in and out. Now, what i'd like to do is monitor only the out folder within each of the user folders for files. If it detects a file, then etc..I have the script down for the detection and the action of the files, but what i'm struggling with is how to write the script whereby it detects only the files in the out folder, and if i add additional user folders like user 24, user25, and so on, the script will continue to enumerate each user folder, and look for files within the out folder for each user.

thanks for any assistance.....
M
 
Here, I tossed this together for ya, just modify the path to point to the root folder that contains all of your user folders.


'Force script to run in csript
RunWithCscript

Dim fs,folder,wshShell,msg
Set fs = CreateObject("Scripting.FileSystemObject")
Set wshshell = CreateObject("WScript.Shell")
Folder = "C:\"

Set folder = fs.getfolder(folder)
Wscript.echo "Processing " &folder
CheckFolderforFiles(Folder)
msgbox "Complete"

Sub CheckFolderforFiles(folder)
For Each subfolder In folder.subfolders
wscript.echo Subfolder.path
If FS.FolderExists(Subfolder.Path & "\Out") Then
Set OutFldr=Fs.GetFolder(Subfolder.path & "\Out")
For Each File In OutFldr.files
FileCount=FileCount+1
wscript.echo File
'Add Code here to perform whatever action you need on the files....
Next
End IF
Next
End Sub

'This subroutine calls this script with cscript.exe then closes wscript
Sub RunWithCscript
If Lcase(Mid(Wscript.Fullname, InstrRev(Wscript.fullname,"\")+1))="cscript.exe" Then
'nothing
else
Set WshShell=CreateObject("Wscript.Shell")
WshShell.run "Cscript.exe """ & Wscript.ScriptFullName & """"
WScript.Quit 'Quit Wscript
end if
End Sub
 
thanks misterniceguy...i appreciate the effort a lot...

now, i'm wondering if this scenario can be done...after the script finds files from each of the "out" folders for each user, how could it copy the files to the correspdonding user/out box for each user? that is, if it finds files in the out box for user12 and user34, only copy those files to another server's user12 and user34 out folders. the other server's folder structure would mirror that of the original server. kind of like synching, but intelligently synching of only specific folders within a user folder.

i'm just a beginner in vbscript, so this is way over my capability. don't know if is is possible to do this though....

thanks for any help you can offer....
M
 
'You will need to modify the server1 and server2 names, it's not very pretty..but should do the trick.

'Force script to run in csript
RunWithCscript

Dim fs,folder,wshShell,msg
Set fs = CreateObject("Scripting.FileSystemObject")
Set wshshell = CreateObject("WScript.Shell")
Folder = "C:\"

Set folder = fs.getfolder(folder)
Wscript.echo "Processing " &folder
CheckFolderforFiles(Folder)
msgbox "Complete"

Sub CheckFolderforFiles(folder)
For Each subfolder In folder.subfolders
wscript.echo Subfolder.path
If FS.FolderExists(Subfolder.Path & "\Out") Then
Set OutFldr=Fs.GetFolder(Subfolder.path & "\Out")
For Each File In OutFldr.files
FileCount=FileCount+1
wscript.echo File
Synchronize_Location=Replace(lcase(File.Path),"C:\drvrtmp","c:\")
If Synchronize_Location <> File.Path Then
wscript.echo &quot;Copying &quot; & File.Path & &quot; to &quot; & Synchronize_Location
Fs.CopyFile File.Path,Synchronize_Location
End If
Next
End IF
Next
End Sub

'This subroutine calls this script with cscript.exe then closes wscript
Sub RunWithCscript
If Lcase(Mid(Wscript.Fullname, InstrRev(Wscript.fullname,&quot;\&quot;)+1))=&quot;cscript.exe&quot; Then
'nothing
else
Set WshShell=CreateObject(&quot;Wscript.Shell&quot;)
WshShell.run &quot;Cscript.exe &quot;&quot;&quot; & Wscript.ScriptFullName & &quot;&quot;&quot;&quot;
WScript.Quit 'Quit Wscript
end if
End Sub
 
'oops, I forgot to clean up my testing paths. Here is the script I meant to post..

'Force script to run in csript
RunWithCscript

Dim fs,folder,wshShell,msg
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set wshshell = CreateObject(&quot;WScript.Shell&quot;)
Folder = &quot;C:\&quot;

Set folder = fs.getfolder(folder)
Wscript.echo &quot;Processing &quot; &folder
CheckFolderforFiles(Folder)
msgbox &quot;Complete&quot;

Sub CheckFolderforFiles(folder)
For Each subfolder In folder.subfolders
wscript.echo Subfolder.path
If FS.FolderExists(Subfolder.Path & &quot;\Out&quot;) Then
Set OutFldr=Fs.GetFolder(Subfolder.path & &quot;\Out&quot;)
For Each File In OutFldr.files
FileCount=FileCount+1
wscript.echo File
Synchronize_Location=Replace(lcase(File.Path),&quot;server1&quot;,&quot;server2&quot;)
If Synchronize_Location <> File.Path Then
wscript.echo &quot;Copying &quot; & File.Path & &quot; to &quot; & Synchronize_Location
Fs.CopyFile File.Path,Synchronize_Location
End If
Next
End IF
Next
End Sub

'This subroutine calls this script with cscript.exe then closes wscript
Sub RunWithCscript
If Lcase(Mid(Wscript.Fullname, InstrRev(Wscript.fullname,&quot;\&quot;)+1))=&quot;cscript.exe&quot; Then
'nothing
else
Set WshShell=CreateObject(&quot;Wscript.Shell&quot;)
WshShell.run &quot;Cscript.exe &quot;&quot;&quot; & Wscript.ScriptFullName & &quot;&quot;&quot;&quot;
WScript.Quit 'Quit Wscript
end if
End Sub
 
I have been trying to do something like this myself, but not quite so difficult. I am trying to write a very simple little script that can be run that will move any files of a certain extension from the folder they are in to another folder. This seems to work fine if I either don't use the If statement and let the "On Error..." take over, or if I state the specific filename in the variable. The "fso.MoveFile AxisTif, XeroxScanDir" line works fine by itself and uses the wildcard in the "AxisTif" variable fine, but the "If fso.FileExists(AxisTif) Then" line is actually looking for a file called "*.tif" instead of interpreting it as a wildcard. Is this just the way it is? Is there another way of doing this? I need the If statement because there's other stuff I need to do also if a tif file is detected in the C:\Axis dir. Thanks for any help offered! See script below.

On Error Resume Next
XeroxScanDir = "C:\XeroxScan"
AxisTif = "C:\Axis\*.tif"
If fso.FileExists(AxisTif) Then
fso.MoveFile AxisTif, XeroxScanDir
End If
 
Something like this ?
Code:
XeroxScanDir = "C:\XeroxScan\"
AxisDir = "C:\Axis"
Set xs=fso.GetFolder(AxisDir)
For Each f in xs.Files
  If Right(LCase(f.Name),4)=".tif" Then
    f.Move XeroxScanDir
  End If
Next

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Well, I think this might work, but it keeps saying on line "f.Move ScanDir" that "file already exists". The tif file I put in the C:\Axis dir does not exist in the C:\XeroxScan dir though. Any ideas? Thanks!
 
And this ?
Code:
XeroxScanDir = "C:\XeroxScan\"
AxisDir = "C:\Axis"
Set xs=fso.GetFolder(AxisDir)
For Each f in xs.Files
  If Right(LCase(f.Name),4)=".tif" Then
    If Not fso.FileExists(XeroxScanDir & f.Name) Then 
      f.Move XeroxScanDir
    Else
      MsgBox f.Name & " exists in " & XeroxScanDir
    End If
  End If
Next
BTW, if you have a error handler don't forget to use the Err.Clear method.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Same error on the same line. At the top of my script, I have On Error Resume Next.
 
Actually, I had the On Error Resume Next remmed out. I unremmed it and now I don't get the runtime error, but it doesn't move the file either.
 
Works for me.
BTW, XeroxScanDir = "C:\XeroxScan[blue]\[/blue]"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
You're right! It was the missing \ in the path that was messing it up. I added it and now it works great! Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top