VB Saving img file with path adds folder name to file name

Grey Walker

Hi I am having some problems with saving img files using Visual Basic, the files are being named wrongly with the folder name being added to the start of the file name.

I parse a web address and then use the split address values to rename my files however the the path value seems to be added to the file as well.

The files in the photo should be named for example "DCAT040iMBE Test13.jpg" but this file is being name "Test1DCAT040iMBE Test13.jpg"

enter image description here

Protected Sub GeneratedCode()
    Dim path As String = "C:\Users\Grey\Documents\visual studio 2010\Projects\QRCodeGenerator\QRCodeGenerator\Output\"
    LogoUpload.SaveAs(path + LogoUpload.FileName)
    TextFile.SaveAs(path + TextFile.FileName)
    Dim lines() As String = IO.File.ReadAllLines(path + TextFile.FileName)
    For Each line As String In lines

        Dim count As Integer
        Dim encoder As New QRCodeEncoder()
        encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H
        encoder.QRCodeScale = 10

        Dim img As Bitmap = encoder.Encode(line)
        Dim logo As System.Drawing.Image = System.Drawing.Image.FromFile(path + LogoUpload.FileName)
        Dim left As Integer = (img.Width / 2) - (logo.Width / 2)
        Dim top As Integer = (img.Height / 2) - (logo.Height / 2)
        Dim g As Graphics = Graphics.FromImage(img)

        Dim parseLine = line
        Dim replaceDelimiter As String
        If Not String.IsNullOrWhiteSpace(line) Then
            replaceDelimiter = Replace(line, "&", "=")
        End If

        Dim fileNameSplit() As String = replaceDelimiter.Split("=")
        Dim newFileName As String

        Dim partTwo = fileNameSplit(1)
        Dim partSix = fileNameSplit(5)

        Dim objFSO
        Dim newFolder As String
        newFolder = "C:\Users\Grey\Documents\visual studio 2010\Projects\QRCodeGenerator\QRCodeGenerator\Output\" + partSix
        objFSO = CreateObject("Scripting.FileSystemObject")
        If (Not System.IO.Directory.Exists(newFolder)) Then
            System.IO.Directory.CreateDirectory(newFolder)
        End If

        count += 1
        g.DrawImage(logo, New Point(left, top))
        newFileName = partTwo & " " & partSix & count & ".jpg"

        img.Save(newFolder + newFileName, ImageFormat.Jpeg)

        amountCreatedLbl.Text = count & " QRCodes Created"
        logo.Dispose()
    Next

End Sub

Could it be that I am generating my newFolder Values wrongly?

edited to add an example of data from the parsed txt file.

  https://mywebsite.com/QRCode/default.aspx?materialcode=DTAT050&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT055iMB&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT040iMBE&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAB060&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAT050&Logo=MyLogo&Companyloc=Test2
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT055iMB&Logo=MyLogo&Companyloc=Test2
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT040iMBE&Logo=MyLogo&Companyloc=Test2
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAB060&Logo=MyLogo&Companyloc=Test2

    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAT050&Logo=MyLogo&Companyloc=Test3
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT055iMB&Logo=MyLogo&Companyloc=Test3
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT040iMBE&Logo=MyLogo&Companyloc=Test3
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAB060&Logo=MyLogo&Companyloc=Test
RianBattle

Looks like you are missing a slash on the line:

img.Save(newFolder + newFileName, ImageFormat.Jpeg)

It should be:

img.Save(newFolder + "\" + newFileName, ImageFormat.Jpeg)

The program doesn't realize that the newDirectory variable is supposed to be a directory, it's just being concatenated to the filename directly. A better option would be to use:

img.Save(System.IO.Path.Combine(newFolder, newFileName), ImageFormat.Jpeg)

The System.IO.Path.Combine() function automatically adds the missing slash between the directory and filename, as well as some additional checks to make sure the result is valid.

As a side note, I would also recommend using & instead of + when joining strings together. Hard to debug issues can come up when you do it that way. I would also recommend turning Option Strict On, you will see a couple of other warnings that come up with your code as is. But, to resolve your issue, the above will work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

Saving image file name exactly as URL image file into folder in PHP

From Dev

NSFileManager how to return folder name of file path

From Dev

Check a file path belong to folder name

From Dev

get relative path with just file name and name of the parent folder

From Dev

Assign the name of the Folder to the name of a file

From Dev

Renaming File name in a folder

From Dev

image File is not saving into Server "Img" Folder

From Dev

Filter the URL path of images (img src) to obtain the file name

From Dev

R: plot saving and file name

From Dev

Path, directory and/or file name

From Dev

Splitting a path to a file name

From Java

PowerShell getting file name without path and number of rows of tsv in a folder

From Dev

How to get the name of the parent folder of a file specified by its full path?

From Dev

Get folder name from path in batch file. Not resolving

From Dev

How to write a function to combine folder path and file name?

From Dev

Windows batch file: get last folder name from path

From Dev

How to read file path where folder has square brackets in the name

From Dev

How to read file path where folder has square brackets in the name

From Dev

Keystroke shortcut for File name while saving file

From Dev

Saving a file to project folder and save its path

From Dev

Saving a file to project folder and save its path

From Dev

Move files to folder name contained in the file name

From Dev

PowerShell - Add grandparent folder name to file name

From Dev

Replace a folder name using a contained file name

From Java

Get folder name of the file in Python

From Dev

Rename file by its folder name

From Dev

Search and Replace File and Folder name

Related Related

  1. 1

    VB Getting The SubFolder name and saving it to a Text file

  2. 2

    VB Getting The SubFolder name and saving it to a Text file

  3. 3

    Saving image file name exactly as URL image file into folder in PHP

  4. 4

    NSFileManager how to return folder name of file path

  5. 5

    Check a file path belong to folder name

  6. 6

    get relative path with just file name and name of the parent folder

  7. 7

    Assign the name of the Folder to the name of a file

  8. 8

    Renaming File name in a folder

  9. 9

    image File is not saving into Server "Img" Folder

  10. 10

    Filter the URL path of images (img src) to obtain the file name

  11. 11

    R: plot saving and file name

  12. 12

    Path, directory and/or file name

  13. 13

    Splitting a path to a file name

  14. 14

    PowerShell getting file name without path and number of rows of tsv in a folder

  15. 15

    How to get the name of the parent folder of a file specified by its full path?

  16. 16

    Get folder name from path in batch file. Not resolving

  17. 17

    How to write a function to combine folder path and file name?

  18. 18

    Windows batch file: get last folder name from path

  19. 19

    How to read file path where folder has square brackets in the name

  20. 20

    How to read file path where folder has square brackets in the name

  21. 21

    Keystroke shortcut for File name while saving file

  22. 22

    Saving a file to project folder and save its path

  23. 23

    Saving a file to project folder and save its path

  24. 24

    Move files to folder name contained in the file name

  25. 25

    PowerShell - Add grandparent folder name to file name

  26. 26

    Replace a folder name using a contained file name

  27. 27

    Get folder name of the file in Python

  28. 28

    Rename file by its folder name

  29. 29

    Search and Replace File and Folder name

HotTag

Archive