Consulting

Results 1 to 8 of 8

Thread: Help with renaming part of file

  1. #1

    Help with renaming part of file

    All,

    Good afternoon. I have hundreds of pdf files with "Model (1)" as part of filename that I would like stripped off, i.e. Y-45E-0372 Model (1).pdf I have tried following code but to no avail (Error: Expected end of statement). Would someone be so kind to take a look at code and see what is wrong? I sure would appreciate it.

    Dim strFolder As String
    Dim strFile As String

    strFolder = "C:\Dave"
    strFile = Dir(strFolder & "\*.*")
    Do While Len(strFile) > 0
    If InStr(strFile, "Model (1)") > 0 Then
    Name strFolder & strFile As strFolder & Replace(strFile, "Model (1), "")
    End If
    strFile = Dir()
    Loop

    Thank you,
    David

  2. #2
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    233
    Location
    Name strFolder & strFile As strFolder & Replace(strFile, "Model (1)", "")
    Artik

  3. #3
    Artik,

    Thank you for taking time out to look at this but I am still getting the Expected end of statement Error.

    David

  4. #4
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    598
    Location
    Hi David. If U want U could just manipulate the file string name. Maybe...
    Name strFolder & strFile As strFolder & "" &  LEFT(strFile,LEN(strFile)-14) & ".pdf"
    Dave

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,079
    Location
    Dim strFolder As String
    Dim strFile As String
    
        strFolder = "C:\Dave\"
        strFile = Dir(strFolder & "\*.*")
        Do While Len(strFile) > 0
        
            If InStr(strFile, "Model (1)") > 0 Then
            
                Name strFolder & strFile As strFolder & Replace(strFile, " Model (1)", "")
            End If
    
            strFile = Dir()
        Loop
    ____________________________________________
    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

  6. #6
    Good morning all,

    I appreciate everyone's input so far but I still can't get this to work. If someone would be so kind to create a folder called Dave in C: and and copy attached .pdf files in zip to Dave directory and use Rename.bak in zip file (change ext. to vbs) or you can view Rename.bak code using notepad. I am not sure if I should be posting the files or not. If I shouldn't, I apologize. I am also attaching screen shot of error.

    Thank you much,
    David
    Attached Images Attached Images
    Attached Files Attached Files

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,079
    Location
    What exactly is not working? I tried it and it seemed to work fine.
    ____________________________________________
    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

  8. #8
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,597
    Location
    Are you using VBA or Windows Scripting Host?

    It looks like you're using WSH (the .VBS), but the statements are VBA so I'm not surprised it isn't working

    You were also missing some path seperators

    If you want to use WSH, then use the FileSystemObject and the .Files collection

    Some thing like this

    
    Dim strFolder
    Dim strFile
    dim oFSO, oFiles, oFile
    
        strFolder = "C:\Users\Daddy\Downloads\test\"
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
    
        For Each oFile in oFSO.GetFolder(strFolder).Files
            strFile = oFile.Name
            If InStr(strFile, "Model (1)") > 0 Then
                oFSO.MoveFile strFolder & strFile, strFolder & Replace(strFile, " Model (1)", "")
            End If
        Next
        
    Last edited by Paul_Hossler; 06-08-2019 at 07:50 PM.
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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