file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names
file system object does not allow spaces in file names file system object does not allow spaces in file names
file system object does not allow spaces in file names
Go Back  Xtreme Visual Basic Talk > > > file system object does not allow spaces in file names


Reply
 
Thread Tools Display Modes
  #1  
Old 02-03-2010, 01:00 PM
frasmus frasmus is offline
Newcomer
 
Join Date: Feb 2010
Posts: 1
Default file system object does not allow spaces in file names


I am working on a VB Console Application that takes an Autocad drawing type DWG and converts it to a PDF using a shell command that calls a third party application. In this case, acmecadconverter.exe from www.dwgtool.com. Then the PDF that is created needs a unique watermark, so I call a second application for that called pdftk.exe from www.accesspdf.com/pdftk/.

Everything works as intended, except when I try file names that contain spaces. The file system object does not tolerate spaces in the drawing file name. For instance, the following command gives me a system.io.filenotfoundexception...

My.Computer.FileSystem.RenameFile("Test Flowchart1.dwg", "Test~Flowchart1.dwg")

It's the same problem for all of my File System commands. Either file not found or invalid arguments.

The complete script I'm using is here:

Code:
    Sub Main()
        Dim arrArgs() As String = Command.Split(","), _
        i As Integer, folderPath As String, myPart As String, _
        fileName As String, myPDF1 As String, myPDF2 As String, _
        myWatermark As String, OutputPath As String, TempFile As String, _
        newFileName As String

        myWatermark = ""
        folderPath = ""
        fileName = ""
        newFileName = ""
        myPart = ""
        myPDF1 = ""
        myPDF2 = ""
        OutputPath = ""
        TempFile = ""

        Console.Write(vbNewLine & vbNewLine)

        If arrArgs(0) <> Nothing Then
            'arguments found
            For i = LBound(arrArgs) To UBound(arrArgs)
                If i = 0 Then 'dwg folder path
                    folderPath = arrArgs(i)
                ElseIf i = 1 Then 'dwg filename
                    fileName = arrArgs(i)
                ElseIf i = 2 Then 'watermark
                    myWatermark = arrArgs(i)
                ElseIf i = 3 Then 'PDF output path
                    OutputPath = arrArgs(i)
                End If
                Console.Write("Parameter " & i & " is " & arrArgs(i) & vbNewLine)
            Next

            My.Computer.FileSystem.CurrentDirectory = folderPath

            'test for space in the file name and replace with ~
            If InStr(fileName, " ") > 0 Then
                newFileName = Replace(fileName, " ", "~")
                My.Computer.FileSystem.RenameFile(fileName, newFileName)
                fileName = newFileName
            End If

            myPart = Left(fileName, InStrRev(fileName, ".") - 1)
            myPDF1 = myPart & ".PDF"
            myPDF2 = myPart & "1.PDF"

            'Check the output path
            If OutputPath <> "" And Right(OutputPath, 1) <> "\" Then
                OutputPath = RTrim(OutputPath) & "\"
            End If

            'Copy the watermark to a temp file name to enable concurrent sessions
            TempFile = RandomNumber() & ".pdf"

            'List the variables
            Console.Write("folderPath=" & folderPath & vbNewLine)
            Console.Write("shortfolderPath=" & folderPath & vbNewLine)
            Console.Write("fileName=" & fileName & vbNewLine)
            Console.Write("myPart=" & myPart & vbNewLine)
            Console.Write("myPDF1=" & myPDF1 & vbNewLine)
            Console.Write("myPDF2=" & myPDF2 & vbNewLine)
            Console.Write("myWatermark=" & myWatermark & vbNewLine)
            Console.Write("TempFile=" & TempFile & vbNewLine)
            Console.Write("OutputPath=" & OutputPath & vbNewLine)

            'Set the current directory so you don't need to reference the path everywhere
            My.Computer.FileSystem.CurrentDirectory = folderPath

            'Create the PDF
            Shell("acmecadconverter /r /ad /e /f 104 " & fileName, AppWinStyle.Hide, True)

            'Copy the watermark to a temp file name to enable concurrent sessions
            My.Computer.FileSystem.CopyFile(myWatermark, TempFile, True)

            'Apply the watermark to a new PDF file
            Shell("pdftk " & myPDF1 & " background " & TempFile & " output " & myPDF2, AppWinStyle.Hide, True)
            'Delete the temporary watermark image
            My.Computer.FileSystem.DeleteFile(TempFile)
            'Replace the original PDF with the watermarked version
            My.Computer.FileSystem.DeleteFile(myPDF1)
            If My.Computer.FileSystem.FileExists(OutputPath & myPDF1) Then My.Computer.FileSystem.DeleteFile(OutputPath & myPDF1)
            System.IO.File.Move(myPDF2, OutputPath & myPDF1)


        Else 'No parameters entered so describe the program functionality

            Console.Write("## Convert a DWG to a PDF ##" & vbNewLine)
            Console.Write("This console application calls the following two programs..." & vbNewLine)
            Console.Write(vbNewLine)
            Console.Write("1. acmecadconverter.exe from www.dwgtool.com" & vbNewLine)
            Console.Write("   gsapp.dll goes with acmecadconverter.exe" & vbNewLine)
            Console.Write("   gsio.dll goes with acmecadconverter.exe" & vbNewLine)
            Console.Write("   gsui.dll goes with acmecadconverter.exe" & vbNewLine)
            Console.Write("   iow.dll goes with acmecadconverter.exe" & vbNewLine)
            Console.Write(vbNewLine)
            Console.Write("2. pdftk.exe from www.accesspdf.com/pdftk/" & vbNewLine)
            Console.Write(vbNewLine)
            Console.Write("All of these must be present in the system path" & vbNewLine)
            Console.Write("For instance, c:\windows\" & vbNewLine)
            Console.Write(vbNewLine)
            Console.Write(vbNewLine)
            Console.Write("Syntax:" & vbNewLine)
            Console.Write(vbNewLine)
            Console.Write("dwg2pdf.exe dwg_folderpath, dwg_filename, watermark_path-filename, [pdf_output_path]" & vbNewLine)
        End If

        Console.Write(vbNewLine & vbNewLine)
        Console.Write("Press any key to continue...")
        Console.ReadKey()

    End Sub

    Private Function RandomNumber() As Integer
        Randomize()
        RandomNumber = Format(100000 * Rnd(2) + 100, "#")
    End Function
Thanks in advance for whatever help you can give me.

Last edited by frasmus; 02-03-2010 at 01:07 PM.
Reply With Quote
  #2  
Old 02-11-2010, 03:29 PM
Cerian Knight's Avatar
Cerian Knightfile system object does not allow spaces in file names Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

You probably need to wrap filenames that contain spaces with an additional set of quotes. This post describes a structured way of doing so: http://www.xtremevbtalk.com/showpost...2&postcount=10
See the rest of the thread for a more simplistic approach.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #3  
Old 02-11-2010, 04:04 PM
AtmaWeapon's Avatar
AtmaWeaponfile system object does not allow spaces in file names AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It doesn't take much to verify whether the FileSystem object rejects spaces or if it needs quotes:
Code:
Module Module1

    Sub Main()
        Dim originalName As String = "test file.txt"
        Dim finalName As String = "test~file.txt"

        ' Get rid of file from previous runs
        My.Computer.FileSystem.DeleteFile(finalName)

        My.Computer.FileSystem.WriteAllText(originalName, "asdf", False)
        My.Computer.FileSystem.RenameFile(originalName, finalName)

        Console.WriteLine(My.Computer.FileSystem.FileExists(originalName))
        Console.WriteLine(My.Computer.FileSystem.FileExists(finalName))
    End Sub

End Module
The verdict on my machine is it doesn't require spaces. However, my guess is the problem lies in this block of code, interesting parts bolded:
Code:
My.Computer.FileSystem.CurrentDirectory = folderPath

'test for space in the file name and replace with ~
If InStr(fileName, " ") > 0 Then
    newFileName = Replace(fileName, " ", "~")
    My.Computer.FileSystem.RenameFile(fileName, newFileName)
    fileName = newFileName
End If
I see nothing to guarantee that setting the CurrentDirectory property will have an effect on RenameFile(). It would make sense if it did, but when I look at the documentation I don't see much to suggest that this is the case. So let's test it out:
Code:
Module Module1

    Sub Main()
        Dim directoryName As String = "test"
        Dim originalName As String = "test file.txt"
        Dim originalPath As String = System.IO.Path.Combine(directoryName, originalName)
        Dim finalName As String = "test~file.txt"
        Dim finalPath As String = System.IO.Path.Combine(directoryName, finalName)

        ' Get rid of file from previous runs
        If My.Computer.FileSystem.FileExists(finalPath) Then
            My.Computer.FileSystem.DeleteFile(finalPath)
        End If

        My.Computer.FileSystem.CreateDirectory(directoryName)
        My.Computer.FileSystem.WriteAllText(originalPath, "asdf", False)
        My.Computer.FileSystem.CurrentDirectory = directoryName
        My.Computer.FileSystem.RenameFile(originalName, finalName)

        Console.WriteLine(My.Computer.FileSystem.FileExists(originalPath))
        Console.WriteLine(My.Computer.FileSystem.FileExists(finalPath))
    End Sub

End Module
I get False, False. So it looks like CurrentDirectory has no effect. So what do you do? Well, you could do as I did in the example and include the directory name when working with the file. In my opinion, it's a little easier to just work with the System.IO objects; it'd save your wrist a ton of "My.Computer.FileSystem" typing. Here's what your block of code would look like (example assumes System.IO is imported):
Code:
        
Dim filePath As String = Path.Combine(folderPath, fileName)
Dim file As New FileInfo(filePath)

If file.Exists AndAlso file.Name.Contains(" ") Then
    Dim newFilePath As String = filePath.Replace(" "c, "~"c)
    file.MoveTo(newFilePath)
End If
Personally I hate stuff like the CurrentDirectory property. It's always begging for you to forget that you changed it, leading you to spend hours tracking down why your files are going to weird places.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply

Tags
file name, file system object, filename, space


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names file system object does not allow spaces in file names
file system object does not allow spaces in file names
file system object does not allow spaces in file names
 
file system object does not allow spaces in file names
file system object does not allow spaces in file names
 
-->