Results 1 to 19 of 19

Thread: ChangingFileExtension

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Smile ChangingFileExtension

    Hello :

    I have appr 15000 files on my computer written with the DOS-editor but
    without a file extension.

    My goal is to change all file extensions to .txt . How can I do this ?
    I have a code which changes file extension from .txt to .doc or
    .doc to .dat but it does not work without a file extension.

    Thanks

    Robert

  2. #2
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: ChangingFileExtension

    Actually this could be very easy...

    1) Put a file1 list box on form.

    2) Change property Pattern to say " *. " - this will only allow files with no extension to display in list.

    3) Add a timer1 and text1 to form. Set timer interval to 3000. Set text to "0"

    4) Add your renaming code (something like this)

    VB Code:
    1. Private Sub Timer1()
    2.  
    3. File1.ListIndex = CLng(Text1.Text)
    4. Name "C:\Test\" & File1.Filename As "C:\Test\" & File1.Filename & ".txt"
    5. Text1.Text = CDbl(Text1.Text) + 1
    6.  
    7. End Sub

    What is happening is that each time the timer fires (3 sec) it looks for the number in the list box and uses it as the index number for the file1 list box.
    You just rename the filename with your rename code.

    The reason I would do it this way is because you can do this in a "For..Next" loop (its to fast though). But I would give windows enough time to process the name change.

    Try that
    Last edited by epixelman; Dec 24th, 2004 at 07:16 PM.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: ChangingFileExtension

    Move all of your files that have no extentions into one easy to access folder and use the following sample:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim sFile$, sPath$
    3.  
    4.     sPath = "c:\temp\Test\"
    5.     sFile = Dir(sPath, vbNormal)
    6.     Do While sFile <> ""
    7.        If sFile <> "." And sFile <> ".." Then
    8.           Name sPath & sFile As sPath & sFile & ".txt"
    9.        End If
    10.        sFile = Dir
    11.     Loop
    12.  
    13. End Sub

  4. #4
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: ChangingFileExtension

    Cool.. RhinoBull

    That will work too!!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: ChangingFileExtension

    Thanks for your reply. Unfortunately I got all kind of error messages
    when I tried it out. ( Type missmatch etc etc )

    However one file changed to what I wanted but somehow the "loop"
    causes problems.

    What I forgot : I am a beginner in VB6.

    Thanks

    RobertXYZ




    Quote Originally Posted by epixelman
    Actually this could be very easy...

    1) Put a file1 list box on form.

    2) Change property Pattern to say " *. " - this will only allow files with no extension to display in list.

    3) Add a timer1 and text1 to form. Set timer interval to 3000. Set text to "0"

    4) Add your renaming code (something like this)

    VB Code:
    1. Private Sub Timer1()
    2.  
    3. File1.ListIndex = CLng(Text1.Text)
    4. Name "C:\Test\" & File1.Filename As "C:\Test\" & File1.Filename & ".txt"
    5. Text1.Text = CDbl(Text1.Text) + 1
    6.  
    7. End Sub

    What is happening is that each time the timer fires (3 sec) it looks for the number in the list box and uses it as the index number for the file1 list box.
    You just rename the filename with your rename code.

    The reason I would do it this way is because you can do this in a "For..Next" loop (its to fast though). But I would give windows enough time to process the name change.

    Try that

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: ChangingFileExtension

    Just follow my intructions and you'll be ok.

  7. #7
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: ChangingFileExtension

    What I gave you was only a basic sample.

    Here is what I have that works... (be sure to put your "no extention files in the same folder as your program)

    Type mismatch?? The only place that I see where you would be getting that is if you have "0" in the textbox instead of 0 (do not use quotes)

    VB Code:
    1. Private Sub Command1_Click()
    2. 'set speed and turn on timer
    3. Timer1.Interval = 500
    4. Timer1.Enabled = True
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. 'select the file based on index number in textbox
    9. File1.ListIndex = CLng(Text1.Text)
    10. 'rename file
    11. Name App.Path & "\" & File1.FileName As App.Path & "\" & File1.FileName & ".txt"
    12. 'up the text box count
    13. Text1.Text = CDbl(Text1.Text) + 1
    14.  
    15. 'check and see if we have reached end of list
    16. If Text1.Text = File1.ListCount Then
    17.     'trun timer off
    18.     Timer1.Enabled = False
    19.     'tell user we are done
    20.     MsgBox "Done", 48, "Complete"
    21.     'refresh file box to show new filenames
    22.     File1.Pattern = "*.txt"
    23.     File1.Refresh
    24. End If
    25. End Sub
    That works for me. Checking the end of the list should solve the loop error.
    Did you try RhinoBulls? His works too!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: ChangingFileExtension

    Quote Originally Posted by epixelman
    What I gave you was only a basic sample.

    Here is what I have that works... (be sure to put your "no extention files in the same folder as your program)

    Type mismatch?? The only place that I see where you would be getting that is if you have "0" in the textbox instead of 0 (do not use quotes)

    VB Code:
    1. Private Sub Command1_Click()
    2. 'set speed and turn on timer
    3. Timer1.Interval = 500
    4. Timer1.Enabled = True
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. 'select the file based on index number in textbox
    9. File1.ListIndex = CLng(Text1.Text)
    10. 'rename file
    11. Name App.Path & "\" & File1.FileName As App.Path & "\" & File1.FileName & ".txt"
    12. 'up the text box count
    13. Text1.Text = CDbl(Text1.Text) + 1
    14.  
    15. 'check and see if we have reached end of list
    16. If Text1.Text = File1.ListCount Then
    17.     'trun timer off
    18.     Timer1.Enabled = False
    19.     'tell user we are done
    20.     MsgBox "Done", 48, "Complete"
    21.     'refresh file box to show new filenames
    22.     File1.Pattern = "*.txt"
    23.     File1.Refresh
    24. End If
    25. End Sub
    That works for me. Checking the end of the list should solve the loop error.
    Did you try RhinoBulls? His works too!

    Thanks for your efforts but I must still be missing something because
    when I click on Command1 I obtain an error message ( beside the line
    File1.ListIndex = CLng(Text1.Text) ---> Type mismatch

    To recap :
    My program is in c:\Folder1 and all my files with no extensions are in
    c:\Folder1 as well.
    I typed the code as you proposed.
    On Form-Load I entered Text1.Text = ""
    What do I do wrong ?

    Thanks

    RobertXYZ

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: ChangingFileExtension

    Just out of curiousity: did you RobertXYZ at least try to run that sample I've posted for YOU ? I am not saying that you must use mine but if something isn't working out then you need to try anothoer approach, isn't it?

    Cheers.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: ChangingFileExtension

    Hi RhinoBull

    Yes,I tried you suggested code but must miss something as it did not work.
    Still trying.Will keep you posted.
    In the meantime thanks.

    RobertXYZ



    Quote Originally Posted by RhinoBull
    Just out of curiousity: did you RobertXYZ at least try to run that sample I've posted for YOU ? I am not saying that you must use mine but if something isn't working out then you need to try anothoer approach, isn't it?

    Cheers.

  11. #11
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: ChangingFileExtension

    Robert...

    I set my text1 text in the properties panel using ... 0

    If you are setting it using code then....

    Text1.Text = "0"

    The type mismatch could be because the text box is blank. It has to have a value in it.

    Try that

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: ChangingFileExtension

    Which part didn't work ??????????????
    All you had to do was only set proper Path to your files and that's it.

    sPath = "c:\Folder1\" if that's the correct path to all of your files.

    Although, no dirlistboxes, filelistboxes, etc are needed except for a single command button.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: ChangingFileExtension

    Attn: RhinoBull

    My mistake,it works now. I typed in sPath "c:\Folder1" instead of
    "c:\Folder1\"

    Your help is appreciated.Thanks again

    RobertXYZ


    Quote Originally Posted by RhinoBull
    Which part didn't work ??????????????
    All you had to do was only set proper Path to your files and that's it.

    sPath = "c:\Folder1\" if that's the correct path to all of your files.

    Although, no dirlistboxes, filelistboxes, etc are needed except for a single command button.

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: ChangingFileExtension

    Not at all. Glad you did sort it out.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    84

    Re: ChangingFileExtension

    Hi :
    I ran your code and it works ok.Thanks a lot for your help.What
    amazes me is that the problem can be solved with different codes because
    the one from Rhino works as well.
    As a complete beginner in VB6 I tought any particular problem can only
    have one solution...
    Where do you learn these things by the way.From books ? Experience ?
    I am sure I could have searched for ever and would not find the code.

    Thanks

    RobertXYZ




    Quote Originally Posted by epixelman
    What I gave you was only a basic sample.

    Here is what I have that works... (be sure to put your "no extention files in the same folder as your program)

    Type mismatch?? The only place that I see where you would be getting that is if you have "0" in the textbox instead of 0 (do not use quotes)

    VB Code:
    1. Private Sub Command1_Click()
    2. 'set speed and turn on timer
    3. Timer1.Interval = 500
    4. Timer1.Enabled = True
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. 'select the file based on index number in textbox
    9. File1.ListIndex = CLng(Text1.Text)
    10. 'rename file
    11. Name App.Path & "\" & File1.FileName As App.Path & "\" & File1.FileName & ".txt"
    12. 'up the text box count
    13. Text1.Text = CDbl(Text1.Text) + 1
    14.  
    15. 'check and see if we have reached end of list
    16. If Text1.Text = File1.ListCount Then
    17.     'trun timer off
    18.     Timer1.Enabled = False
    19.     'tell user we are done
    20.     MsgBox "Done", 48, "Complete"
    21.     'refresh file box to show new filenames
    22.     File1.Pattern = "*.txt"
    23.     File1.Refresh
    24. End If
    25. End Sub
    That works for me. Checking the end of the list should solve the loop error.
    Did you try RhinoBulls? His works too!

  16. #16
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: ChangingFileExtension

    LOL ...
    Books ? Yes, they are very helpfull, no doubt about that. But mostly it's all about every day work, practice, every day work, practice, every day work, practice for past few dozen of years ... That's about it ...

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ChangingFileExtension

    there are un-limited solutions to any given problem. some are easier to implement than others, while some are easier to read (and maintain) then others. any one may be used, as long as it gets the job done.

    while it is good to learn proper technique, you could just as easily develop your own style of code, and do quite well.

    the problem comes in when others have to maintain your code. if they are familar with the technique that you used, then they will be able to maintain it a lot easier than if they're not.

    we use good techniques, and constantly pick up better techniques from the code that we see. if you want to see excellent examples, visit the code bank. these programs shine in their ability to solve any given problem.

  18. #18
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Re: ChangingFileExtension

    Many solutions is part of the game.

    The reason I suggested my solution is because I like to see the "solution" work before my eyes as it is happening. The solution I posted here, I have in a much larger scale where you can customize the extensions visually.

    I like RhinoBull's solution (which is quick and to the point) too because that code can be used in the background with out the user knowing it.

    It all depends on how you want to present your function to the user and more importantly how clean, manageable and easy to custmize a particular solution can be handled

    As they say... "all roads lead to Rome"... but some may have more curves than the other.

    Happy Learning!!


  19. #19
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390
    this sounds like a one off thing - if this is the case u could always run this from the command prompt :

    rename *. *.txt


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