Discussion:
Script for news items
(too old to reply)
Jonathan Duane
2010-03-16 21:54:20 UTC
Permalink
Hi,

I am new enough to VB and so far i have been lucky enough to get code
from some really helpful guys at Experts Exchange the script i have so
far is

Set objFSO = CreateObject("Scripting.FileSystemObject")
SOURCE = "C:\jtest"
TARGET1 = "c:\jtest1"
TARGET2 = "c:\jtest2"
TARGET3 = "c:\jtest3"

Main

Sub Main
'Verify if source folder exists
If Not (objFSO.FolderExists(SOURCE)) Then
MsgBox "Source Folder Missing"
Else
'Run the moveFiles function
moveFiles Source, Target1
moveFiles Source, Target2
moveFiles Source, Target3
'Delete the TARGET folder (and contents)
objFSO.DeleteFolder SOURCE,Force
'Recreate TARGET folder
objFSO.CreateFolder SOURCE
'Wait time before checking SOURCE again
WScript.Sleep 5000
Main
End If
End Sub

Function moveFiles(copysource, ctarget)
'Create target directory if does not exist
If Not (objFSO.FolderExists(ctarget)) Then
objFSO.CreateFolder(ctarget)
End If
'Copy files from SOURCE to TARGET
objFSO.CopyFolder copysource, ctarget
End Function 'replicateFolders


But as the guys have pointed out when files come into the souce folder
and are bigger than 1 oe 2 mbs the script will grab them and them in
when tey havent fully transferred, is there anyway of putting a line
into the code where it says wait til the file hasnt been accessed for
x amount of seconds???
Pegasus [MVP]
2010-03-16 23:35:41 UTC
Permalink
Post by Jonathan Duane
Hi,
I am new enough to VB and so far i have been lucky enough to get code
from some really helpful guys at Experts Exchange the script i have so
far is
Set objFSO = CreateObject("Scripting.FileSystemObject")
SOURCE = "C:\jtest"
TARGET1 = "c:\jtest1"
TARGET2 = "c:\jtest2"
TARGET3 = "c:\jtest3"
Main
Sub Main
'Verify if source folder exists
If Not (objFSO.FolderExists(SOURCE)) Then
MsgBox "Source Folder Missing"
Else
'Run the moveFiles function
moveFiles Source, Target1
moveFiles Source, Target2
moveFiles Source, Target3
'Delete the TARGET folder (and contents)
objFSO.DeleteFolder SOURCE,Force
'Recreate TARGET folder
objFSO.CreateFolder SOURCE
'Wait time before checking SOURCE again
WScript.Sleep 5000
Main
End If
End Sub
Function moveFiles(copysource, ctarget)
'Create target directory if does not exist
If Not (objFSO.FolderExists(ctarget)) Then
objFSO.CreateFolder(ctarget)
End If
'Copy files from SOURCE to TARGET
objFSO.CopyFolder copysource, ctarget
End Function 'replicateFolders
But as the guys have pointed out when files come into the souce folder
and are bigger than 1 oe 2 mbs the script will grab them and them in
when tey havent fully transferred, is there anyway of putting a line
into the code where it says wait til the file hasnt been accessed for
x amount of seconds???
If this is the code that the ExpertsExchange guys propose then the
subscription you paid them is a waste of money. Here is why:

- The line
objFSO.DeleteFolder SOURCE,Force
must read
objFSO.DeleteFolder SOURCE, true
or perhaps
objFSO.DeleteFolder SOURCE, false

- You have the lines
objFSO.DeleteFolder SOURCE,Force
objFSO.CreateFolder SOURCE
and elsewhere
If Not (objFSO.FolderExists(SOURCE)) Then MsgBox "Source Folder Missing"
Now would it not make a whole lot more sense to *always* create
the source folder in case it is missing, not just after deleting it
yourself?

- The Function moveFiles(copysource, ctarget)
is a perfect example of how to confuse people and make maintenance
difficult. Why? Because, despite of its name, it does not *move* files -
it *copies*them!

