Results 1 to 13 of 13

Thread: Rename multiple files in one directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    13

    Rename multiple files in one directory

    Is it possible to rename all the files in a folder with a simple program using vb.NET

    Lets say there is a folder containing the files, files are vary every time loaded:

    A.258551.txt
    b_466464.txt
    c-548754354.txt

    Will it be possible to rename these files in one operation,as below:

    A.txt
    B.txt
    C.txt

    Any code please

  2. #2
    Lively Member
    Join Date
    Jun 2018
    Location
    Krähental
    Posts
    64

    Re: Rename multiple files in one directory

    I can't think of a single command that would do what you want.

    But it's trivial to read the directory (all the file names in the directory) into an array using the GetFiles() function

    Directory.GetFiles(@"c:\MyDir", "*.txt");

    Then to iterate through that list renaming each file individually (I assume your intention is) based on the first character. Doing whatever you need to do in the event of a duplicate being created.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    13

    Re: Rename multiple files in one directory

    Quote Originally Posted by Axcontrols2 View Post
    I can't think of a single command that would do what you want.

    But it's trivial to read the directory (all the file names in the directory) into an array using the GetFiles() function

    Directory.GetFiles(@"c:\MyDir", "*.txt");

    Then to iterate through that list renaming each file individually (I assume your intention is) based on the first character. Doing whatever you need to do in the event of a duplicate being created.
    Thanks for your reply

    There is no issue if muliple operation should be run just tell me in details what should I do to handle this operation for each file please

    Waiting your code

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    13

    Re: Rename multiple files in one directory

    There is no issue how much operation should be run for this, please help me in details with operation that can handle this for each file please
    Waiting your code

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    13

    Re: Rename multiple files in one directory

    There is no issue how much operation should be run for this, please help me in details with operation that can handle this for each file please
    Waiting your code

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

    Re: Rename multiple files in one directory

    Hi

    it would work with Powershell in 2 Stages (perhaps just 1)
    is Powershell a option for you ?
    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    13

    Re: Rename multiple files in one directory

    I don't think so I prefer a code rather than go with power shell option

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

    Re: Rename multiple files in one directory

    Hi,

    well you could use Substring

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim s1 As String = "A.258551.txt"
            MessageBox.Show(s1.Substring(0, 1) & ".txt")
        End Sub
    that would result in A.txt

    but what happens after Z ?
    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    13

    Re: Rename multiple files in one directory

    I don't think that this is useful file name changed every time and it not fixed I need a code which take the first character as condition in each case and ignore other characters

  10. #10
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Rename multiple files in one directory

    If you only want the first character then https://docs.microsoft.com/en-us/dot...tframework-4.8 is probably what you need.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Rename multiple files in one directory

    Quote Originally Posted by comeps View Post
    I don't think that this is useful file name changed every time and it not fixed I need a code which take the first character as condition in each case and ignore other characters
    The code was an example of how to use the SubString function, it wasn't code to solve your issue.
    This forum doesn't usually have people write the code that you want.
    The forum is for helping programmers fix issues with code they have written, or guide someone about possible ways to solve an issue.

    Sometimes, someone may have the desire to write the code out of interest or boredom, but I wouldn't count on it. You may be able to eventually get enough code examples from various people to put it together through persistently asking more pointed questions about particular parts of the problem, but you're taking your chances.

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

    Re: Rename multiple files in one directory

    Quote Originally Posted by comeps View Post
    I don't think that this is useful file name changed every time and it not fixed I need a code which take the first character as condition in each case and ignore other characters
    well it was an example, you would/could use that 'SubString' Information in a ...For Each.. Loop to rename the Files

    in Post#10 you got a Link to read on the SubString method
    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.

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

    Re: Rename multiple files in one directory

    Quote Originally Posted by comeps View Post
    Thanks for your reply

    There is no issue if muliple operation should be run just tell me in details what should I do to handle this operation for each file please

    Waiting your code
    the demand 'Waiting your code' isn't appropriate
    try first , or wait for AxControls2 to respond
    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.

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