7 Replies - 1913 Views - Last Post: 01 January 2011 - 01:50 PM Rate Topic: -----

#1 wardy484  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-December 10

Batch File Renamer/Converter

Posted 30 December 2010 - 04:17 PM

I am trying to create a program that renames files to 1 - (however many files are in the folder) and also convert .pngs to .bmps, I had done this but I messed it up a little. I am not that experienced with VB.Net but I am looking to learn so any help would be great. I have tried this myself but as you can see in the code I've gone the wrong way about it. I'm clueless now to how to do this, any help would be appreciated


 If RadioButton1.Checked = True Then
            Dim files As String()
            files = IO.Directory.GetFiles(TextBox1.Text)
            Dim filepath_new As String

            filepath_new = TextBox1.Text

            For Each filepath As String In files
                For x = 0 To fileNo

                    My.Computer.FileSystem.RenameFile("*", x & ".bmp")
                    System.IO.File.Move(filepath, filepath_new)
                    x = x + 1
                Next x
            Next


Is This A Good Question/Topic? 0
  • +

Replies To: Batch File Renamer/Converter

#2 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 173
  • View blog
  • Posts: 1,094
  • Joined: 10-December 08

Re: Batch File Renamer/Converter

Posted 30 December 2010 - 04:38 PM

Well, firstly I'd use Dim Info As New IO.DirectoryInfo(TextBox1.Text), but then I'd use For Each fi In Info.GetFiles. It means that you can rename & move the files in one line of code, rather than lots. I'll post some code tomorrow, but I'm really tired atm, and need sleep.
Was This Post Helpful? 0
  • +
  • -

#3 wardy484  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-December 10

Re: Batch File Renamer/Converter

Posted 30 December 2010 - 04:44 PM

Okay great, I'll have a look at what you told me and have a play hopefully I can fix it. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 173
  • View blog
  • Posts: 1,094
  • Joined: 10-December 08

Re: Batch File Renamer/Converter

Posted 31 December 2010 - 10:43 AM

Okay. Firstly, I'll start off by saying that changing a file's attributes from .bmp to .png will not actually change its format. It'll just change the name. Now, This code will open a dialog to select a directory, and then change all the file names from that directory from .png to .jpg (put it inside a Button_Click event):
        Dim fbd As New FolderBrowserDialog
        fbd.ShowDialog()
        Dim Info As New IO.DirectoryInfo(fbd.SelectedPath)
        For Each fi In Info.GetFiles
            If fi.Name.EndsWith(".png") Then
                'The file is a png... we can rename it to a .jpg
                Dim NameWithoutExtension As String = fi.Name.Substring(0, fi.Name.Length - 4)
                Dim NewFileName As String = NameWithoutExtension & ".jpg"
                My.Computer.FileSystem.RenameFile(fi.FullName, NewFileName)
            End If
        Next


This post has been edited by Jack Eagles1: 31 December 2010 - 10:44 AM

Was This Post Helpful? 1
  • +
  • -

#5 wardy484  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-December 10

Re: Batch File Renamer/Converter

Posted 31 December 2010 - 12:08 PM

Right thanks I'll look into correctly reformating an image properly. Also the main problem I was having was trying to rename them. I wanted to name them 1 to (total number of files in folder) but I am clueless about how to do this. I think it would each item to be done seperate but I'm not sure how.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,689
  • Joined: 02-July 08

Re: Batch File Renamer/Converter

Posted 31 December 2010 - 12:38 PM

Now changing the extension on an image file - is that going to change the file's underlying format or should we make a new Bitmap and save it with the right imaging format.
' Get a Bitmap.
Dim bmp As Bitmap = picImage.Image

' Save the picture as a BMP, JPEG, and GIF.
bmp.Save(file_name & "bmp", _
   System.Drawing.Imaging.ImageFormat.Bmp)
bmp.Save(file_name & "jpg", _
   System.Drawing.Imaging.ImageFormat.Jpeg)
bmp.Save(file_name & "gif", _
   System.Drawing.Imaging.ImageFormat.Gif)


Was This Post Helpful? 2
  • +
  • -

#7 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 173
  • View blog
  • Posts: 1,094
  • Joined: 10-December 08

Re: Batch File Renamer/Converter

Posted 31 December 2010 - 04:53 PM

Okay. Edit of my original code to incorporate 's code:


        Dim fbd As New FolderBrowserDialog
        fbd.ShowDialog()
        Dim Info As New IO.DirectoryInfo(fbd.SelectedPath)
        For Each fi In Info.GetFiles
            If fi.Name.EndsWith(".png") Then
                'The file is a png... we can convert it to a .jpg
                Dim bm As Image = Image.FromFile(fi.FullName)
                Dim NameWithoutExtension As String = fi.Name.Substring(0, fi.Name.Length - 4)
                Dim NewFileName As String = NameWithoutExtension & ".jpg"
                bm.Save(fbd.SelectedPath & "\" & NewFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                'You may want to delete the files at some point... but you can't do it just after they've been accessed by your program.
                'My.Computer.FileSystem.DeleteFile(fi.FullName) will throw a 'File is being Used by another process' exception. I'm sure you'll be able to find an easy workaround.
            End If
        Next


Was This Post Helpful? 2
  • +
  • -

#8 wardy484  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-December 10

Re: Batch File Renamer/Converter

Posted 01 January 2011 - 01:50 PM

Thats brilliant guys, thank you both. I'm gonna have a good look through to code and try to implement it but thats looks like just what I needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1