Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Copy Files Using VbScript

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,43169
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Copy Files Using VbScript
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Copy Files Using VbScript - 2/14/2007 2:52:30 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Hey All,

Look like am not getting right logic to strart the below scenario


Okie here we go...

 I have Folder A and Floder B,assume Folder A getting new files every one hour or may not get anything new.

i suppose to write a script to copy Folder A files to Folder B

Condtion 1:

The script run every one hour to copy files in Folder A to Folder B.

Condition2:

Every time script run it find the new file and copy to folder B.

For example.

Assume the script is running at 10 Am

Folder A got three files at the time  File1,file 2,File3.

so the script just copy all three files to Folder B.

on the next run at 11 Am

Folder A got anothe two new files File 4 and file 5.

Now Folder A have 5 files ( File1,file 2,File3,files File 4 and file 5.)

on this run (at 11Am) the script should copy only File 4 and File 5 to Folder B.


I know this is possible to get the time stamp of the files and copy to the folder but still some thing i feel not 100% right.

I saw lot of examples in this forum and one particular almost nearer to my scenario answered by EBGREEN

http://www.visualbasicscript.com/m_41914/mpage_3/key_copy/tm.htm#42244

Appreciate all your help and Thanks in Advance

Ashok
 
 
Post #: 1
 
 RE: Copy Files Using VbScript - 2/14/2007 3:17:36 AM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
This should do what you are looking for.  it looks to see if the folder has already been copied, if it doesn't already exist, it copies it.


      


_____________________________

"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

(in reply to ashok_ganeshs)
 
 
Post #: 2
 
 RE: Copy Files Using VbScript - 2/14/2007 3:25:08 AM   
  ebgreen


Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
I would suggest getting Robocopy and setting it up as a scheduled job.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 3
 
 RE: Copy Files Using VbScript - 2/14/2007 3:29:36 AM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
Cheater!  That's a 3rd party app!  yer only allowed to use vbscript!

_____________________________

"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: Copy Files Using VbScript - 2/14/2007 4:03:11 AM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
If you really MUST check the time/date stamp ont he files, you can use this script:

      
It will only copy files that are newer than the last copy time.
It *should* work, but is only aircode at this time, haven't tested it yet.


_____________________________

"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 5
 
 RE: Copy Files Using VbScript - 2/14/2007 4:36:47 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Bunch of thanks for all your replies.

First take this Code written by Digital
*****************************************************************************************
Dim sOriginFolder, sDestinationFolder, sFile, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOriginFolder = "c:\tp"
sDestinationFolder = "c:\tp2"
For Each sFile In oFSO.GetFolder(sOriginFolder).Files
If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
 oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
 WScript.Echo "Copying : " & Chr(34) & oFSO.GetFileName(sFile) & Chr(34) & " to " & sDestinationFolder
End If
Next
*****************************************************************************************
yes this works and logic is also simple check the file in folder B if its not there then copy to that folder.

But Assume i have 1500 files in floder A and each day its growing the file count...personaly i don't feel its a wise idea to go with that code

Thats why i thought go with Timestamp logic...just wanna check any new file come in last one hour

is that make sense ?

Thanks
Ashok

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 6
 
 RE: Copy Files Using VbScript - 2/14/2007 4:43:14 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Here is the link
"Automatically Run a Script Any Time a File is Added to a Folder"
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1011.mspx

(in reply to ashok_ganeshs)
 
 
Post #: 7
 
 RE: Copy Files Using VbScript - 2/14/2007 5:04:15 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Digital,

Timestamp code works for me...i just tested on my end it works fine.

Thanks again
Ashok

(in reply to ashok_ganeshs)
 
 
Post #: 8
 
 RE: Copy Files Using VbScript - 2/14/2007 5:25:05 AM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
Just a note... either way you are going to be checking each file in the folder.

If we are throwing around 3rd party app suggestions, I would suggest ViceVersa Pro.  It will run, and then monitor the folder.  If there is a file added, it will then copy that file to the other folder immediately.

_____________________________

"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

(in reply to ashok_ganeshs)
 
 
Post #: 9
 
 RE: Copy Files Using VbScript - 2/14/2007 5:36:00 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Thanks for your suggestion.

Checked Robocopy and ViceVersa Pro also,both looks good (GUI stuff) but decide to go with script for easy usage purpose.

Thanks Again

Ashok

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 10
 
 RE: Copy Files Using VbScript - 2/14/2007 6:50:54 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Digital,

One honest question is this any way in can store the time stamp on seperate text file rather than using regedit ?

oShell.RegWrite "HKLM\SYSTEM\LastTimeCopied",Now,"REG_SZ"

Thanks
Ashok


(in reply to ashok_ganeshs)
 
 
Post #: 11
 
 RE: Copy Files Using VbScript - 2/14/2007 8:36:15 AM   
  Parabellum


Posts: 222
Score: 0
Joined: 11/12/2006
From: UK
Status: offline
Just a though... but you could approach this from a different angle..

