Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename all files in a directory with .jpg extension

Status
Not open for further replies.

mtg0711

Technical User
Oct 6, 2009
5
US
I am writing a program to make it easy to gather pictures of sites such as Facebook by using my browser cache. This program will take all cache files (no extension) and add .jpg to all the files in the directory the user browses for.

I already have the part where the user chooses the directory figured out, but I am having problems with the code I would use for the next step. It will need to read all the files in the chosen directory and just add .jpg to the end (e.g. CACHE_001 -> CACHE_001.jpg)

I am using Microsoft Visual Basic Express Edition (info can be for version 2005 or 2008)
 
The short answer is look into GetFiles, this will allow you to loop through all files in the directory. You can then rename them. I can't think of an easy way to use GetFiles to filter only on files without an extension. You can get all files then use Path.GetExtension(foundFile) to evaluate if the found file has an extension.

Code:
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
    My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
    FileIO.SearchOption.SearchAllSubDirectories, "*.*")

If Path.GetExtension(foundFile).Equals("") Then
  'Rename Files Here
End If   

Next
 
I've never used the FileSystem.GetFiles, but this will work for System.IO.Directory.GetFiles.

Code:
        Dim strFiles As String() = Directory.GetFiles(SelectedDirectory, "*.", SearchOption.AllDirectories)
        If strFiles.Length > 0 Then
            For Each FileString As String In strFiles
                Dim fi As FileInfo = New FileInfo(FileString)
                fi.MoveTo(IO.Path.ChangeExtension(FileString, ".jpg"))
            Next
        End If

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen,
Code:
     Dim strFiles As String() = Directory.GetFiles(SelectedDirectory, "*.[b][COLOR=red]*[/color][/b]", SearchOption.AllDirectories)


Zameer Abdulla
 
No. If you put the second asterisk it will pull everything. "*." pulls only those without an extension.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I will try this, it is fine if it looks for all files, since all the files in the directory won't have an extension.
 
OK, two questions.

1) Kliot, what should I put in the 'Rename files here comment?
2) The line "If directory.GetExtension..." gives an error, 'GetExtension' is not a member of string.
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim directory As String
        directory = txtDirectory.Text
        ChDir(directory)

        For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
    My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
    FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            If directory.GetExtension(foundFile).Equals("") Then
'What should I put here??? 
            End If

        Next

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub
End Class
 

Sorry it's path.GetExtension

I would use Sorwen's suggestion of filtering files with "*.", this will select files without extension. I originally didn't think it would work but it seems to. This way you don't need to worry about getting the extension.

You have to use Move to rename a file, there are several ways to do this, I like.

File.Move("path/sourceFileName", "path/destinationFile")
 
Really if you wanted to be totally awesomely correct you would look at the header for the file and make sure it is a jpeg before you labeled it as such since browsers are usually mixed cache.

Code:
    Private Sub LabelAllJpegs(ByVal SelectedDirectory As String)
        Dim strFiles As String() = Directory.GetFiles(SelectedDirectory, "*.", SearchOption.AllDirectories)
        If strFiles.Length > 0 Then
            For Each FileString As String In strFiles
                Dim fi As FileInfo = New FileInfo(FileString)
                If IsJpeg(FileString) = True Then
                    fi.MoveTo(IO.Path.ChangeExtension(FileString, ".jpg"))
                End If
            Next
        End If
    End Sub

    Private Function IsJpeg(ByVal FullFilePath As String) As Boolean
        Dim strm As IO.Stream
        Dim br As IO.BinaryReader

        If File.Exists(FullFilePath) Then
            Dim jpgHeaderChk(1) As Byte
            strm = File.Open(FullFilePath, FileMode.Open, FileAccess.Read)
            br = New BinaryReader(strm)
            jpgHeaderChk = br.ReadBytes(2)
            strm.Close()
            br.Close()

            'JPEG SOI marker (FFD8 hex)
            If jpgHeaderChk(0).ToString("X"c) = "FF" And jpgHeaderChk(1).ToString("X"c) = "D8" Then
                Return True
            End If
        End If

        Return False
    End Function

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oops. While that will not cause any problems it is still better to close the byte reader first.

so
Code:
            jpgHeaderChk = br.ReadBytes(2)
            strm.Close()
            [red]br.Close()[/red]

is
Code:
            jpgHeaderChk = br.ReadBytes(2)
            [red]br.Close()[/red]
            strm.Close()


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top