Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - vbscript equivalent of copy a+b c

Reply
 
Old 07-29-2009   #1 (permalink)
Bonno Bloksma


 
 

vbscript equivalent of copy a+b c

Hi,

I've searched how to do it but I'm unable to find a sollution.
I want to log something on the computer while executing a VBScript. No
problem.
I want to have that log at the end of the script copied to a central
location. No problem.
I want all logs for today in one file on the central location, problem.

What I'm looking for is the VBS equivalent of the prompt command
COPY A.TXT+B.TXT C.TXT
where in the end the file C contains te content of A and B.

How to I do that in VB script?
My last commands so far are:
Set objFile = FSO.GetFile(strTmpDirectory & strFile)
objFile.Copy (strDestination & strFile)
But this wil just copy the file and overwrite an existing file. There does
not seem to be an
objFile.Append (strDestination & strFile)
So how do I do this?

Bono Bloksma



My System SpecsSystem Spec
Old 07-29-2009   #2 (permalink)
Tom Lavedas


 
 

Re: vbscript equivalent of copy a+b c

On Jul 29, 9:40*am, "Bonno Bloksma" <b.blok...@xxxxxx> wrote:
Quote:

> Hi,
>
> I've searched how to do it but I'm unable to find a sollution.
> I want to log something on the computer while executing a VBScript. No
> problem.
> I want to have that log at the end of the script copied to a central
> location. No problem.
> I want all logs for today in one file on the central location, problem.
>
> What I'm looking for is the VBS equivalent of the prompt command
> COPY A.TXT+B.TXT C.TXT
> where in the end the file C contains te content of A and B.
>
> How to I do that in VB script?
> My last commands so far are:
> * Set objFile = FSO.GetFile(strTmpDirectory & strFile)
> * objFile.Copy (strDestination & strFile)
> But this wil just copy the file and overwrite an existing file. *There does
> not seem to be an
> * objFile.Append (strDestination & strFile)
> So how do I do this?
>
> Bono Bloksma
AFAIK, there is still no append method available to scripting. I
wrote this subroutine many, many years ago to do the job ...

' Appends file2 to end of file1
Sub AppendFile(sFileSpec1, sFileSpec2)
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
With .OpenTextFile(sFileSpec1, 8)
.Write fso.OpentextFile(sFileSpec2, 1).ReadAll
End With
End sub

To use this to add two files together, use CopyFile to copy file A to
C then append B to C. For example, ...

FSO.CopyFile strTmpDirectory & strFileA, strDestFolder & strFileC
appendFile strDestFolder & strFileC, strTmpDirectory & strFileB

This approach works fine for reasonably sized files, say less than
tens of megabytes, though there is almost no memory limitation, since
it is based on virtual (paging) memory, not strictly available RAM.
Very large files are just slower to copy over.

Of course the other way to do it is to just Run the command shell COPY
statement from the script ...

with createobject("wscript.shell")
.run "%comspec% /c copy " & sFileSpecA & "+" & sFileSpecB _
& sFileSpecC, 0, true
end with

Tom Lavedas
***********
My System SpecsSystem Spec
Old 07-29-2009   #3 (permalink)
Tom Lavedas


 
 

Re: vbscript equivalent of copy a+b c

On Jul 29, 10:32*am, Tom Lavedas <tglba...@xxxxxx> wrote:
Quote:

> On Jul 29, 9:40*am, "Bonno Bloksma" <b.blok...@xxxxxx> wrote:
>
>
>
Quote:

> > Hi,
>
Quote:

> > I've searched how to do it but I'm unable to find a sollution.
> > I want to log something on the computer while executing a VBScript. No
> > problem.
> > I want to have that log at the end of the script copied to a central
> > location. No problem.
> > I want all logs for today in one file on the central location, problem.
>
Quote:

> > What I'm looking for is the VBS equivalent of the prompt command
> > COPY A.TXT+B.TXT C.TXT
> > where in the end the file C contains te content of A and B.
>
Quote:

> > How to I do that in VB script?
> > My last commands so far are:
> > * Set objFile = FSO.GetFile(strTmpDirectory & strFile)
> > * objFile.Copy (strDestination & strFile)
> > But this wil just copy the file and overwrite an existing file. *There does
> > not seem to be an
> > * objFile.Append (strDestination & strFile)
> > So how do I do this?
>
Quote:

> > Bono Bloksma
>
> AFAIK, there is still no append method available to scripting. *I
> wrote this subroutine many, many years ago to do the job ...
>
> ' Appends file2 to end of file1
> Sub AppendFile(sFileSpec1, sFileSpec2)
> dim fso
> * set fso = CreateObject("Scripting.FileSystemObject")
> * With .OpenTextFile(sFileSpec1, 8)
> * * .Write fso.OpentextFile(sFileSpec2, 1).ReadAll
> * End With
> End sub
>
> To use this to add two files together, use CopyFile to copy file A to
> C then append B to C. *For example, ...
>
> *FSO.CopyFile strTmpDirectory & strFileA, strDestFolder & strFileC
> *appendFile strDestFolder & strFileC, strTmpDirectory & strFileB
>
> This approach works fine for reasonably sized files, say less than
> tens of megabytes, though there is almost no memory limitation, since
> it is based on virtual (paging) memory, not strictly available RAM.
> Very large files are just slower to copy over.
>
> Of course the other way to do it is to just Run the command shell COPY
> statement from the script ...
>
> with createobject("wscript.shell")
> * .run "%comspec% /c copy " & sFileSpecA & "+" & sFileSpecB _
> * & sFileSpecC, 0, true
> end with
>
> Tom Lavedas
> ***********
Oops, I forgot the space betweeen fileB and fileC in the command line,
that is ...

.run "%comspec% /c copy " & sFileSpecA & "+" & sFileSpecB _
& sFileSpecC, 0, true

should be

.run "%comspec% /c copy " & sFileSpecA & "+" & sFileSpecB _
& " " & sFileSpecC, 0, true

Tom Lavedas
***********
My System SpecsSystem Spec
Old 07-29-2009   #4 (permalink)
mr_unreliable


 
 

Re: vbscript equivalent of copy a+b c

Bonno Bloksma wrote:
Quote:

> How to I do that in VB script?
> My last commands so far are:
> Set objFile = FSO.GetFile(strTmpDirectory & strFile)
> objFile.Copy (strDestination & strFile)
> But this wil just copy the file and overwrite an existing file. There does
> not seem to be an
> objFile.Append (strDestination & strFile)
> So how do I do this?
>
hi Bono,

There is another alternative to FSO (at least for win98),
that is the "copyhere" method of shell.application.

There are a couple of side benefits. For one, copyhere
is faster than fso. And for another, if the copy takes
long enough (don't ask what "enough" means), copyhere
will show a dialog and a progressbar. And finally,
if the dialog does show, you will also see an "animation",
which looks like papers flying through the air.

<code>
' script to demo shell.application, jw 04Dec07
Option Explicit
Dim oSHApp ' as object
Dim sSrc, sDest ' as string
Const FOF_SIMPLEPROGRESS = 256 '(&H100)

Set oSHApp = CreateObject("Shell.Application")

sSrc = "c:\windows\temp\*.*"
sDest = "A:\"

' use ms Animated Copy Applet, showing names and progressbar...
oSHApp.Namespace(sDest).CopyHere sSrc

' use ms Animated Copy Applet, showing progressbar (but no names)...
' oSHApp.Namespace(sDest).CopyHere sSrc, FOF_SIMPLEPROGRESS

Set oSHApp = nothing ' clean up
WScript.Quit
</code>

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)


My System SpecsSystem Spec
Old 07-29-2009   #5 (permalink)
Tom Lavedas


 
 

Re: vbscript equivalent of copy a+b c

On Jul 29, 1:32*pm, mr_unreliable <kindlyReplyToNewsgr...@xxxxxx>
wrote:
Quote:

> Bonno Bloksma wrote:
Quote:

> > How to I do that in VB script?
> > My last commands so far are:
> > * Set objFile = FSO.GetFile(strTmpDirectory & strFile)
> > * objFile.Copy (strDestination & strFile)
> > But this wil just copy the file and overwrite an existing file. *There does
> > not seem to be an
> > * objFile.Append (strDestination & strFile)
> > So how do I do this?
>
> hi Bono,
>
> There is another alternative to FSO (at least for win98),
> that is the "copyhere" method of shell.application.
>
> There are a couple of side benefits. *For one, copyhere
> is faster than fso. *And for another, if the copy takes
> long enough (don't ask what "enough" means), copyhere
> will show a dialog and a progressbar. *And finally,
> if the dialog does show, you will also see an "animation",
> which looks like papers flying through the air.
>
> <code>
> ' script to demo shell.application, jw 04Dec07
> Option Explicit
> Dim oSHApp *' as object
> Dim sSrc, sDest *' as string
> Const FOF_SIMPLEPROGRESS = 256 '(&H100)
>
> Set oSHApp = CreateObject("Shell.Application")
>
> * *sSrc = "c:\windows\temp\*.*"
> * *sDest = "A:\"
>
> * *' use ms Animated Copy Applet, showing names and progressbar...
> * *oSHApp.Namespace(sDest).CopyHere sSrc
>
> * *' use ms Animated Copy Applet, showing progressbar (but no names)....
> * *' oSHApp.Namespace(sDest).CopyHere sSrc, FOF_SIMPLEPROGRESS
>
> Set oSHApp = nothing *' clean up
> WScript.Quit
> </code>
>
> cheers, jw
> ____________________________________________________________
>
> You got questions? *WE GOT ANSWERS!!! *..(but, no guarantee
> * * the answers will be applicable to the questions)
Are you sure this APPENDS the source files to a single destination
FILE? The documents insist that it copies "files to a folder", which
is what your example seems to imply, as well.

Tom Lavedas
***********
My System SpecsSystem Spec
Old 07-29-2009   #6 (permalink)
mr_unreliable


 
 

Re: vbscript equivalent of copy a+b c

Tom Lavedas wrote:
Quote:

> Are you sure this APPENDS the source files to a single destination
> FILE? The documents insist that it copies "files to a folder", which
> is what your example seems to imply, as well.
>
You are right. I lost track of what the O.P. had asked for.

mea culpa, jw
My System SpecsSystem Spec
Old 07-29-2009   #7 (permalink)
Todd Vargo


 
 

Re: vbscript equivalent of copy a+b c

Tom Lavedas wrote:
Quote:

> AFAIK, there is still no append method available to scripting. I
> wrote this subroutine many, many years ago to do the job ...
>
> ' Appends file2 to end of file1
> Sub AppendFile(sFileSpec1, sFileSpec2)
> dim fso
> set fso = CreateObject("Scripting.FileSystemObject")
> With .OpenTextFile(sFileSpec1, 8)
> .Write fso.OpentextFile(sFileSpec2, 1).ReadAll
> End With
> End sub
>
> To use this to add two files together, use CopyFile to copy file A to
> C then append B to C. For example, ...
>
> FSO.CopyFile strTmpDirectory & strFileA, strDestFolder & strFileC
> appendFile strDestFolder & strFileC, strTmpDirectory & strFileB
>
> This approach works fine for reasonably sized files, say less than
> tens of megabytes, though there is almost no memory limitation, since
> it is based on virtual (paging) memory, not strictly available RAM.
> Very large files are just slower to copy over.
>
> Of course the other way to do it is to just Run the command shell COPY
> statement from the script ...
>
> with createobject("wscript.shell")
> .run "%comspec% /c copy " & sFileSpecA & "+" & sFileSpecB _
> & sFileSpecC, 0, true
> end with
IIRC, ReadAll does not work with binary files. Instead of uning ReadAll
method, use Read method and just specify a large enough chunk (x clusters
perhaps). The following makes no attempt to verify either file exists or
that the media is writable (I leave that open as an exercise).

AppendFile sFileSpecB, sFileSpecC

' Appends srcfile to dstfile regardless of content type
Function AppendFile(srcfile, dstfile)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f1, f2, Msg
Set fso = CreateObject("Scripting.FileSystemObject")
remaining = fso.GetFile(srcfile).Size
If remaining > 0 Then
Set f1 = fso.OpenTextFile(srcfile, ForReading)
Set f2 = fso.OpenTextFile(dstfile, ForAppending, True)
Do While (remaining > 0)
If remaining > 32768 then
f2.Write f1.Read(32786)
remaining = remaining - 32768
Else
f2.Write f1.Read(remaining)
remaining = 0
End If
Loop
End If
End Function

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 07-30-2009   #8 (permalink)
Bonno Bloksma


 
 

Re: vbscript equivalent of copy a+b c

Hi Tom,
Quote:

> What I'm looking for is the VBS equivalent of the prompt command
> COPY A.TXT+B.TXT C.TXT
> where in the end the file C contains te content of A and B.
[.....]
Quote:

>AFAIK, there is still no append method available to scripting. I
>wrote this subroutine many, many years ago to do the job ...
Quote:

>' Appends file2 to end of file1
>Sub AppendFile(sFileSpec1, sFileSpec2)
>dim fso
> set fso = CreateObject("Scripting.FileSystemObject")
> With .OpenTextFile(sFileSpec1, 8)
> .Write fso.OpentextFile(sFileSpec2, 1).ReadAll
> End With
>End sub
This will do nicely thank you. This will let me append the local log
fragment to the central log.
Quote:

>This approach works fine for reasonably sized files, say less than
>tens of megabytes, though there is almost no memory limitation, since
>it is based on virtual (paging) memory, not strictly available RAM.
>Very large files are just slower to copy over.
As I'm just adding a few dozen log lines to the central log, it is at max a
few kilobytes.

Bonno Bloksma


My System SpecsSystem Spec
Old 07-30-2009   #9 (permalink)
Bonno Bloksma


 
 

Re: vbscript equivalent of copy a+b c

Hi,

Are you sure there are no errors in this sub?

----------<quote>-----------------------------
AFAIK, there is still no append method available to scripting. I
wrote this subroutine many, many years ago to do the job ...

' Appends file2 to end of file1
Sub AppendFile(sFileSpec1, sFileSpec2)
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
With .OpenTextFile(sFileSpec1, 8)
.Write fso.OpentextFile(sFileSpec2, 1).ReadAll
End With
End sub
----------<quote>-----------------------------

When I call the sub I get:
Error: Invalid on unqualified reference
Code 800A01F9

What should the code look like without the with statement?
It looks like the With statment should maybe be
With fso.OpenTextFile(sFileSpec1, 8)

However, this stil fails if the central log for the day does not exist. It seems the
OpenTextFile(sFileSpec1, 8) does not create a new empty file when not present. Maybe I need another
test first and do a copy if this is the first append attempt of the day.

Bonno Bloksma


My System SpecsSystem Spec
Old 07-30-2009   #10 (permalink)
ekkehard.horner


 
 

Re: vbscript equivalent of copy a+b c

Bonno Bloksma schrieb:
Quote:

> Hi,
>
> Are you sure there are no errors in this sub?
>
[...]
Quote:

> However, this stil fails if the central log for the day does not exist. It seems the
> OpenTextFile(sFileSpec1, 8) does not create a new empty file when not present. Maybe I need another
> test first and do a copy if this is the first append attempt of the day.
[...]
Check the Docs for

object.OpenTextFile(filename[, iomode[, create[, format]]])

and concentrate on the "create" parameter.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
vbscript to copy files from netlog to local profile folder VB Script
Copy & paste from IE/FF into a new Outlook email via VBScript VB Script
how-to run sequential copy processes thru vbscript VB Script
Equivalent of powershell to vbscript command PowerShell
equivalent to vbscript "." PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46