Instead of looking at time stamps ect, you could use file attributes... (native since DOS 2.0!!)
When you create a file it is automaticaly created with the archive attribute selected. it is also re-added when ever a file is ammended or changed.
you could use XCOPY to copy all files with the archive attribute set, and then remove the attribute (untill it is next changed) as part of the XCOPY process..

or if you wish to use vbscript... you can read and write to this attribute like so..


      

to write back to the atribute  wyou would use...

  file.attributes = file.attributes + 32    '<------ to set archive on

  file.attributes = file.attributes - 32    '<------ to set archive off


not sure if it helps... but thats the way i'd probably try and approach it.

< Message edited by Parabellum -- 2/14/2007 8:39:34 AM >

(in reply to ashok_ganeshs)
 
 
Post #: 12
 
 RE: Copy Files Using VbScript - 2/15/2007 1:03:02 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Thanks for your suggestion...looks good to me but i have quiestion

Is this code triger it automatically whenever the new file is added in the folder ? could you please explain little bit more about your code.

Here is my another Approach...writing wmiscript to triger vbscript to copy.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceDeletionEvent WITHIN 10 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop


Thanks
Ashok

(in reply to Parabellum)
 
 
Post #: 13
 
 RE: Copy Files Using VbScript - 2/15/2007 4:42:07 AM   
  Parabellum


Posts: 222
Score: 0
Joined: 11/12/2006
From: UK
Status: offline
 
take a look at the follwing code i found here:
http://www.scriptinganswers.com/vault/Files%20and%20Folders/



      


You could adapt this code... so that instead of running
quote:

 
  objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES


you could run


      

The /m switch on the xcopy command tells it to only copy files with the archive attribute set, and to untick it once its copied.

< Message edited by Parabellum -- 2/15/2007 4:44:08 AM >

(in reply to ashok_ganeshs)
 
 
Post #: 14
 
 RE: Copy Files Using VbScript - 2/15/2007 5:04:47 AM   
  Parabellum


Posts: 222
Score: 0
Joined: 11/12/2006
From: UK
Status: offline
infact forget the xcopy...

I'm feeling generous...
here's a complete working version (tested), Based on the code i pasted above.
you could set this script to run as a sheduled task every hour for example..

Dont forget to change the variables (i high-lighted them in red)


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Option Explicit
Dim objFSO, strSourceFolder, strDestFolder, strExclusion, strPrompt
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSourceFolder = "c:\MyImportantData"
strDestFolder = "c:\MyBackupFolder"
strExclusion = ""
' Example:    zip,vbs,exe,js,rar
If IsNoData(strExclusion) Then strExclusion = "" 'Make sure it is blank.
'Call function that will copy folder structure as well as files in it.
CopyFolderStructure strSourceFolder, strDestFolder, strExclusion
Function CopyFolderStructure(strSource, strDestination, strExcludedExt)
Const OVER_WRITE_FILES = True
Dim objDir, objFolder, objFiles, strCurExt, intX, arrExt, blnExclude

'Connect to the current directory in strSource.
Set objDir = objFSO.GetFolder(strSource)

'If destination folder doesn't exist, create it.
If Not objFSO.FolderExists(strDestination) Then
objFSO.CreateFolder(strDestination)
End If

