Results 1 to 4 of 4

Thread: [RESOLVED] Rename files with no extension to .txt

  1. #1

    Thread Starter
    Lively Member Tegan's Avatar
    Join Date
    Jun 2005
    Location
    Lincoln, NE
    Posts
    108

    Resolved [RESOLVED] Rename files with no extension to .txt

    Code:
     Dim Files As String() = IO.Directory.GetFiles(f.SelectedPath + "\", "*.*", IO.SearchOption.AllDirectories)
            Dim fil As String
            For Each fil In Files
                If fil.IndexOf(".") = -1 Then
                    FileSystem.Rename(fil, fil & ".txt")
                End If
            Next
    I'm trying to write some code to rename all the files in the directory and subdirectories. If the file has no extension it needs to add the extension .txt.

    Any help appreciated.
    Tegan Snyder

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Rename files with no extension to .txt

    What happens when you try to run your code?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Rename files with no extension to .txt

    The System.IO.Path.GetExtension function will get you the extension of a filename that you pass to it. That may be an easier way to check for an extension.

  4. #4

    Thread Starter
    Lively Member Tegan's Avatar
    Join Date
    Jun 2005
    Location
    Lincoln, NE
    Posts
    108

    Re: Rename files with no extension to .txt - RESOLVED

    Thanks I got it to work correctly now:

    Code:
    Imports System.IO
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                f.ShowDialog()
            Catch ex As Exception
            End Try
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim Files As String() = IO.Directory.GetFiles(f.SelectedPath + "\", "*.*", IO.SearchOption.AllDirectories)
            Dim fil As String
            For Each fil In Files
                If System.IO.Path.GetExtension(fil) = "" Then
                    FileSystem.Rename(fil, fil & ".txt")
                End If
            Next
        End Sub
    End Class
    Tegan Snyder

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