Solved

Copying a text file from one location to another

Posted on 2000-04-01
6
159 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
z = x + y – 1 6 58
Access 2013 combo box not working 3 27
using web browser with BING 40 89
VB6 - Convert HH:MM into Decimal 8 38
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now