'If current folder doesn't exist under destination folder, create it.
If Not objFSO.FolderExists(strDestination & "\" & objDir.Name) Then
objFSO.CreateFolder(strDestination & "\" & objDir.Name)
End If
'Validate if there is something in strExcludedExt.
If Not IsNoData(strExcludedExt) Then
'If yes, transfer content from strExcludedExt in an array.
arrExt = Split(strExcludedExt, ",")
blnExclude = False  'Intialize to False blnExclude
End If
'Loop through all files in current folder if any and copy all files in destination folder
'except for excluded extension if any in strExcludedExt.
For Each objFiles In objFSO.GetFolder(strSource).Files
If objFiles.attributes and 32 Then ' Check if the Archive Atrtibut Is set
 'If there is exclusion, will compare if current extension file is excluded or not.
 If Not IsNoData(strExcludedExt) Then
  strCurExt = objFSO.GetExtensionName(objFiles.Name) 'Take current extension.
  'Loop through the array to find if current extension has to be copied or not.
  For intX = 0 To UBound(arrExt)
   If LCase(strCurExt) = arrExt(intX) Then
    blnExclude = True 'If found, set to True blnExclude and exit for.
    Exit For
   Else
    blnExclude = False
   End If
  Next
  If Not blnExclude Then 'If blnExclude is True, current file will not be copied.
   objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES
   objFiles.attributes = objFiles.attributes - 32
  End If
 Else 'If no exclusion, copy everything in all folders/subfolders
  objFSO.CopyFile strSource & "\" & objFiles.Name, strDestination & "\" & objDir.Name & "\" & objFiles.Name, OVER_WRITE_FILES
  objFiles.attributes = objFiles.attributes - 32
 End If
End If
Next
' If there is subfolder(s) under current folder in strPath, Call
' recursively this sub until there is no other subfolder(s)
For Each objFolder In objFSO.GetFolder(strSource).SubFolders
CopyFolderStructure objFolder.Path, strDestination & "\" & objDir.Name, strExcludedExt
Next
End Function
Function IsNoData(varVal2Check)
'Verify if varVal2Check contain something.
On Error Resume Next
  If IsNull(varVal2Check) Or IsEmpty(varVal2Check) Then
IsNoData = True
  Else
      If IsDate(varVal2Check) Then
 IsNoData = False
      Elseif varVal2Check = "" Then
 IsNoData = True
ElseIf Not IsObject(varVal2Check) Then
 IsNoData = False
Else
          IsNoData = False
      End If
  End If
End Function

< Message edited by Parabellum -- 2/15/2007 5:06:43 AM >

(in reply to Parabellum)
 
 
Post #: 15
 
 RE: Copy Files Using VbScript - 2/20/2007 1:59:40 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Hey Guys,

After so many thought i like to go with WMI...here is the code

'==========================================================================
' NAME: WMI_Copy_Prod2Blade.vbs
' AUTHOR:Your Name
' DATE  :
' COMMENT: This Code copy files from Production folder to Blade folder,whenever new file is added in to Production Foler
''==========================================================================
strComputer = "." 'Local Computer running on this script
set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\scripts""'") 'Monitor the files in Script Folder in C dirve
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
  '  WScript.Echo objLatestEvent.TargetInstance.PartComponent   
  'Copying the files to Destination Folder
     strFile = objLatestEvent.TargetInstance.PartComponent
     strFile = Replace(Mid(strFile, InStr(strFile, Chr(34)) + 1), "\\", "\")
     strFile = Left(strFile, Len(strFile) - 1)
     objFSO.CopyFile strFile, "C:\FolderB\"
    
Loop

i did test it works fine.

Thanks

(in reply to Parabellum)
 
 
Post #: 16
 
 RE: Copy Files Using VbScript - 2/22/2007 3:37:52 PM   
  maksim

 

Posts: 7
Score: 0
Joined: 2/20/2007
Status: offline
The simplicity of (code below) is brilliant, i have removed the dialogue box to appear after every file, only at the end. Is it possible to only include sertain file extensions. All the code has been excluding file extensions. For work got a folder that has lots of files but only two extensions need copying. In addition, is it possible to cut instead of copy.



Dim sOriginFolder, sDestinationFolder, sFile, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOriginFolder = "c:\test"
sDestinationFolder = "c:\test2"
For Each sFile In oFSO.GetFolder(sOriginFolder).Files
If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
End if
next
WScript.Echo "Copying :  All the files have been copied to " & sDestinationFolder

< Message edited by maksim -- 2/22/2007 3:58:13 PM >

(in reply to ashok_ganeshs)
 
 
Post #: 17
 
 RE: Copy Files Using VbScript - 2/23/2007 2:09:34 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
for particular file copy use "TYPE"
eg:
Object.type= txt (Text document) 'copy only text document

Instead of copy to cut use "Move"

eg:

object.Move destination

Thanks
Ashok

(in reply to maksim)
 
 
Post #: 18
 
 RE: Copy Files Using VbScript - 2/23/2007 2:24:32 AM   
  ashok_ganeshs

 

Posts: 92
Score: 0
Joined: 12/5/2006
Status: offline
Dim objFSO
strSourceFolder = "C:\folderA\*.txt"
strDestFolder = "c:\FolderB\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile  strSourceFolder , strDestFolder 

(in reply to ashok_ganeshs)
 
 
Post #: 19
 
 RE: Copy Files Using VbScript - 2/23/2007 2:43:41 AM   
  DiGiTAL.SkReAM


Posts: 1097
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

ORIGINAL: maksim

The simplicity of (code below) is brilliant, i have removed the dialogue box to appear after every file, only at the end. Is it possible to only include sertain file extensions. All the code has been excluding file extensions. For work got a folder that has lots of files but only two extensions need copying. In addition, is it possible to cut instead of copy.

Dim sOriginFolder, sDestinationFolder, sFile, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sOriginFolder = "c:\test"
sDestinationFolder = "c:\test2"
For Each sFile In oFSO.GetFolder(sOriginFolder).Files
If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
End if
next
WScript.Echo "Copying :  All the files have been copied to " & sDestinationFolder


Thanks for the kind words. 

To only check for certain extensions, use this code.  It also has a boolean to select whether to move or copy the file:

      


_____________________________

"There's the one man who learns by reading, the two men that learn by watching, and the rest of us have to pee on the electric fence for ourselves." - Roy Rogers

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

(in reply to maksim)
 
 
Post #: 20
 
 
Page:   [1] 2   next >