Solved

Copying a text file from one location to another

Posted on 2000-04-01
6
163 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
Industry Leaders: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

733 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