Results 1 to 4 of 4

Thread: Renaming Files

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 1999
    Posts
    5

    Post

    I need to copy files in a source directory (i.e. 1.tif, 2.tif, etc.) and rename them to a destination directory (i.e. 199912215001.001, 199912215001.002, etc). Think I have to use the CopyFile method but I'm chasing my tale can anyone help?

    Thanks

  2. #2
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Post

    Hi,
    Try :
    FileCopy "Source File", "Destination File"
    Al.

  3. #3
    Member
    Join Date
    Jul 1999
    Location
    J-ville, NC
    Posts
    54

    Post

    Try doing this:

    OldFile = "C:\Location\1.tiff"
    NewFile = "C:\Location\199912215001.001.tiff"

    Name OldFile As NewFile

    ------------------
    David Underwood
    Teen Programmer

    ICQ - 14028049
    E-mail - [email protected]



  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Post

    'if you are copying lots and lots of files try this out
    'I use this to back up folders so I haven't tried to
    'rename anything doing this...you can try that.
    'look for the =================to see where you should try it.
    'Wayne


    'bas module code for copying a file from
    'one directory to another
    'in this case from c:\yourFolder to a:\yourFolder
    'CAUTION....WILL OVERWRITE EXISTING FILES WITHOUT PROMPT

    Option Explicit
    '
    Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" _
    (ByVal lpPathName As String, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

    Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
    (ByVal lpExistingFileName As String, _
    ByVal lpNewFileName As String, _
    ByVal bFailIfExists As Long) As Long

    Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long

    Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    (ByVal hFindFile As Long, _
    lpFindFileData As WIN32_FIND_DATA) As Long

    Declare Function FindClose Lib "kernel32" _
    (ByVal hFindFile As Long) As Long
    '
    Public Const INVALID_HANDLE_VALUE = -1
    Public Const MAX_PATH = 260

    Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
    End Type

    Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
    End Type

    Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type

    Public Function rgbCopyFiles(sSourcePath As String, _
    sDestination As String, _
    sFiles As String) As Long

    Dim WFD As WIN32_FIND_DATA
    Dim SA As SECURITY_ATTRIBUTES

    Dim r As Long
    Dim hFile As Long
    Dim bNext As Long
    Dim copied As Long
    Dim currFile As String

    'Create the target directory if it doesn't exist
    Call CreateDirectory(sDestination, SA)

    'Start searching for files in the Target directory.
    hFile = FindFirstFile(sSourcePath & sFiles, WFD)

    If (hFile = INVALID_HANDLE_VALUE) Then

    'nothing to do, so bail out
    MsgBox "No " & sFiles & " files found."
    Exit Function

    End If

    'Copy each file to the new directory
    If hFile Then

    Do

    'trim trailing nulls, leaving one to terminate the string
    currFile = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr$(0)))

    '==========================================================================
    'haven't tried this part but I think it should work
    currFile = Name currFile As "c:\YourOldFolder\YourNewFolder\"
    '==========================================================================

    'copy the file to the destination directory & increment the count
    Call CopyFile(sSourcePath & currFile, sDestination & currFile, False)
    copied = copied + 1

    'find the next file matching the initial file spec
    bNext = FindNextFile(hFile, WFD)

    Loop Until bNext = 0

    End If

    'Close the search handle
    Call FindClose(hFile)

    'and return the number of files copied
    rgbCopyFiles = copied

    End Function

    '>>>> CODE FOR FORM <<<<

    Dim sSourcePath As String
    Dim sDestination As String
    Dim sFiles As String
    Dim numCopied As Long

    'set the appropriate initializing values
    sSourcePath = "c:\YourOLdFolder\"
    sDestination = "c:\YourNewFolder\"
    sFiles = "*.*"

    'perform the copy and return the copied file count; end application
    numCopied = rgbCopyFiles(sSourcePath, sDestination, sFiles)

    MsgBox numCopied & " files copied to " & sDestination


    Unload Me

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