Solved

Copying a text file from one location to another

Posted on 2000-04-01
6
160 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 65
VBA: Add rows to listbox based on criteria 7 80
Access 2016 VB code 9 103
Zip Folders Using Chilkat Routines 1 17
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…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

26 Experts available now in Live!

Get 1:1 Help Now