Discussion:
copy & rename file via VB6
(too old to reply)
R-M
2003-10-02 06:39:03 UTC
Permalink
Hi

I've used following statement in VB6 and want to copy
and rename f1.txt

Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub

but following error appearred:

"Runtime error 53: file not found."

how can I do it?
any help would be greatly appreciated.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Steven Burn
2003-10-02 08:16:58 UTC
Permalink
Try something along the lines of......

Dim strOld as String, strNew As String
strOld = "c:\text1.text"
strNew = "c:\Temp1.tmp"

FileCopy("c:\text1.text", Replace(strOld, strOld, strNew))

--
Regards

Steven Burn
Ur I.T. Mate Group CEO
www.it-mate.co.uk

Disclaimer:
My advice is provided on an 'as-is' basis. Just because I tell you
something, does not mean I am right.
Post by R-M
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Frampton Firesword
2003-10-02 09:30:55 UTC
Permalink
Please test your posts!
If you'd typed that into VB it would have gone red
straight away. You don't put the brackets round a function
when you aren't returning a value from it.
-----Original Message-----
Try something along the lines of......
Dim strOld as String, strNew As String
strOld = "c:\text1.text"
strNew = "c:\Temp1.tmp"
FileCopy("c:\text1.text", Replace(strOld, strOld,
strNew))
--
Regards
Steven Burn
Ur I.T. Mate Group CEO
www.it-mate.co.uk
My advice is provided on an 'as-is' basis. Just because I
tell you
something, does not mean I am right.
Post by R-M
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
http://www.opera.com/m2/
.
Steven Burn
2003-10-03 00:11:21 UTC
Permalink
My apologies..... I was half asleep when I typed it.

--
Regards

Steven Burn
Ur I.T. Mate Group CEO
www.it-mate.co.uk

Disclaimer:
My advice is provided on an 'as-is' basis. Just because I tell you
something, does not mean I am right.
Post by Frampton Firesword
Please test your posts!
If you'd typed that into VB it would have gone red
straight away. You don't put the brackets round a function
when you aren't returning a value from it.
-----Original Message-----
Try something along the lines of......
Dim strOld as String, strNew As String
strOld = "c:\text1.text"
strNew = "c:\Temp1.tmp"
FileCopy("c:\text1.text", Replace(strOld, strOld,
strNew))
--
Regards
Steven Burn
Ur I.T. Mate Group CEO
www.it-mate.co.uk
My advice is provided on an 'as-is' basis. Just because I
tell you
something, does not mean I am right.
Post by R-M
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
http://www.opera.com/m2/
.
M R
2003-10-04 04:35:17 UTC
Permalink
Thanks
I tried your solution like below,

Private Sub Command1_Click()
s = FileCopy("c:\m.dbf", "c:\m13.dbf")
MsgBox "ok"
End Sub

but I've got "compiler error : expected function or variable."


what happened?
I'd be thankful if you direct me.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Randy Birch
2003-10-04 14:12:37 UTC
Permalink
FileCopy is a statement, not a function. It does not return a value. Use:

FileCopy "c:\m.dbf", "c:\m13.dbf"
MsgBox "ok"
--
Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


"M R" <***@yahoo.co.uk> wrote in message news:***@TK2MSFTNGP11.phx.gbl...
: Thanks
: I tried your solution like below,
:
: Private Sub Command1_Click()
: s = FileCopy("c:\m.dbf", "c:\m13.dbf")
: MsgBox "ok"
: End Sub
:
: but I've got "compiler error : expected function or variable."
:
:
: what happened?
: I'd be thankful if you direct me.
:
:
: *** Sent via Developersdex http://www.developersdex.com ***
: Don't just participate in USENET...get rewarded for it!
Christophe
2003-10-02 08:28:42 UTC
Permalink
Hello, use this class and test if file exist ( function "existe") before
copy.
Function "deplace" will copy the file

Christophe VERGON

'---------------------------------------------------------------------------
------------
' Module : Nomfichier
' DateTime : 02/10/03 10:24
' Author : VERGON Christophe
' Purpose : File Name how to delete, copy, replace
'---------------------------------------------------------------------------
------------
Private mchemin As String
Private mnom As String
Private mextention As String * 3

