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 1st, 2007, 05:07 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
VBScript - Copy files with certain date creation

Hello,
can anyone help me to create script file for copying files for last n days (last 30 days).
thanks
sam

Reply With Quote
  #2  
Old August 2nd, 2007, 07:34 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
Well, you've been pretty vague in what you're looking for. Here's a script that will look in a provided folder and copy all files that haven't been accessed in 30 days. Did you just want to archive them and delete the originals? This script simply creates copies. That seems a bit pointless to me.

vb Code:
Original - vb Code
  1. numDays = 30
  2. strSource = "C:\Windows\Temp"
  3. strDest = "C:\Windows\Temp2"
  4.  
  5. Set objfso = CreateObject("Scripting.FileSystemObject")
  6. If objfso.FolderExists(strPath) Then
  7.    Set objFolder = objfso.GetFolder(strPath)
  8.    Call CheckFolder(objFolder, numDays, True)
  9. Else
  10.    WScript.Echo "The specified folder", strPath, "does not exist"
  11. End If
  12.  
  13. Sub CheckFolder(objFolder, numDays, bRecurse)
  14.    Set colFiles = objFolder.Files
  15.    If colFiles.Count > 0 Then
  16.        For Each objFile In colFiles
  17.           Call CopyOldFile(objFile, numDays)
  18.        Next
  19.    Else
  20.        WScript.Echo "No files in folder", objFolder.Path
  21.    End If
  22.    If bRecurse Then
  23.        Set colSubfolders = objFolder.SubFolders
  24.        If colSubfolders.Count > 0 Then
  25.           For Each SubFolder In colSubfolders
  26.               CheckFolder SubFolder, numDays, True
  27.           Next
  28.        End If
  29.    End If
  30. End Sub
  31.  
  32. Sub CopyOldFile(objFile, numDays)
  33.    On Error Resume Next
  34.    Err.Clear
  35.    dateFile = objFile.DateLastModified
  36.    dateToday = Now()
  37.    If dateFile <= dateToday Then
  38.        daysOld = dateToday - dateFile
  39.        If daysOld > numDays Then
  40.           strFile = objFile.Path
  41.           objFile.Attributes = 0
  42.           objFile.Copy strDest
  43.           If Err.number <> 0 Then
  44.               WScript.Echo Err.number, Err.Description, Err.Source, strFile
  45.           Else
  46.               WScript.Echo strFile, "copied"
  47.           End If
  48.        End If
  49.    Else
  50.        WScript.Echo "Incorrect date stamp in", strFile
  51.    End If
  52. End Sub
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Reply With Quote
  #3  
Old August 2nd, 2007, 10:23 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
Copy files with certian date creation

Nilpo, sorry about the post with confusion and I do appreciated that you took time to reply my post. I have one folder which collecting data files every day. Everyday 2 or some case 4 files created. What i am looking for to copy last 30 days files. For last 30 days, there could be 60 files or 120 files. Regardless # of files, i would like to copy by date. Please let me know if you have any question. I got one code but it is not working. This is the error, "Backup folder not exist". I do have folder backup there though.

dim BackupFilesSigDate, oFSO, oFolder, srcfolder, tgtfolder, oFile
BackupFilesSigDate = date-1
srcfolder="C:\Images"
tgtfolder="C:\Backup"

set oFSO = CreateObject("Scripting.FileSystemObject")
on error resume next
set oFolder = oFSO.GetFolder(tgtfolder)
if clng(oFile.DateCreated)=BackupfileSigDate-1 then
wscript.echo "The Backup Folder does not exists. Operation aborted."
set oFolder = nothing : set oFSO = nothing
wscript.quit(1)
end if


set oFSO = CreateObject("Scripting.FileSystemObject")
on error resume next
set oFolder = oFSO.GetFolder(srcfolder)
if clng(oFile.DateCreated)=BackupfileSigDate-1 then
wscript.echo "The Source Folder does not exists. Operation aborted."
set oFolder = nothing : set oFSO = nothing
wscript.quit(2)
end if

on error goto 0
for each oFile in oFolder.files
if oFile.DateCreated=BackupfileSigDate then
oFSO.copyfile oFile.path, tgtFolder & "\" & oFile.name, true
end if
next
set oFolder = nothing : set oFSO = nothing

thanks in advance



Quote:
Originally Posted by Nilpo
Well, you've been pretty vague in what you're looking for. Here's a script that will look in a provided folder and copy all files that haven't been accessed in 30 days. Did you just want to archive them and delete the originals? This script simply creates copies. That seems a bit pointless to me.

vb Code:
Original - vb Code
  1. numDays = 30
  2. strSource = "C:\Windows\Temp"
  3. strDest = "C:\Windows\Temp2"
  4.  
  5. Set objfso = CreateObject("Scripting.FileSystemObject")
  6. If objfso.FolderExists(strPath) Then
  7.    Set objFolder = objfso.GetFolder(strPath)
  8.    Call CheckFolder(objFolder, numDays, True)
  9. Else
  10.    WScript.Echo "The specified folder", strPath, "does not exist"
  11. End If
  12.  
  13. Sub CheckFolder(objFolder, numDays, bRecurse)
  14.    Set colFiles = objFolder.Files
  15.    If colFiles.Count > 0 Then
  16.        For Each objFile In colFiles
  17.           Call CopyOldFile(objFile, numDays)
  18.        Next
  19.    Else
  20.        WScript.Echo "No files in folder", objFolder.Path
  21.    End If
  22.    If bRecurse Then
  23.        Set colSubfolders = objFolder.SubFolders
  24.        If colSubfolders.Count > 0 Then
  25.           For Each SubFolder In colSubfolders
  26.               CheckFolder SubFolder, numDays, True
  27.           Next
  28.        End If
  29.    End If
  30. End Sub
  31.  
  32. Sub CopyOldFile(objFile, numDays)
  33.    On Error Resume Next
  34.    Err.Clear
  35.    dateFile = objFile.DateLastModified
  36.    dateToday = Now()
  37.    If dateFile <= dateToday Then
  38.        daysOld = dateToday - dateFile
  39.        If daysOld > numDays Then
  40.           strFile = objFile.Path
  41.           objFile.Attributes = 0
  42.           objFile.Copy strDest
  43.           If Err.number <> 0 Then
  44.               WScript.Echo Err.number, Err.Description, Err.Source, strFile
  45.           Else
  46.               WScript.Echo strFile, "copied"
  47.           End If
  48.        End If
  49.    Else
  50.        WScript.Echo "Incorrect date stamp in", strFile
  51.    End If
  52. End Sub

Reply With Quote
  #4  
Old August 2nd, 2007, 07:37 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
Try removing the clng() functions when you're making your date comparison. VBS will handle types all on it's own. You'll notice in my example, I didn't convert any of them. I did that intentionally.

Reply With Quote
  #5  
Old August 3rd, 2007, 12:13 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
I just noticed this as well...

oFSO.copyfile oFile.path, tgtFolder & "\" & oFile.name, true

Take a look at the FileSystemObject's BuildPath function. Although, that shouldn't stop this from working.

Reply With Quote
Reply

Viewing: ASP Free ForumsSystem AdministrationWindows Scripting > VBScript - Copy files with certain date creation


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 |