Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 16th, 2007, 11:19 AM
sam76210 sam76210 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 5 sam76210 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 8 sec
Reputation Power: 0
VBScript - How to get files from Sub Folders

Hello Guy's,
Please help me with this code. I am new in VBScript. Here is my Code;

dim BackupFilesSigDate, HTRoFSO, HTRFile, HTRFolder, HTRsrcfolder
BackupFilesSigDate = Now()-0.1
wscript.echo BackupFilesSigDate

'HTRsrcfolder= "C:\HTRDATA\DM4FS1" & "C:\HTRDATA\DM4FS2" '& C:\HTRDATA\DM4FS2"THIS DOES NOT WORK

HTRsrcfolder= "C:\HTRDATA\DM4FS1"


set HTRoFSO = CreateObject("Scripting.FileSystemObject")
on error resume next
set HTRFolder = HTRoFSO.GetFolder(HTRsrcfolder)

Wscript.Echo HTRFolder.Path

on error goto 0
on error resume next

for each HTRFile in HTRFolder.files

if HTRFile.DateLastModified>=BackupFilesSigDate then
wscript.echo HTRFile

'HTRoFSO.DeleteFile HTRFile.path, HTRtgtfolder & "\" & HTRFile.name, True 'THIS DOES NOT WORK

'wscript.echo HTRoFSO.Delete 'THIS DOES NOT WORK

'HTRFile.path, HTRtgtfolder & "\" & HTRFile.name, True 'THIS DOES NOT WORK

on error resume next
end if
Next

set HTRFolder = nothing : set HTRoFSO = nothing


Two Issues;

1. With above code, i can not able to delete files (i am sorting newest file for last 4 hrs...Basically trying to get newest few hrs old files). I got the files but couldn't able to delete them.

2. i have many sub-folders. What will be the easy way to find 4 hrs (lastmodifiedDate) files from all the sub-folder and delete them.

I do appreciate in advance for all expertiese.

Sam

Reply With Quote
  #2  
Old August 17th, 2007, 01:36 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,048 Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Days 12 h 32 m 33 sec
Reputation Power: 555
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo
MySpace
You were basically on the right track, but you had a lot of syntax errors. There's also no point toggling error-handling on and off that many times.

I've streamlined your code a bit and made it more efficient by using subroutines. I've also added subfolders to it and made it so that you can add more folders to the list. Just add them to the array.

vb Code:
Original - vb Code
  1. BackupFilesSigDate = Now() - 0.167 'Time 4 hours ago
  2.  
  3. Set arrFolders = Array("C:\HTRDATA\DM4FS1", "C:\HTRDATA\DM4FS2")
  4. Set objFso = CreateObject("Scripting.FileSystemObject")
  5.  
  6. For i = 0 To arrFolders.Count - 1
  7.     Set objFolder = objFso.GetFolder(arrFolder(i))
  8.     killFiles(objFolder) 'Delete files
  9.     doSubfolders(objFolder) 'Delete files in subfolders
  10.     Set objFolder = Nothing
  11. Next
  12.  
  13. Sub killFiles (ByRef objFolder) 'Delete all files in folder
  14.     Set colFiles = objFolder.Files
  15.    
  16.     On Error Resume Next
  17.     For Each objFile In colFiles
  18.         If objFile.DateLastModified <= BackupFilesSigDate Then 'Delete files older than 4 hours ago
  19.             objFile.Delete
  20.         End If
  21.     Next
  22.     On Error Goto 0
  23. End Sub
  24.  
  25. Sub getSubFolders(strFolder) 'If subfolders exist, process files in each folder
  26.     Set colSubfolders = objFolder.SubFolders
  27.     For Each objSubfolder In colSubfolders
  28.         killFiles(objSubfolder)
  29.     Next
  30. End Sub
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Last edited by Nilpo : August 24th, 2007 at 07:55 PM.

Reply With Quote
  #3  
Old August 17th, 2007, 12:49 PM
sam76210 sam76210 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 5 sam76210 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 8 sec
Reputation Power: 0
How to get files from Sub Folders

Quote:
Originally Posted by Nilpo
You were basically on the right track, but you had a lot of syntax errors. There's also no point toggling error-handling on and off that many times.

I've streamlined your code a bit and made it more efficient by using subroutines. I've also added subfolders to it and made it so that you can add more folders to the list. Just add them to the array.

vb Code:
Original - vb Code
  1. BackupFilesSigDate = Now() - 0.167 'Time 4 hours ago
  2.  
  3. arrFolders = Array("C:\HTRDATA\DM4FS1", "C:\HTRDATA\DM4FS2")
  4. Set objFso = CreateObject("Scripting.FileSystemObject")
  5.  
  6. For i = 0 To arrFolders.Count - 1
  7.     objFolder = objFso.GetFolder(arrFolder(i))
  8.     killFiles(objFolder) 'Delete files
  9.     doSubfolders(objFolder) 'Delete files in subfolders
  10.     Set objFolder = Nothing
  11. Next
  12.  
  13. Sub killFiles (ByRef objFolder) 'Delete all files in folder
  14.     Set colFiles = objFolder.Files
  15.    
  16.     On Error Resume Next
  17.     For Each objFile In colFiles
  18.         If objFile.DateLastModified <= BackupFilesSigDate Then 'Delete files older than 4 hours ago
  19.             objFile.Delete
  20.         End If
  21.     Next
  22.     On Error Goto 0
  23. End Sub
  24.  
  25. Sub getSubFolders(strFolder) 'If subfolders exist, process files in each folder
  26.     Set colSubfolders = objFolder.SubFolders
  27.     For Each objSubfolder In colSubfolders
  28.         killFiles(objSubfolder)
  29.     Next
  30. End Sub



Nilpo,
As always, I really appreciated your helpful reply. I do get error for "For i = 0 To arrFolders.Count - 1" 'object Required'. I can take hint to resolve this my own. I am enjoying VBScript. Also - Would you recomend any books etc for a beginer like me.

Thanks Again
Sam

Reply With Quote
  #4  
Old August 24th, 2007, 07:52 PM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,048 Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)Nilpo User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Days 12 h 32 m 33 sec
Reputation Power: 555
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo
MySpace
To resolve the Object Required error, be sure that your array is not empty and is properly initialized.

Reply With Quote
Reply

Viewing: ASP Free ForumsSystem AdministrationWindows Scripting > VBScript - How to get files from Sub Folders


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT