CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    1

    2 Ways to Rename a file, please help!

    hi!

    Ok so I have about 12,000 files that I have in a list box (well many lsit boxes but if I find the right code for one it will work for them all)

    Anyways, here's what the program does. I have a folders contents being called into a list box. Some files have this annoying little mark at the end of them which is (U) and (U) [!]. I dont want these to be shown within the list box.

    Can I either
    1) Write a program that goes through each files and renames each file what it is minus (U) like If File has (U) delete (U)
    or
    2) Hide the (U) within the list box somehow?

    This is completely boggling my mind and google for that matter, please help!

  2. #2
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Re: 2 Ways to Rename a file, please help!

    u can use replace() replace the "(U)" or "(U) [!]" with " ", but u should check the character position first and see if the file contains that character at the end

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: 2 Ways to Rename a file, please help!

    2) Hide the (U) within the list box somehow?
    Presumably you are using a command like List1.Additem FileName, so why cant you check for the "U" or whatever, prior to adding it to the list ?



    If you are actually trying to get rid of the offending files then, for a one off solution to problems like yours, I normally turn to DOS

    ie, Start, Run, CMD, OK

    here you can do things like

    REN *U.* *.* (renames and file ending with U to same name, but dropping the U )

    or

    DEL *U.* (deletes any file ending with U)

    I am not sure exactly what you are trying to do, but DOS sounds like a quick fix to me

    (Make sure you have backups of all your files in case you mess them up !)
    Last edited by George1111; December 14th, 2008 at 06:50 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: 2 Ways to Rename a file, please help!

    Could you show a screenshot of those files with 'odd' characters?

    BTW, welcome to the forums!

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: 2 Ways to Rename a file, please help!

    Renaming the files and deleting parts of the names within your listbox only are two pairs of shoes.

    These [xxx] or (nnn) parts usually are there for some reason: if there is already a file named image.jpg and e.g. the IE wants to create a new file with the same name in the same (temporary) folder, it creates image[1].jpg

    In most cases you won't be able to rename image[1].jpg to image.jpg, because the filename would yhen be duplicate, so the renaming will be denied.

    Nevertheless you can certainly make a loop and go through the names of your listbox to remove unwanted name parts, but you must be aware, that this might produce duplicate names in the list, where an unambiguous relation to the original file is no longer given.

    Removal of character sequences can be done by several means. Regular expressions might come to mind, but also a conventional algorithm can be implemented.

    This little sample would remove all parts in square brackets from a name, be it [001] or [U] or [x01]:

    I simply put a listbox List and button Command1 on my form to show how it works.


    Code:
    Private Sub Command1_Click()
      Dim i%, n%, m%, a$, b$
      For i = 0 To List1.ListCount - 1
        Do
           n = InStr(List1.List(i), "[")
           If n Then
              m = InStr(n, List1.List(i), "]")
              If m Then
                 If n > 1 Then a$ = Left$(List1.List(i), n - 1) Else a$ = ""
                 If m < Len(List1.List(i)) Then b$ = Mid$(List1.List(i), m + 1) Else b$ = ""
                 List1.List(i) = a$ + b$
              End If
           End If
        Loop Until n = 0 Or m = 0
      Next
        
    End Sub
    
    Private Sub Form_Load()
      With List1
      .AddItem "Name[001].xxx"
      .AddItem "name[U].xxz"
      .AddItem "[234]name.dot"
      .AddItem "name[001]"
      .AddItem "name[1][2].end"
      .AddItem "[xxx]"
      
      End With
      
    End Sub
    Note that names like "[xxx]" or "[1][12]" will result in being deleted completely.

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

Featured