Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Operations in VB! 3

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I am coding VBA in Access 97. I want a functionality that would move a file(entered by input) from a know directory to another know directory.
From artile "HOWTO: Use the Animated Copy Functions in Windows 95/98" in MSDN, I have modified its code to form what I called a moveFile function which takes in the filename(entered by input) and the Form from which the function is initiated.
As you see, the code from MSDN uses Windows API, and it is not 'neat'. Ideally, I want to manipulate the Files/Directories as objects, but I can't find any Object libraries (Explorer,File MAnager, File, etc. etc.) that would allow me to do just that.

When I attempt to run the following code, I received error "File blahblah could not be found, etc. etc."
I can't debug it!!
Code:
 Public Sub moveFile(fileName As String, theForm As Form)
            Dim lenFileop As Long
            Dim foBuf() As Byte
            Dim fileop As SHFILEOPSTRUCT
            Dim result As Long
            lenFileop = LenB(fileop)    ' double word alignment increase
            ReDim foBuf(1 To lenFileop) ' the size of the structure.
            MsgBox homeDir & "hold\" & fileName
            With fileop
                    .hwnd = theForm.hwnd
                    .wFunc = FO_MOVE
                  .pFrom = homeDir & "hold\" & fileName
                  
                    .pTo = homeDir & "open\" & fileName
                    .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or _
                            FOF_NOCONFIRMMKDIR
            End With
            ' Now we need to copy the structure into a byte array
            Call CopyMemory(foBuf(1), fileop, lenFileop)
            ' Next we move the last 12 bytes by 2 to byte align the data
            Call CopyMemory(foBuf(19), foBuf(21), 12)
            result = SHFileOperation(foBuf(1))
            'If result <> 0 Then  ' Operation failed
            '   MsgBox Err.LastDllError 'Show the error returned from
            '                           'the API.               Else
            '   If fileop.fAnyOperationsAborted <> 0 Then
            '      MsgBox &quot;Operation Failed&quot;
            '   End If
            'End If
 End Sub
Could anyone see what's wrong with this??

Just now, I found something called 'FileSystemObject' for VB, but When I attempt to type:
Code:
Dim theFileObj as new FileSystemObject
, FileSystemObject does not get recognised by VBA in Access!!!!
The file I need to move is actually Excel97 file, would that be OK?? What object libary do I need to move files with this object?
[sig][/sig]
 
In order to use the FileSystemObject object, you need to reference the Microsoft Scripting Runtime library, Scrrun.dll.
I'm not sure about Access 97 (been a long time since I've used it), but in VB6, this library didn't automatically appear in my list of components in the References window. I had to manually browse for the Scrrun.dll file and add it to my list of references.

In this library, there are 2 main object that may be of interest to you: FileSystemObject, which you have discovered, is sort of a &quot;global&quot; type of object that can perform various actions on the entire file system.
The File object, on the other hand, can perform various functions on a single file (read, write, move, etc).

A File object is instantiated something like:

Dim FSO as Scripting.FileSystemObject
Dim thisFile as Scripting.File

Set FSO = New FileSystemObject
Set thisFile = FSO.GetFile(&quot;c:\Winnt\Kernel32.dll&quot;)
thisFile.Delete LOL

Hope this helps.

Steve [sig][/sig]
 
Copies a file.

Syntax

FileCopy source, destination
Unless this is 'Learning Exercise' with the requirement to use the FileSystemObject or API call, look at the following - this is directly copied from Ms Access 97 help &quot;FileCopy:


The FileCopy statement syntax has these named arguments:

Part Description
source Required. String expression that specifies the name of the file to be copied. The source may include directory or folder, and drive.
destination Required. String expression that specifies the target file name. The destination may include directory or folder, and drive.
Remarks

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


and this is the &quot;example&quot; code:

This example uses the FileCopy statement to copy one file to another. For purposes of this example, assume that SRCFILE is a file containing some data.

Dim SourceFile, DestinationFile
SourceFile = &quot;SRCFILE&quot; ' Define source file name.
DestinationFile = &quot;DESTFILE&quot; ' Define target file name.
FileCopy SourceFile, DestinationFile ' Copy source to target.
[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top