Solved

Copying a text file from one location to another

Posted on 2000-04-01
6
164 Views
Last Modified: 2010-05-02
I want to copy a specified file from certain drive and folder to another location on a different drive. How do I go about this so that the app recognizes the the source and destination paths, whether or not they currently exist.
0
Comment
Question by:mallards
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Expert Comment

by:mfclaes
ID: 2676779
Hi Mallards,

maybe this might help you
this is not all my code,
but I don't remember where it came from





Option Explicit

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Boolean
    hNameMappings As Long
    lpszProgressTitle As String
End Type

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Const FO_COPY = &H2
Private Const FOF_ALLOWUNDO = &H40
Public Sub SHCopyFile(ByVal from_file As String, ByVal to_file As String)
Dim sh_op As SHFILEOPSTRUCT

    With sh_op
        .hWnd = 0
        .wFunc = FO_COPY
        .pFrom = from_file & vbNullChar & vbNullChar
        .pTo = to_file & vbNullChar & vbNullChar
        .fFlags = FOF_ALLOWUNDO
    End With

    SHFileOperation sh_op
End Sub
Function FileExists(FileName As String) As Boolean
 
    On Error GoTo errorhandler
       
        FileExists = (GetAttr(FileName) And vbDirectory) = 0
   
errorhandler:
        'if an error occurs, this function returns false
 
Exit Function
 
End Function

Function Direxists(Dirname As String) As Boolean
 
On Error GoTo errorhandler:
       
        Direxists = (GetAttr(Dirname) And vbDirectory)
   
errorhandler:
        ' if an error occurs, this function returns false
 
Exit Function

End Function
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2676795
Add a reference (Vb Menu -> Project -> References) to your project to "Microsoft Scripting Runtime" (SCRRUN.DLL).. and then you can use a FileSystemObject:

<----- Code Begin ----->

Dim strSource As String, strTarget As String
strSource = "c:\SourcePath\MyFile.Txt"
strTarget = "c:\TargetPath\MyFile.Txt"

Dim fso As New FileSystemObject
Dim fil As File

Set fil = fso.GetFile(strSource)

' To Copy
fil.Copy (strTarget)

' To Move
fil.Move (strTarget)

Set fil = Nothing
Set fso = Nothing

<----- Code End ----->
0
 

Expert Comment

by:catdaddy123
ID: 2677054
Ive seen a great Shfileoperation example at http://616.org/ under the VB section
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:wsh2
ID: 2677294
Now.. the last method to show ya.. is pure and simple Visual Basic.. <smile>

<----- Code Begin ----->

Dim strSource As String, strTarget As String
strSource = "c:\SourcePath\MyFile.Txt"
strTarget = "c:\TargetPath\MyFile.Txt"

Dim intFile1 as Integer
intFile1 = Freefile
Open strSource For Binary Access Read as IntFile1

Dim intFile2 as Integer
intFile2 = Freefile
Open strSource For Binary Access Write as IntFile2

Dim strBuffer as String
strBuffer = Space(LOF(intFile1))
Get intFile1,,strBuffer
Put intFile2,,strBuffer

Close(intFile1)
Close(intFile2)

<----- Close End ----->
0
 

Expert Comment

by:mfclaes
ID: 2677662
I just remembers that VB has a build in function called FileCopy

If you try to use the FileCopy statement on a currently open file, an error occurs.

here is an example

Dim SourceFile, DestinationFile
SourceFile = "SRCFILE"   ' Define source file name.
DestinationFile = "DESTFILE"   ' Define target file name.
FileCopy SourceFile, DestinationFile   ' Copy source to target.

0
 
LVL 14

Expert Comment

by:wsh2
ID: 2682469
Oh my goodness.. <slapping forehead and smiling>.. mfclaes you are right.. Great catch!!!.. <smile>.

However, there is a caveat with the Filecopy statement. If you try to use the FileCopy statement on a currently open file, an error will occur.. <sigh>.

Nevertheless.. well done mfclaes.. <smile>.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question