none
Illegal Characters in Path

    Question

  • I am using the following code to rename a file, the filnames are from a listbox where the data source is from the user. But while renaming an error pops up saying illegal characters is path

        Dim loc_path = "D:\Test"
        Dim filenames() As String = Directory.GetFiles(loc_path, "*.mp3")
         
         
        For i = 0 To ListBox1.Items.Count - 1
        filepath = Path.Combine(loc_path, ListBox1.Items(i).ToString)
        filename = Path.GetFileName(filenames(i))
         
         temp = lst_names.items(i).tostring
         
         
        fname = temp
             
         
        My.Computer.FileSystem.RenameFile(filepath, fname) '// This is where the error pops up
        Next
    

    I get ;

    System.ArgumentException: Illegal characters in path.
       at System.IO.Path.CheckInvalidPathChars(String path)
       at System.IO.Path.Combine(String path1, String path2)
       at Microsoft.VisualBasic.FileIO.FileSystem.GetFullPathFromNewName(String Path, String NewName, String ArgumentName)
       at Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(String file, String newName)
       at Microsoft.VisualBasic.MyServices.FileSystemProxy.RenameFile(String file, String newName)
       at WindowsApplication1.Form2.Button4_Click(Object sender, EventArgs e) in D:\PRaser\PRaser\Form2.vb:line 158

    Ive found out that the reason for this error is due to the spaces in the filename string (that would be temp variable which holds names like "Song Name Number One" like that)

    how can i bypass this issue?

     

    thnx in advnc

    Thursday, September 29, 2011 12:47 PM

Answers

  • raaif,

    You might have to create a little routine (you can make it a method) to replace those characters.  Try this:

    Dim loc_path = "D:\Test"
        Dim filenames() As String = Directory.GetFiles(loc_path, "*.mp3")
         Dim stPath As String
    Dim CharPos As Integer
    Dim stChars() As Char = System.IO.Path.GetInvalidPathChars
         
        For i = 0 To ListBox1.Items.Count - 1
        filepath = Path.Combine(loc_path, ListBox1.Items(i).ToString)
        filename = Path.GetFileName(filenames(i))
         
         temp = lst_names.items(i).tostring
         
         
        fname = temp
    
        stPath = fname
    
        CharPos = 0
    
        For Each achr As Char In stChars
    
            My.Application.DoEvents()
    
            CharPos = InStr(stPath, achr)
    
           If CharPos > 0 Then
    
                fname= Replace(fname, achr.ToString, "")
    
           End If
    
        Next
         
       My.Computer.FileSystem.RenameFile(filepath, fname) 
    Next
    • Edited by Tom_Overton Thursday, September 29, 2011 1:17 PM
    • Marked as answer by raaif Thursday, September 29, 2011 2:31 PM
    Thursday, September 29, 2011 1:11 PM

All replies

  • Not so strange you try to set the filename to the filepath without a name to the filename.

     http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.filesystem.renamefile(v=VS.100).aspx

    I never use that one I use the IO File, the FileIO is created for those who were using VB6 and did not understand .Net

    I never use that one, I do the File.Move which does the same, but you have twice to give the full path name.

    http://msdn.microsoft.com/en-US/library/system.io.file.move(v=VS.100).aspx


    Success
    Cor
    Thursday, September 29, 2011 1:04 PM
  • Hi

    Spaces shouldn't cause such a problem. There may be other invalid characters in the string. The first thing to do (in my opinion) is to set a breakpoint at the error line and check what the variable values are. Check both variables and check too if the file actually exists to be renamed.

    Second thing I would do is to set a Try.... Catch block around the error line to message box the errors (in the case you have some that do give an error and some that don't)


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Thursday, September 29, 2011 1:07 PM
  • raaif,

    You might have to create a little routine (you can make it a method) to replace those characters.  Try this:

    Dim loc_path = "D:\Test"
        Dim filenames() As String = Directory.GetFiles(loc_path, "*.mp3")
         Dim stPath As String
    Dim CharPos As Integer
    Dim stChars() As Char = System.IO.Path.GetInvalidPathChars
         
        For i = 0 To ListBox1.Items.Count - 1
        filepath = Path.Combine(loc_path, ListBox1.Items(i).ToString)
        filename = Path.GetFileName(filenames(i))
         
         temp = lst_names.items(i).tostring
         
         
        fname = temp
    
        stPath = fname
    
        CharPos = 0
    
        For Each achr As Char In stChars
    
            My.Application.DoEvents()
    
            CharPos = InStr(stPath, achr)
    
           If CharPos > 0 Then
    
                fname= Replace(fname, achr.ToString, "")
    
           End If
    
        Next
         
       My.Computer.FileSystem.RenameFile(filepath, fname) 
    Next
    • Edited by Tom_Overton Thursday, September 29, 2011 1:17 PM
    • Marked as answer by raaif Thursday, September 29, 2011 2:31 PM
    Thursday, September 29, 2011 1:11 PM
  • Use the Try...Catch method suggested by leshay to identify the values of filepath and fname. If you are still not sure why the path is invalid after looking at it then post a follow-up.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, September 29, 2011 1:15 PM
  • Thanks alot Tom Overton!! I dont know how invalid path characters came in there but it worked :D 

    And by the way for future rference can u pls spot what are the invalid path characters this is the filepath data;

    D:\Test\5prodigy1(songs.pk).mp3
    D:\Test\5prodigy10(songs.pk).mp3
    D:\Test\5prodigy11(songs.pk).mp3
    D:\Test\5prodigy12(songs.pk).mp3
    D:\Test\5prodigy13(songs.pk).mp3
    D:\Test\5prodigy14(songs.pk).mp3
    D:\Test\5prodigy2(songs.pk).mp3
    D:\Test\5prodigy3(songs.pk).mp3
    D:\Test\5prodigy4(songs.pk).mp3
    D:\Test\5prodigy5(songs.pk).mp3
    D:\Test\5prodigy6(songs.pk).mp3
    D:\Test\5prodigy7(songs.pk).mp3
    D:\Test\5prodigy8(songs.pk).mp3
    D:\Test\5prodigy9(songs.pk).mp3

    And data for temp variable

    song vol one
    song vol two
    song vol three
    song vol four

    and such and such until song vol fourteen

     

    • Edited by raaif Thursday, September 29, 2011 2:33 PM
    Thursday, September 29, 2011 2:31 PM