Consulting

Results 1 to 6 of 6

Thread: FileName

  1. #1

    FileName

    Hi everyone I could do with some urgent help, I have a piece of code that changes tif & pdf file names to example from 47892 to 4789200001-CR- N but I need it to erase the number so it lends up like 000001-CR-N and the next file 000002-CR-N and so on. If some body could help that would be great
    Here is the code.

    [VBA]Option Explicit
    Function MyCheck(TheFileName) As Boolean
    If InStr(TheFileName, "-CR-") > 0 And InStr(TheFileName, "-N.") > 0 Then MyCheck = True
    End Function
    Sub ChangeFilename()
    Dim strfile As String, fileext As String, filepath As String, filenum As String, n As Integer 'My variables
    filepath = BrowseForFolder("R:\DFS\Strategy and Compliance\FINEST\Projects\Imaging Trial\Documents\Creditors\") & "\"
    strfile = Dir(filepath)
    Do While strfile <> ""
    Debug.Print strfile
    'Check that filename is Isnumeric
    If Not MyCheck(strfile) Then
    If Right$(strfile, 3) = "tif" Or Right$(strfile, 3) = "pdf" Then
    filenum = Left(strfile, Len(strfile) - 4)
    fileext = Right(strfile, 3)
    'End of each file name with - Cr -N
    'Name filepath & strfile As filepath & filenum & Format(n, "1") + 1 & "." & fileext
    Name filepath & strfile As filepath & filenum & Format(n, "000001") & "-CR-" & Format(Now(), "YYMMDD") & "-N" & "." & fileext
    End If
    End If
    strfile = Dir
    Loop
    End Sub[/VBA]
    Last edited by Bob Phillips; 10-09-2012 at 12:56 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    [VBA]Option Explicit

    Function MyCheck(TheFileName) As Boolean
    If InStr(TheFileName, "-CR-") > 0 And InStr(TheFileName, "-N.") > 0 Then MyCheck = True
    End Function

    Sub ChangeFilename()
    Dim strfile As String, fileext As String
    Dim filepath As String, filenum As String, n As Long 'My variables

    filepath = BrowseForFolder("R:\DFS\Strategy and Compliance\FINEST\Projects\Imaging Trial\Documents\Creditors\") & "\"
    strfile = Dir(filepath)
    Do While strfile <> ""

    Debug.Print strfile
    'Check that filename is Isnumeric
    If Not MyCheck(strfile) Then

    If Right$(strfile, 3) = "tif" Or Right$(strfile, 3) = "pdf" Then

    filenum = Left(strfile, Len(strfile) - 4)
    fileext = Right(strfile, 3)
    'End of each file name with - Cr -N
    'Name filepath & strfile As filepath & filenum & Format(n, "1") + 1 & "." & fileext
    n = n + 1
    Name filepath & strfile As filepath & filenum & Format(n, "000000") & "-CR-" & Format(Now(), "YYMMDD") & "-N" & "." & fileext
    End If
    End If

    strfile = Dir
    Loop
    End Sub[/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Still not coming out quite right.
    Out come
    47892000011-CR- N.tif and 267632000021-CR-121009-N.tif
    But I need
    I need 000001-CR-N.tif and 000002-CR-N.tif

  4. #4
    Correction the out come is 47892000001- CR-N.tif and 267632000002-CR-N.tif but I need I need 000001-CR-N.tif and 000002-CR-N.tif

  5. #5
    Needs to erase the original file name

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    I saw you say that, but your code said otherwise

    [VBA]Sub ChangeFilename()
    Dim strfile As String, fileext As String
    Dim filepath As String, filenum As String, n As Long 'My variables

    filepath = BrowseForFolder("R:\DFS\Strategy and Compliance\FINEST\Projects\Imaging Trial\Documents\Creditors\") & "\"
    strfile = Dir(filepath)
    Do While strfile <> ""

    Debug.Print strfile
    'Check that filename is Isnumeric
    If Not MyCheck(strfile) Then

    If Right$(strfile, 3) = "tif" Or Right$(strfile, 3) = "pdf" Then

    filenum = Left(strfile, Len(strfile) - 4)
    fileext = Right(strfile, 3)
    'End of each file name with - Cr -N
    'Name filepath & strfile As filepath & filenum & Format(n, "1") + 1 & "." & fileext
    n = n + 1
    Name filepath & strfile As filepath & Format(n, "000000") & "-CR-" & Format(Now(), "YYMMDD") & "-N" & "." & fileext
    End If
    End If

    strfile = Dir
    Loop
    End Sub[/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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