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!

Move File Method and Rename Question 3

Status
Not open for further replies.

belstoy

Technical User
May 31, 2005
37
US
I am attempting to move a file and rename it. The code I am currently trying to use is:

[My.Computer.FileSystem.MoveFile("C:\My Documents\File2*.xls", "C:\My Documents\Sales\Processed2.xls")]

I am getting a compile error.

Any thoughts?, please advise.

Belstoy

 
That is VB .NET code.
You will need to ask in Forum796 if you are using VB .NET.
 
Code:
Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  fso.MoveFile "C:\My Documents\File2*.xls", "C:\My Documents\Sales\Processed2.xls"

I hope this helps.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Thanks,

I am rookie when it comes to this stuff...
I am still having some issues. Is there another alternative? My system doesn't seem to respond to this code.

Thanks for your help!

Belstoy

 

er, I see you are tring to copy multi files using a wild card to a single file. That cannot be done that way.

See the VB DIR() method in the VB help files and use it to loop through a pattern of files, and then use the fso code above (or the VBA FileCopy() and Kill functions) to move each individual file separately
 
Thanks,

I am actually just trying to move one file. It's just that the file will always have a different data extension on it.
Example: File2120106 or File2010107 (that's why I was using the wildcard).

If I use the DIR function:
[MyFile = Dir("C:\My Documents\File2*.xls")]

Can I use the MoveFile Method in association with MyFile somehow?

Sorry I'm such a novice.

Thanks,
Belstoy



 
use the FSO code above, use the variable (MyFile) holding the file name as the source file
 
Can you show me how exactly how that code should look using MyFile?

[MyFile = Dir("C:\My Documents\File2*.xls")]

[Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile "C:\My Documents\MyFile", "C:\My Documents\Sales\Processed2.xls"]

Thanks,
Belstoy
 
Ok. I was only going to give you a small piece to get you going, but I was in a good mood and decided to add alot of extra checking and error handling, to help you further.
So yes, it could have been done in 4 lines of code:
Code:
[COLOR=blue]
Public Function MoveAndRenameMyFile(SourcePath As String, SourcePattern As String, Destination As String) As Boolean
[/color][COLOR=green]
    '=======================================================
    'Call MoveAndRenameMyFile("C:\My Documents\", "File2*.xls", "C:\My Documents\Sales\Processed2.xls")
    '=======================================================
[/color][COLOR=blue]
    Dim oFso            As Object
    Dim MyFile          As String
    
    On Error GoTo ErrorOut
    
    Set oFso = CreateObject("Scripting.FileSystemObject")
    MyFile = Dir(SourcePath & SourcePattern)
    
    'Loop until a matching file is found
    Do Until LenB(MyFile) = 0
    
        'Pattern matching check
        If UCase$(MyFile) Like UCase$(SourcePattern) Then
        
            'Found a similar file. Make sure we are not copying it to the same location
            If StrComp(SourcePath & MyFile, Destination, vbTextCompare) <> 0 Then
            
                'Copy to new location, OVERWRITING any file with the same name.
                'Then DELETE the original
                Call oFso.CopyFile(SourcePath & MyFile, Destination, True)
                Call oFso.DeleteFile(SourcePath & MyFile, True)
            Else
                'Failed!
                Err.Raise vbObjectError + 512 + 101, "MoveAndRenameMyFile", "Source and Destination identical"
            End If
            'Success
            Exit Do
        End If
        Call Dir
    Loop
    
ErrorOut:
    If Err.Number <> 0 Then
        Err.Raise Err.Number, "MoveAndRenameMyFile", Err.Description
    Else
        If LenB(MyFile) = 0 Then
            'Failed!
            'MsgBox "No File Found"
            Err.Raise vbObjectError + 512 + 101, "MoveAndRenameMyFile", "No File Found"
        Else
            MoveAndRenameMyFile = True 'Success!
        End If
    End If
End Function
[/color]


Call it like this:

Call MoveAndRenameMyFile("C:\My Documents\", "File2*.xls", "C:\My Documents\Sales\Processed2.xls")


 
Thanks so much. This is extremely helpful. I have finally made it work.

Thanks again!
 

Great!

>I have finally made it work
What have you done to get it to work? Did you also try the code I posted, and did it not work?
 
Could you not use :
Code:
 Name "C:\My Documents\File2*.xls" As "C:\My Documents\Sales\Processed2.xls"

Everybody body is somebodys Nutter.
 
SBerthold,

Your code did work. I thank you.

Belstoy
 
Oh, you are welcome!
(I was only wondering because of the post marked pink star only on another post and because of your previous comment, quessed you had decided my code was overflow, or you came up with a similar solution).
But that's fine, now that I know.
 
I have a similar problem to one that was previously posted and didn't want to create a new thread for copying. My dilemma is this: My utility is backing up an entire directory listing from one place to another (all files/folders). This works if the folder structure is different in both the source and destination directories. It works the first time through if no files/folders exist in the destination directory, but once they are there, the second run of the app dies when trying to make a directory that already exists.

I need the copy functions to do two things:
1)Ignore making directories if they already exist
2)Ignore copying the same files if they already exist

I only want to backup new data when i pass the same source and destination directories.

Here is the code that I have thus far:

============================================================
' Copy all files below this directory.
Private Sub CopyFiles(ByVal source_dir As String, ByVal destination_dir As String)
Dim dirs As Collection
Dim fname As String
Dim search_handle As Long
Dim file_data As WIN32_FIND_DATA
Dim i As Integer

Set dirs = New Collection

' Get the first file.
search_handle = FindFirstFile( _
source_dir & "*.*", file_data)
If search_handle <> INVALID_HANDLE_VALUE Then
' Get the rest of the files.
Do
' Get the file name.
fname = file_data.cFileName
fname = Left$(fname, InStr(fname, Chr$(0)) - 1)

' Skip the files "." and "..".
If fname <> "." And fname <> ".." Then

' See if the file is a directory.
If file_data.dwFileAttributes And DDL_DIRECTORY Then
' This is a directory.
' Make the new directory.
'
'*************************************
'
' Add destination_dir check
'
'*************************************

MkDir destination_dir & fname ' crashes here on second run if directory exists

' Save the directory name so
' we can search it later.
dirs.Add fname
Else
' This is not a directory.
' Copy the file.
'
'*************************************
'
' possible file check
'
'*************************************

FileCopy source_dir & fname, destination_dir & fname
End If
End If

' Get the next file.
If FindNextFile(search_handle, file_data) = 0 Then Exit Do
Loop

' Close the file search hanlde.
FindClose search_handle
End If

' Search subdirectories.
For i = 1 To dirs.Count
fname = dirs(i)
Call CopyFiles(source_dir & fname & "\", destination_dir & fname & "\")
Next i

End Sub
------------------------------------------------------------
' Copy source_file to destination_file. If source_file is a
' directory, copy it and its files, creating 'destination_file if necessary.

Private Sub XCopyFile(ByVal source_file As String, ByVal destination_file As String)

' See if source_file is a directory.
If GetAttr(source_file) And vbDirectory Then
' This is a directory.
If Right$(source_file, 1) <> "\" Then source_file = source_file & "\"
If Right$(destination_file, 1) <> "\" Then destination_file = _
destination_file & "\"

' Create destination_file if necessary.
On Error Resume Next
MkDir destination_file
On Error GoTo 0

' Copy the files in the directory.
Call CopyFiles(source_file, destination_file)
Else
' This is not a directory.
' Copy the file.
FileCopy source_file, destination_file

End If

End Sub
============================================================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top