dcsimg
Results 1 to 10 of 10

Thread: Rename files in folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    6

    Rename files in folder

    Hi all!
    Please forgive me if this is not the correct room to ask this question. I am VERY new to VB and I searched for what I thought was the right room to ask this question. If not please direct me to the correct area. Thx!

    I am trying to rename about 1100 files in a folder with a vb script. The names of the files have 14 characters long plus the file extension of .jpg. I need to extract the 7th-11th characters (skip 12th) and include the 13th & 14th plus file extension. Example 064383010056_A.jpg needs to end up as 01005_A.jpg.

    I found a script to remove the leading 6 characters but I'm having trouble removing the 12th and putting the two together to get my desired result. Could someone help me out? I've been searching the web for the last 7 hours trying to fix this myself.

    Thank you for the help!!!

    Code:
    folderspec = "C:\Temp\Test\"
    strRename = ""
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    set f = fs.GetFolder(folderspec)
    
    for each f1 in f.files
    	Filename = folderspec & f1.name
    	NewFilename = replace(Filename, "064383", strRename)
    	fs.MoveFile Filename, NewFilename
    next
    
    msgbox "Done"

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,719

    Re: Rename files in folder

    You have definitely posted in the wrong place as that code is definitely not VB.NET. You say this:
    Quote Originally Posted by smorice View Post
    I am trying to rename about 1100 files in a folder with a vb script.
    Does that mean that you are actually using VBScript or are you actually using VB6 or VBA and you are just one of the many who incorrectly refer to code as script? We need to establish what language you are using so the mods can move this thread to the right place. VB.NET, VB6, VBA and VBScript all belong in different forums.

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,428

    Re: Rename files in folder

    if your using .Net here a start

    split the File name into elements or read about SubString
    here a Test..
    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim testString1 As String = "064383010056_A.jpg"
            ' Create an array containing "0", "6", "4" ...etc....
            Dim charArray() As Char = testString1.ToCharArray
            Debug.Print(testString1(0)) 'print the first element
            Debug.Print(testString1(17)) 'print the Last element
    
    
            'or use SubString, you have a StartIndex and then the Lenght
            Debug.Print(testString1.Substring(0, 5))
    
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,719

    Re: Rename files in folder

    Ah, I just realised that you said that you found that code on the web, so maybe you are using VB.NET and that code is basically useless to you as it isn't VB.NET.

    As with any problem, you should break it down into parts. The first part is to get all the files in the folder. Directory.GetFiles or DirectoryInfo.GetFiles can help you there. The next part is to get just the file name. The Path.GetFileName method or FileInfo.Name property can help you there. You can then use String.Substring as suggested to get the two parts of the file name that you need to retain and combine them. Finally, File.Move or File.MoveTo will do the renaming.

    The reason that beginners so often find it hard to find information is because they expect to find a turnkey solution for a multi-step process. The more steps in your process, the less likely you'll find all of them implemented in the same place. If you break your process down into those steps and research each one individually though, you're far more likely to find help for each of them. You then just need to combine the solutions into one. This is basically problem solving 101 - divide and conquer. It's not a technique specific to programming but it is critical to programming, so the sooner you apply it, the better.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    6

    Re: Rename files in folder

    jmcilhinney, I just realized that I was in the .Net forum I thought I posted this to the Visual Basic forum in general. I apologize for the mistake. I am so new I know just enough to be dangerous. I Googled VBScripts to rename files in a folder and that is how I started. From there I tried several things but due to the fact I'm trying to pull remove several leading characters and one embedded in the middle it makes it challenging for a noob...

    Thank you for your time! I will post to another forum.

    Very best,
    Scott

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    6

    Re: Rename files in folder

    ChrisE, I appreciate your work, but this is a regular VB script. I posted in the wrong forum by mistake. I thought I was in the general VB forum not the .Net. I apologize for the error and appreciate your time and assistance.

    Very best,
    Scott

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    6

    Re: Rename files in folder

    jmcilhinney , I posted in the wrong forum by mistake. I thought I was in the general VB forum not the .Net. I apologize for the error and appreciate your time and assistance. I will post my question to another forum.

    Very best,
    Scott

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,719

    Re: Rename files in folder

    Quote Originally Posted by smorice View Post
    jmcilhinney , I posted in the wrong forum by mistake. I thought I was in the general VB forum not the .Net. I apologize for the error and appreciate your time and assistance. I will post my question to another forum.

    Very best,
    Scott
    There is no "general VB forum". There's a VB section on the home page and it contains links to all the specialised VB forums, which includes VB.NET, VB6, VBA and VBScript. Now that you have created this thread, don't create a duplicate elsewhere. Ask the mods to move this thread. Click the Report Post icon under your first post and send a message requesting it. I have already done so once but they may not have been sure whether it needed to be moved or not, as Chris and I weren't.

  9. #9
    Lively Member
    Join Date
    Jan 2020
    Posts
    66

    Re: Rename files in folder

    try

    Option Explicit

    Dim fso, folder, file, tmFile
    Dim folderName

    folderName = "\\pcc\Int\PC\Inbox\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderName)

    For each file In folder.Files
    If instr(file, "TM") > 0 THEN
    file.name = replace(file.name, ".txt". "A.txt")
    End IF
    Next

  10. #10
    Lively Member
    Join Date
    Jan 2020
    Posts
    66

    Re: Rename files in folder

    try



    Option Explicit

    Dim fso, folder, file, tmFile
    Dim folderName

    folderName = "\\pcc\Int\PC\Inbox\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderName)

    For each file In folder.Files
    If instr(file, "TM") > 0 THEN
    file.name = replace(file.name, ".txt". "A.txt")
    End IF
    Next

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width