Public Property Get chemin() As String
chemin = mchemin
End Property

Public Property Let chemin(ByVal vNewValue As String)
If Mid$(vNewValue, Len(vNewValue), 1) <> "\" Then
vNewValue = vNewValue & "\"
End If
mchemin = vNewValue
End Property
Public Property Get nomfichier() As String
nomfichier = mnom
End Property
Public Property Let nomfichier(ByVal vNewValue As String)
mnom = vNewValue
End Property
Public Property Get extention() As String
extention = mextention
End Property

Public Property Let extention(ByVal vNewValue As String)
mextention = vNewValue
End Property

Public Function existe() As Boolean
Dim a$
a$ = Dir(mchemin & mnom & "." & mextention)
If a$ = "" Then
existe = False
Else
existe = True
End If

End Function

Public Sub renomme(NewName As String)
Name mchemin & mnom & "." & mextention As mchemin & NewName & "." &
mextention
End Sub

Public Sub deplace(newpath As String)
If Mid$(newpath, Len(newpath), 1) <> "\" Then
newpath = newpath & "\"
End If
Name mchemin & mnom & "." & mextention As newpath & mnom & "." & mextention
End Sub

Public Sub CopieEtRenomme(NewName As String, ext As String)

Dim oldname As String
Dim newcompletename As String
On Error GoTo CopieEtRenomme_Error



oldname = mchemin & mnom & "." & mextention
newcompletename = mchemin & NewName & "." & ext
FileCopy oldname, newcompletename

On Error GoTo 0
Exit Sub

CopieEtRenomme_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
CopieEtRenomme of Module de classe Nomfichier"

End Sub
Public Sub efface()
Dim n As String
n = mchemin & mnom & "." & mextention
Kill (n)
End Sub
Post by R-M
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Frampton Firesword
2003-10-02 09:26:16 UTC
Permalink
It's a bad idea to display a message box in a class - much
better to raise an error, then the client of the class can
choose whether to display a message box or work out for
itself why the error occured.
-----Original Message-----
Hello, use this class and test if file exist (
function "existe") before
copy.
Function "deplace" will copy the file
Christophe VERGON
'---------------------------------------------------------
------------------
------------
' Module : Nomfichier
' DateTime : 02/10/03 10:24
' Author : VERGON Christophe
' Purpose : File Name how to delete, copy, replace
'---------------------------------------------------------
------------------
------------
Private mchemin As String
Private mnom As String
Private mextention As String * 3
Public Property Get chemin() As String
chemin = mchemin
End Property
Public Property Let chemin(ByVal vNewValue As String)
If Mid$(vNewValue, Len(vNewValue), 1) <> "\" Then
vNewValue = vNewValue & "\"
End If
mchemin = vNewValue
End Property
Public Property Get nomfichier() As String
nomfichier = mnom
End Property
Public Property Let nomfichier(ByVal vNewValue As String)
mnom = vNewValue
End Property
Public Property Get extention() As String
extention = mextention
End Property
Public Property Let extention(ByVal vNewValue As String)
mextention = vNewValue
End Property
Public Function existe() As Boolean
Dim a$
a$ = Dir(mchemin & mnom & "." & mextention)
If a$ = "" Then
existe = False
Else
existe = True
End If
End Function
Public Sub renomme(NewName As String)
Name mchemin & mnom & "." & mextention As mchemin &
NewName & "." &
mextention
End Sub
Public Sub deplace(newpath As String)
If Mid$(newpath, Len(newpath), 1) <> "\" Then
newpath = newpath & "\"
End If
Name mchemin & mnom & "." & mextention As newpath & mnom
& "." & mextention
End Sub
Public Sub CopieEtRenomme(NewName As String, ext As
String)
Dim oldname As String
Dim newcompletename As String
On Error GoTo CopieEtRenomme_Error
oldname = mchemin & mnom & "." & mextention
newcompletename = mchemin & NewName & "." & ext
FileCopy oldname, newcompletename
On Error GoTo 0
Exit Sub
MsgBox "Error " & Err.Number & " (" & Err.Description
& ") in procedure
CopieEtRenomme of Module de classe Nomfichier"
End Sub
Public Sub efface()
Dim n As String
n = mchemin & mnom & "." & mextention
Kill (n)
End Sub
Post by R-M
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
http://www.opera.com/m2/
.
Larry Serflaten
2003-10-02 13:26:12 UTC
Permalink
Hello, use this class and test if file exist ( function "existe") before copy.
Function "deplace" will copy the file
Just as an observation, it would help if you would have included
English translations in the code you posted (to an English speaking group).
The routines will not be useful unless it is known what they do.