- To address your main point, you need to tell us more about how the
source files get generated, e.g.
* how many are there?
* do they always have the same names?
* how often do they arrive?
* is it possible to arrange for some handshake so that new files
do not arrive while the old files get copied and old files do not
get copied while new files arrive?

Below is your cleaned-up code, minus the functionality raised in
the last point above. To make it really robust you would need to
introduce some error checking code.

Set objFSO = CreateObject("Scripting.FileSystemObject")
SOURCE = "C:\jtest"
TARGET1 = "c:\jtest1"
TARGET2 = "c:\jtest2"
TARGET3 = "c:\jtest3"

Do
'Verify if source folder exists
If Not objFSO.FolderExists(SOURCE) Then objFSO.CreateFolder SOURCE
'Run the moveFiles function
CopyFiles SOURCE, TARGET1
CopyFiles SOURCE, TARGET2
CopyFiles SOURCE, TARGET3

'Delete the TARGET folder (and contents)
objFSO.DeleteFolder SOURCE, True
'Recreate TARGET folder
objFSO.CreateFolder SOURCE

'Wait time before checking SOURCE again
WScript.Sleep 5000
Loop

Function CopyFiles(copysource, ctarget)
'Create target directory if does not exist
If Not (objFSO.FolderExists(ctarget)) Then objFSO.CreateFolder(ctarget)
'Copy files from SOURCE to TARGET
objFSO.CopyFolder copysource, ctarget
End Function 'replicateFolders
Jonathan Duane
2010-03-17 13:42:26 UTC
Permalink
Post by Pegasus [MVP]
Post by Jonathan Duane
Hi,
I am new enough to VB and so far i have been lucky enough to get code
from some really helpful guys at Experts Exchange the script i have so
far is
Set objFSO = CreateObject("Scripting.FileSystemObject")
SOURCE = "C:\jtest"
TARGET1 = "c:\jtest1"
TARGET2 = "c:\jtest2"
TARGET3 = "c:\jtest3"
Main
Sub Main
       'Verify if source folder exists
       If Not (objFSO.FolderExists(SOURCE)) Then
               MsgBox "Source Folder Missing"
       Else
               'Run the moveFiles function
               moveFiles Source, Target1
               moveFiles Source, Target2
               moveFiles Source, Target3
               'Delete the TARGET folder (and contents)
               objFSO.DeleteFolder SOURCE,Force
               'Recreate TARGET folder
               objFSO.CreateFolder SOURCE
               'Wait time before checking SOURCE again
               WScript.Sleep 5000
               Main
       End If
End Sub
Function moveFiles(copysource, ctarget)
       'Create target directory if does not exist
       If Not (objFSO.FolderExists(ctarget)) Then
               objFSO.CreateFolder(ctarget)
       End If
       'Copy files from SOURCE to TARGET
       objFSO.CopyFolder copysource, ctarget
End Function 'replicateFolders
But as the guys have pointed out when files come into the souce folder
and are bigger than 1 oe 2 mbs the script will grab them and them in
when tey havent fully transferred, is there anyway of putting a line
into the code where it says wait til the file hasnt been accessed for
x amount of seconds???
If this is the code that the ExpertsExchange guys propose then the
- The line
  objFSO.DeleteFolder SOURCE,Force
  must read
  objFSO.DeleteFolder SOURCE, true
  or perhaps
  objFSO.DeleteFolder SOURCE, false
- You have the lines
  objFSO.DeleteFolder SOURCE,Force
  objFSO.CreateFolder SOURCE
  and elsewhere
  If Not (objFSO.FolderExists(SOURCE)) Then MsgBox "Source Folder Missing"
  Now would it not make a whole lot more sense to *always* create
  the source folder in case it is missing, not just after deleting it
yourself?
- The Function moveFiles(copysource, ctarget)
   is a perfect example of how to confuse people and make maintenance
   difficult. Why? Because, despite of its name, it does not *move* files -
   it *copies*them!
