 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
ScriptStudent
Joined: 16 Jun 2005 Posts: 4
|
|
Recurse |
|
Hey guys I have a VBScript that searches and copies all files by ext and by Date specified. You can edit it to look for a specific range of the DateLastModified. It works perfectly except for one thing it cant recurse. I want the script to recurse through the subdirectories of the entire C:\ drive and copy the results into a folder on the same drive. I tried the wizard but it seems all the templates are designed to compare a file first. Can I use JAL statements to accomplish this? Thanks in advance.
|
|
Thu Jun 16, 2005 10:51 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7975
|
|
Re: Recurse |
|
Sure. Actually both VBScript and JAL can be used for that purpose. You should code a user-defined function and have it call itself recursively for each directory. Pretty simple. Here is a classical example of a recursive function in VBScript Function factorial(N) if N <=1 Then Factorial = 1 Else Factorial = N * Factorial(N-1) End If End Function Sub Main MsgBox Factorial(5) End Sub : Hey guys I have a VBScript that searches and copies all files : by ext and by Date specified. You can edit it to look for a specific : range of the DateLastModified. It works perfectly except for one thing : it cant recurse. I want the script to recurse through the subdirectories : of the entire C:\ drive and copy the results into a folder on the same drive. : I tried the wizard but it seems all the templates are designed to compare a : file : first. Can I use JAL statements to accomplish this? Thanks in advance.
|
|
Thu Jun 16, 2005 11:07 pm |
|
 |
ScriptStudent
Joined: 16 Jun 2005 Posts: 4
|
|
Re: Recurse |
|
Hi guys Im looking at your example and Im not sure I understand. When I use the wizard I notice in respone to the anwered paramter questions there is only a series of yes and no strings in the code. One of those strings is a respone to the question (recurse subfolder?). If I paste my code in after the wizard is finish is there any way I can call on that same parameter to recurse???? Here is my VBScript: Option Explicit Dim BackupFilesSigDate, filesys, oFolder, srcfolder, tgtfolder, oFile On Error Resume Next BackupFilesSigDate = Date - 60 srcfolder = "C:\" tgtfolder = "C:\24HourBackUp" Set filesys = CreateObject("Scripting.FileSystemObject") Set oFolder = filesys.GetFolder(srcfolder) If Err.number 0 Then ' Object couldn't be created ' Log error Log.Write Err.Description, apgSeverityError, Err.Number On Error Goto 0 Wscript.echo "The Source Folder does not exists. Operation aborted." Set oFolder = Nothing Set filesys = Nothing Else For Each oFile In oFolder.Files If DateDiff("d", BackupFilesSigDate, oFile.DateLastModified) > 58 Then filesys.copyfile oFile.Path, tgtfolder & "\" & oFile.Name, True End If Next End If Set oFile = Nothing Set oFolder = Nothing Set filesys = Nothing
|
|
Thu Jun 16, 2005 11:25 pm |
|
 |
