Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copying a text file from one location to another

Posted on 2000-04-01
6
Medium Priority
?
166 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

618 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