Results 1 to 7 of 7

Thread: Re-naming files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Question

    Hi there

    I am not sure if any other threads have covered this topic, if so could you point me in the right direction...

    I am looking for some code that is used to re-name all files within a certain directory.

    something along the lines of

    Public sub Change_Name()
    Dim File_Path as string

    File_Path = "C:/my documents"

    'Somehow find all files with a particular extension for me it will be .rep

    'then code to change the .rep part to .txt

    For "all files in directory??"
    Mystring = Left(Myfilename,Instr(1,Myfilename,"."))
    Mystring = Mystring & ".txt"
    next

    End sub

    Or would it be easier using replace ?

    Any help would be appreciated

    Thanks
    Steve

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986

    Renaming

    To rename files you could ofcourse Copy the file and then the delete the old, but that would be kinda crappy and slow, so you this code instead.

    Code:
    'Put this in a module
    Public Declare Function SHFileOperation Lib _
    "shell32.dll" Alias "SHFileOperationA" _
    (lpFileOp As Any) As Long
    Public Declare Sub SHFreeNameMappings Lib _
    "shell32.dll" (ByVal hNameMappings As Long)
    
    Public Declare Sub CopyMemory Lib "KERNEL32" _
    Alias "RtlMoveMemory" (hpvDest As Any, hpvSource _
    As Any, ByVal cbCopy As Long)
    Public Type SHFILEOPSTRUCT
                   hwnd As Long
                   wFunc As FO_Functions
                   pFrom As String
                   pTo As String
                   fFlags As FOF_Flags
                   fAnyOperationsAborted As Long
                   hNameMappings As Long
                   lpszProgressTitle As String 'only used if FOF_SIMPLEPROGRESS
                   End Type
    
                   Public Enum FO_Functions
                   FO_MOVE = &H1
                   FO_COPY = &H2
                   FO_DELETE = &H3
                   FO_RENAME = &H4
                   End Enum
    
                   Public Enum FOF_Flags
                   FOF_MULTIDESTFILES = &H1
                   FOF_CONFIRMMOUSE = &H2
                   FOF_SILENT = &H4
                   FOF_RENAMEONCOLLISION = &H8
                   FOF_NOCONFIRMATION = &H10
                   FOF_WANTMAPPINGHANDLE = &H20
                   FOF_ALLOWUNDO = &H40
                   FOF_FILESONLY = &H80
                   FOF_SIMPLEPROGRESS = &H100
                   FOF_NOCONFIRMMKDIR = &H200
                   FOF_NOERRORUI = &H400
                   FOF_NOCOPYSECURITYATTRIBS = &H800
                   FOF_NORECURSION = &H1000
                   FOF_NO_CONNECTED_ELEMENTS = &H2000
                   FOF_WANTNUKEWARNING = &H4000
                   End Enum
    
                   Public Type SHNAMEMAPPING
                   pszOldPath As String
                   pszNewPath As String
                   cchOldPath As Long
                   cchNewPath As Long
                   End Type
    
    Public Function SHFileOP(ByRef lpFileOp As SHFILEOPSTRUCT) As Long
    'This uses a method suggested at MSKB to
    'ensure that all parameters are passed correctly
    'Call this wrapper rather than the API function directly
    
    Dim result As Long
    Dim lenFileop As Long
    Dim foBuf() As Byte
    
    lenFileop = LenB(lpFileOp)
    ReDim foBuf(1 To lenFileop) 'the size of the structure.
    
    'Now we need to copy the structure into a byte array
    Call CopyMemory(foBuf(1), lpFileOp, 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))
    
    SHFileOP = result
    End Function
    
    'Put this in a form.
    Dim lret As Long
    Dim fileop As SHFILEOPSTRUCT
    
    With fileop
    .hwnd = 0
    .wFunc = FO_RENAME
    .pFrom = App.Path & "\ODLFILE.TXT" & vbNullChar & vbNullChar
    .pTo = App.Path & "\NEWFILE" & Curver & ".HI!" & vbNullChar & vbNullChar
    .lpszProgressTitle = "Please wait, renaming..."
    .fFlags = FOF_SIMPLEPROGRESS Or FOF_ALLOWUNDO
    End With
    
    lret = SHFileOP(fileop)
    
    If Module1.result <> 0 Then 'Operation failed
    MsgBox Err.LastDllError 'Show the error returned from the API.
    Else
    If fileop.fAnyOperationsAborted <> 0 Then
    MsgBox "Operation Failed"
    End If
    End If
    Hope that helps, it's kinda hassle, think you could use FSO but I have no experience with that, sorry!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Exclamation Cheers !

    Thanks for all the help but I have another two questions/issues

    1. What is FSO
    2. Surely there must be an easier way than all of that above!!! I will start wading through it slowly....

    Cheers
    Steve

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    FSO stands for (I assume) FileSystemObjects. Set a reference to the Microsoft Scripting Runtime library if you want to use it.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    To answer your second question. Here's an easier way (I don't know if it's faster but...)
    Code:
    Private Sub RenameFile(sPath As String)
        Dim sFile As String
        sPath = sPath & IIf(Right$(sPath, 1) <> "\", "\", "")
        sFile = Dir(sPath & "*.rep")
        Do While Len(sFile)
            Name sPath & sFile As sPath & Left$(sFile, InStrRev(sFile, ".")) & "txt"
            sFile = Dir
        Loop
    End Sub
    Good luck!

  6. #6
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Wink Use Name

    to rename files, just use the Name function:

    Code:
    Name "c:\File.xxx" As "c:\File.yyy"
    to use FSO:

    Code:
    Dim File
    Dim sDirectory
    Dim Msg
    'File, Msg and sDirectory are just variables, you can name them what you want
    
    Set Fso = CreateObject("Scripting.FileSystemObject")
    'make FileSystemObject
    Set sDirectory = Fso.getFolder("c:\temp")
    'set sDirectory to c:\temp
    For Each File In sDirectory.Files
    'loop for each file in sDirectory
        Msg = Msg & File & vbCr
    Next File
    'Next file
    MsgBox Msg
    'show all files
    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thumbs up You People are Really Helpful

    Thanks for all the help, I will try all of the methods and see which one works the best.

    Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width