ScriptStudent
Joined: 16 Jun 2005 Posts: 4
|
|
Re: Recurse |
|
Hey Guys I re-wrote the entire function in JAL. I validated it and found no errors but when I run it nothing happens, here is the script: // declare intermidiate variables Dim file_name, string Dim found, boolean Dim attr, number Dim directory, boolean Dim source_name, string Dim dest_name, string Dim dest_name, string Dim File, string Dim file_date, string Dim file_days, string Dim days, string Dim source_datetime, datetime Dim TenDaysAgo, datetime Dim dest_datetime, datetime Dim no_change, boolean Dim seconds, number DateAdd( "date", -10, TenDaysAgo ) FileFindFirst( "*.txt", File, found ) LoopWhile( found, END_LOOP ) FileGetAttr( "source_name", attr ) BitwiseGetBit( attr, 5, directory ) IfThen( directory, Find_Next ) FileExists( File, found ) //Determine if file date is equal to or less than specified date If( found, COMPARE_FILETIME, Find_Next ) COMPARE_FILETIME: // Get source file date/time FileDate( "found", file_date ) //compare the file date being read to ten days ago DateDiff( " TenDaysAgo", "file_date", days ) //if the date of the file is more than ten days old do nothing and try the next file IsGreater( file_date, TenDaysAgo, no_change ) IfThen( no_change, FIND_NEXT ) //otherwise copy file COPY_FILE: FileCopy( "File", "C:\TimeT\File" ) Find_Next: // Find next file FileFindNext( file, found ) END_LOOP:
|
|
Fri Jun 17, 2005 4:05 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7975
|
|
Re: Recurse |
|
I guess you are confusing different things. Which wizard/template are you talking about? Any way if you warp your code into a function with srcfolder parameter as in my example you can call it recursivelly. The Folders collection can be used to recursive folders after you are done with files, example Sub BackupFiles(srsfolder) ... here is you code ... before Set ... = Nothing inser tthe following For Each oSubFolder In oFolder.Folders BackupFiles(oSubFolder) ... End Sub '''''''''''''''''''''' Sub Main Dim filesys, oFolder Set filesys = CreateObject("Scripting.FileSystemObject") Set oFolder = filesys.GetFolder("c:\") Set oFolder = Nothing Set filesys = Nothing end Sub : Hi guys Im looking at your example and Im not sure I understand. : When I use the wizard I notice in respone to the anwered paramter questions : there is only a series of yes and no strings in the code. One of : those strings is a respone to the question (recurse subfolder?). : If I paste my code in after the wizard is finish is there any way I can : call on that same parameter to recurse???? Here is my VBScript: Option : Explicit : Dim BackupFilesSigDate, filesys, oFolder, srcfolder, tgtfolder, oFile : On Error Resume Next : BackupFilesSigDate = Date - 60 : srcfolder = "C:\" : tgtfolder = "C:\24HourBackUp" : Set filesys = CreateObject("Scripting.FileSystemObject") : Set oFolder = filesys.GetFolder(srcfolder) : If Err.number 0 Then : ' Object couldn't be created : ' Log error : Log.Write Err.Description, apgSeverityError, Err.Number : On Error Goto 0 : Wscript.echo "The Source Folder does not exists. Operation : aborted." : Set oFolder = Nothing : Set filesys = Nothing : Else : For Each oFile In oFolder.Files : If DateDiff("d", BackupFilesSigDate, oFile.DateLastModified) > : 58 Then : filesys.copyfile oFile.Path, tgtfolder & "\" & oFile.Name, : True : End If : Next : End If : Set oFile = Nothing : Set oFolder = Nothing : Set filesys = Nothing
|
|
Fri Jun 17, 2005 8:38 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7975
|
|
Re: Recurse |
|
Run the script in the debugger and you will qucikly find where it doesn't work. By the way, what you want to do can be done much easier using FileDir utility http://www.softtreetech.com/24x7/archive/46.htm You can schedule it as a program type job and use standard DOS xcopy.exe with parameters as the command to copy found files. Note that FileDir supports subdirectory recursions. : Hey Guys I re-wrote the entire function in JAL. : I validated it and found no errors but when I run it : nothing happens, here is the script: // declare intermidiate variables : Dim file_name, string : Dim found, boolean : Dim attr, number : Dim directory, boolean : Dim source_name, string : Dim dest_name, string : Dim dest_name, string : Dim File, string : Dim file_date, string : Dim file_days, string : Dim days, string : Dim source_datetime, datetime : Dim TenDaysAgo, datetime : Dim dest_datetime, datetime : Dim no_change, boolean : Dim seconds, number : DateAdd( "date", -10, TenDaysAgo ) : FileFindFirst( "*.txt", File, found ) : LoopWhile( found, END_LOOP ) : FileGetAttr( "source_name", attr ) : BitwiseGetBit( attr, 5, directory ) : IfThen( directory, Find_Next ) : FileExists( File, found ) : //Determine if file date is equal to or less than specified date : If( found, COMPARE_FILETIME, Find_Next ) : COMPARE_FILETIME: // Get source file date/time : FileDate( "found", file_date ) : //compare the file date being read to ten days ago : DateDiff( " TenDaysAgo", "file_date", days ) : //if the date of the file is more than ten days old do nothing and try the : next file : IsGreater( file_date, TenDaysAgo, no_change ) : IfThen( no_change, FIND_NEXT ) : //otherwise copy file : COPY_FILE: FileCopy( "File", "C:\TimeT\File" ) : Find_Next: // Find next file : FileFindNext( file, found ) : END_LOOP:
|
|
Fri Jun 17, 2005 12:05 pm |
|
 |
ScriptStudent
Joined: 16 Jun 2005 Posts: 4
|
|
Re: Recurse |
|
Hi,
Can the FileDir utility be licensed as part of a commercial software package
|
|
Sun Jun 19, 2005 8:08 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7975
|
|
Re: Recurse |
|
Please contact Sales for assistance : Hi, : Can the FileDir utility be licensed as part of a commercial software package
|
|
Sun Jun 19, 2005 9:52 pm |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|