- To address your main point, you need to tell us more about how the
   source files get generated, e.g.
  * how many are there?
  * do they always have the same names?
  * how often do they arrive?
  * is it possible to arrange for some handshake so that new files
    do not arrive while the old files get copied and old files do not
    get copied while new files arrive?
Below is your cleaned-up code, minus the functionality raised in
the last point above. To make it really robust you would need to
introduce some error checking code.
Set objFSO = CreateObject("Scripting.FileSystemObject")
SOURCE = "C:\jtest"
TARGET1 = "c:\jtest1"
TARGET2 = "c:\jtest2"
TARGET3 = "c:\jtest3"
Do
  'Verify if source folder exists
  If Not objFSO.FolderExists(SOURCE) Then objFSO.CreateFolder SOURCE
  'Run the moveFiles function
  CopyFiles SOURCE, TARGET1
  CopyFiles SOURCE, TARGET2
  CopyFiles SOURCE, TARGET3
  'Delete the TARGET folder (and contents)
  objFSO.DeleteFolder SOURCE, True
  'Recreate TARGET folder
  objFSO.CreateFolder SOURCE
  'Wait time before checking SOURCE again
  WScript.Sleep 5000
Loop
Function CopyFiles(copysource, ctarget)
  'Create target directory if does not exist
  If Not (objFSO.FolderExists(ctarget)) Then objFSO.CreateFolder(ctarget)
  'Copy files from SOURCE to TARGET
  objFSO.CopyFolder copysource, ctarget
End Function 'replicateFolders
Hi,

I really appreciate the prompt reply..Ok this is what happens, i work
for a radio station and we have stations across the country, we have a
news server were people record their audio clips and create txt
filesto read, when they are created on the news server, the news
serverthen (through a process called internal newswires copys them to
a unc path e.g \\servername\share they come in at random times and
random sizes (depends how big the audio clip is really) but the text
files will always be around the same size it is when they are copied
to the \\servername\share from the news server i want the scipt to run
and then copy the audio clips and text files into different locations
around the country that are connected through UNC paths via VPN

Again thank you so much for your help
Pegasus [MVP]
2010-03-17 19:29:33 UTC
Permalink
Post by Jonathan Duane
Hi,
I really appreciate the prompt reply..Ok this is what happens, i work
for a radio station and we have stations across the country, we have a
news server were people record their audio clips and create txt
filesto read, when they are created on the news server, the news
serverthen (through a process called internal newswires copys them to
a unc path e.g \\servername\share they come in at random times and
random sizes (depends how big the audio clip is really) but the text
files will always be around the same size it is when they are copied
to the \\servername\share from the news server i want the scipt to run
and then copy the audio clips and text files into different locations
around the country that are connected through UNC paths via VPN
Again thank you so much for your help
I can think of two methods to avoid the occasional conflict:
a) You can arrange for some dialog between the NewsWires process
and your own copy process.
b) You rely on a minimum time gap between two successive
transmissions from the NewsWires process (e.g. 5 minutes)
Jonathan Duane
2010-03-18 10:38:52 UTC
Permalink
Post by Pegasus [MVP]
Post by Jonathan Duane
Hi,
I really appreciate the prompt reply..Ok this is what happens, i work
for a radio station and we have stations across the country, we have a
news server were people record their audio clips and create txt
filesto read, when they are created on the news server, the news
serverthen (through a process called internal newswires copys them to
a unc path e.g \\servername\share they come in at random times and
random sizes (depends how big the audio clip is really) but the text
files will always be around the same size it is when they are copied
to the \\servername\share from the news server i want the scipt to run
and then copy the audio clips and text files into different locations
around the country that are connected through UNC paths via VPN
Again thank you so much for your help
a) You can arrange for some dialog between the NewsWires process
    and your own copy process.
b) You rely on a minimum time gap between two successive
    transmissions from the NewsWires process (e.g. 5 minutes)- Hide quoted text -
- Show quoted text -
Thanks a mill is there anyway i could run this VBS as a service or
take an action if it fails i.e run it again if it is does fail??

I am nearly there :)

Loading...