You offered one translation, but posted several others without a clue as to
Public Property Get chemin() As String
Public Property Get nomfichier() As String
Public Sub renomme(NewName As String)
Public Sub CopieEtRenomme(NewName As String, ext As String)
Public Sub efface()
Again as an observation, why do you use local names for the subs
but pass in English named parameters? That is a bit curious, to me
anyway.... :-)

LFS
Christophe
2003-10-02 18:16:51 UTC
Permalink
Hi,

at fisrt the question was : is the file existing ?

secondly I was a little short in time to translate all, but with a little
reflexion I was thinking you will understand what the class do.
Post by Larry Serflaten
Public Property Get chemin() As String: getPathname
Public Property Get nomfichier() As String : get file name
Public Sub renomme(NewName As String): rename
Public Sub CopieEtRenomme(NewName As String, ext As String) : copy and
rename
Post by Larry Serflaten
Public Sub efface() : delete
Again as an observation, why do you use local names for the subs
but pass in English named parameters? That is a bit curious, to me
anyway.... :-)
They're not local names, but french name. I use the both english or french
name, it depend of my feeling.

Sorry,

But I think that the problem is solved ? No ?

Christophe Vergon
Post by Larry Serflaten
Hello, use this class and test if file exist ( function "existe") before copy.
Function "deplace" will copy the file
Just as an observation, it would help if you would have included
English translations in the code you posted (to an English speaking group).
The routines will not be useful unless it is known what they do.
You offered one translation, but posted several others without a clue as to
Public Property Get chemin() As String
Public Property Get nomfichier() As String
Public Sub renomme(NewName As String)
Public Sub CopieEtRenomme(NewName As String, ext As String)
Public Sub efface()
Again as an observation, why do you use local names for the subs
but pass in English named parameters? That is a bit curious, to me
anyway.... :-)
LFS
Frampton Firesword
2003-10-02 09:32:46 UTC
Permalink
To correct the code you've written...
m = Shell(Environ("comspec") & " copy c:\f1.txt
d:\f2.txt", vbMaximizedFocus)

(get rid of the linefeed)

but Steven Burn's suggestion is better - although he does
seem to have got subs confused with functions, see my
reply to his post.
-----Original Message-----
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
http://www.opera.com/m2/
.
M R
2003-10-04 04:46:27 UTC
Permalink
Thanks
I tried your solution like below:

Private Sub Command1_Click()
m = Shell(Environ("comspec") & " copy c:\f1.txt " & _
"d:\f2.txt", vbMaximizedFocus)

MsgBox "ok"

End Sub

but following error happened (in Dos enviroment):
"specified command search directory bad:
too many parameters
too many parameters"


thanks





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Randy Birch
2003-10-04 14:17:18 UTC
Permalink
Though FileCopy is preferred, the syntax for shelling to the command prompt
to do this is :

Dim m As Long
m = Shell(Environ("comspec") & " /c copy d:\m.txt " & "d:\q.txt",
vbNormalFocus)
--
Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


"M R" <***@yahoo.co.uk> wrote in message news:***@TK2MSFTNGP10.phx.gbl...
:
: Thanks
: I tried your solution like below:
:
: Private Sub Command1_Click()
: m = Shell(Environ("comspec") & " copy c:\f1.txt " & _
: "d:\f2.txt", vbMaximizedFocus)
:
: MsgBox "ok"
:
: End Sub
:
: but following error happened (in Dos enviroment):
: "specified command search directory bad:
: too many parameters
: too many parameters"
:
:
: thanks
:
:
:
:
:
: *** Sent via Developersdex http://www.developersdex.com ***
: Don't just participate in USENET...get rewarded for it!
Rick Rothstein
2003-10-04 14:45:02 UTC
Permalink
Post by Randy Birch
Though FileCopy is preferred, the syntax for shelling to the command prompt
Dim m As Long
m = Shell(Environ("comspec") & " /c copy d:\m.txt " & "d:\q.txt",
vbNormalFocus)
With the added possibility of having to surround one or both Path-FileNames
with quote marks if embedded blank spaces are present in them (the quotes
will protect the blanks from being interpreted as delimiters by DOS).

Rick - MVP
Frampton Firesword
2003-10-04 23:04:57 UTC
Permalink
so that would be either
m = Shell(Environ("comspec") & " /c copy ""d:\m.txt"" ""d:\q.txt""",

or

m = Shell(Environ("comspec") & " /c copy d:\m.txt d:\q.txt",

but not

m = Shell(Environ("comspec") & " /c copy d:\m.txt " & "d:\q.txt", as that's
just a concatenation where one's not necessary.
Post by Randy Birch
Post by Randy Birch
Though FileCopy is preferred, the syntax for shelling to the command
prompt
Post by Randy Birch
Dim m As Long
m = Shell(Environ("comspec") & " /c copy d:\m.txt " & "d:\q.txt",
vbNormalFocus)
With the added possibility of having to surround one or both
Path-FileNames
Post by Randy Birch
with quote marks if embedded blank spaces are present in them (the quotes
will protect the blanks from being interpreted as delimiters by DOS).
Rick - MVP
Rick Rothstein
2003-10-05 02:04:57 UTC
Permalink
Post by Frampton Firesword
so that would be either
m = Shell(Environ("comspec") & " /c copy ""d:\m.txt"" ""d:\q.txt""",
Yes, for any path-filenam.
Post by Frampton Firesword
or
m = Shell(Environ("comspec") & " /c copy d:\m.txt d:\q.txt",
Yes for path or names without embedded spaces; no otherwise.
Post by Frampton Firesword
but not
m = Shell(Environ("comspec") & " /c copy d:\m.txt " & "d:\q.txt", as that's
just a concatenation where one's not necessary.
Correct. The more general case would be something like this...

PathFileNameFrom = "d:\m.txt"
PathFileNameTo = "d:\q.txt"
Shell Environ$("comspec") & " /c copy """ & _
PathFileNameFrom & """ """ & _
PathFileNameTo & """", vbHide

Note that I used the subroutine format for this instead of the function
format (we're not doing anything with the return value anyway); that I added
the $ sign to the Environ function name (to make it return a String rather
than a Variant), and included the vbHide parameter (so the statement will be
executed invisibly).

Rick - MVP
Frampton Firesword
2003-10-05 16:58:29 UTC
Permalink
I always use the "" in order to pass filenames in quotes to MSDOS, just in
case they do have embedded spaces, I also always invariably use the 'sub'
form of shell, rather than the 'function' form.
Post by Rick Rothstein
Post by Frampton Firesword
so that would be either
m = Shell(Environ("comspec") & " /c copy ""d:\m.txt"" ""d:\q.txt""",
Yes, for any path-filenam.
Post by Frampton Firesword
or
m = Shell(Environ("comspec") & " /c copy d:\m.txt d:\q.txt",
Yes for path or names without embedded spaces; no otherwise.
Post by Frampton Firesword
but not
m = Shell(Environ("comspec") & " /c copy d:\m.txt " & "d:\q.txt", as
that's
Post by Frampton Firesword
just a concatenation where one's not necessary.
Correct. The more general case would be something like this...
PathFileNameFrom = "d:\m.txt"
PathFileNameTo = "d:\q.txt"
Shell Environ$("comspec") & " /c copy """ & _
PathFileNameFrom & """ """ & _
PathFileNameTo & """", vbHide
Note that I used the subroutine format for this instead of the function
format (we're not doing anything with the return value anyway); that I added
the $ sign to the Environ function name (to make it return a String rather
than a Variant), and included the vbHide parameter (so the statement will be
executed invisibly).
Rick - MVP
Norm Cook
2003-10-02 11:41:29 UTC
Permalink
See the Name statement.
Post by R-M
Hi
I've used following statement in VB6 and want to copy
and rename f1.txt
Private Sub Command1_Click()
m = Shell("copy c:\f1.txt d:\f2.txt", vbMaximizedFocus)
MsgBox "ok."
End Sub
"Runtime error 53: file not found."
how can I do it?
any help would be greatly appreciated.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
michale snider
2003-10-03 20:24:45 UTC
Permalink
have a look at the file system object it can do all that In project
references select Microsoft scripting runtime.
In your module
add the following
Global fs As New FileSystemObject

From then on you have full access the file system objects properties
Us fs.filexists to validate the source exist then fs.filecopy to copy to
new path and name.

Alternatively if you don't want to use the references elsewhere use the
following.
Dim nn as boolean
Set fs = CreateObject("Scripting.FileSystemObject")
nn = fs.fileexists("C:\ip.txt")
if nn=true then fs.FileCopy "C:\ip.txt", "C:\newip.txt"
Hopfully this will fit yur needs ?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Steven Burn
2003-10-03 21:05:54 UTC
Permalink
Using FSO for FileCopy is completely unnecessary.

The FileCopy API will do this without the need for extra components.

Dim strOld as String, strNew As String
strOld = "c:\text1.text"
strNew = "c:\Temp1.tmp"

FileCopy "c:\text1.text", Replace(strOld, strOld, strNew)


--
Regards

Steven Burn
Ur I.T. Mate Group CEO
www.it-mate.co.uk

Disclaimer:
My advice is provided on an 'as-is' basis. Just because I tell you
something, does not mean I am right.
Post by michale snider
have a look at the file system object it can do all that In project
references select Microsoft scripting runtime.
In your module
add the following
Global fs As New FileSystemObject
From then on you have full access the file system objects properties
Us fs.filexists to validate the source exist then fs.filecopy to copy to
new path and name.
Alternatively if you don't want to use the references elsewhere use the
following.
Dim nn as boolean
Set fs = CreateObject("Scripting.FileSystemObject")
nn = fs.fileexists("C:\ip.txt")
if nn=true then fs.FileCopy "C:\ip.txt", "C:\newip.txt"
Hopfully this will fit yur needs ?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Rick Rothstein
2003-10-03 23:47:54 UTC
Permalink
Post by Steven Burn
Using FSO for FileCopy is completely unnecessary.
Correct!
Post by Steven Burn
The FileCopy API will do this without the need for extra components.
Actually, FileCopy is not an API function... it is a built-in VB command.
Post by Steven Burn
Dim strOld as String, strNew As String
strOld = "c:\text1.text"
strNew = "c:\Temp1.tmp"
FileCopy "c:\text1.text", Replace(strOld, strOld, strNew)
Uhhh... Replace(strOld, strOld, strNew) is exactly equal to strNew. Simply
use

FileCopy strOld, strNew

given the variables you declare and made assignments to.

Rick - MVP
Steven Burn
2003-10-04 02:35:06 UTC
Permalink
"Actually, FileCopy is not an API function... it is a built-in VB command."

hehe woops ;o) (I knew what I meant if that counts?)

--
Regards

Steven Burn
Ur I.T. Mate Group CEO
www.it-mate.co.uk

Disclaimer:
My advice is provided on an 'as-is' basis. Just because I tell you
something, does not mean I am right.
Post by Rick Rothstein
Post by Steven Burn
Using FSO for FileCopy is completely unnecessary.
Correct!
Post by Steven Burn
The FileCopy API will do this without the need for extra components.
Actually, FileCopy is not an API function... it is a built-in VB command.
Post by Steven Burn
Dim strOld as String, strNew As String
strOld = "c:\text1.text"
strNew = "c:\Temp1.tmp"
FileCopy "c:\text1.text", Replace(strOld, strOld, strNew)
Uhhh... Replace(strOld, strOld, strNew) is exactly equal to strNew. Simply
use
FileCopy strOld, strNew
given the variables you declare and made assignments to.
Rick - MVP
Loading...