|
-
Feb 26th, 2013, 09:31 AM
#1
Thread Starter
Hyperactive Member
Writing to Text File - Need to Escape Quotes
I can't seem to get the correct syntax for this:
Code:
objTmpWriter.WriteLine("C:\Program Files\7-Zip\7z.exe a C:\xampp\htdocs\out\" & _
Form1.UniqueIDNum & ".zip c:\0\downtime.pdf")
I write this to a batch file which is then run with Process.Start but of course it will not run "Program Files" unless I get the quotes around it. Doubling on the quotes doesn't seem to work correctly either.
-
Feb 26th, 2013, 09:56 AM
#2
Thread Starter
Hyperactive Member
Re: Writing to Text File - Need to Escape Quotes
Sorry about this...the instruction said to use double quotes, when in reality I needed to use triple quotes....translation, "Short circuit between the seat and the keyboard."
Code:
objTmpWriter.WriteLine("""C:\Program Files\7-Zip\7z.exe"" a C:\xampp\htdocs\out\" & _
Form1.UniqueIDNum & ".zip c:\0\downtime.pdf")
-
Feb 26th, 2013, 09:58 AM
#3
Re: Writing to Text File - Need to Escape Quotes
Do you mean because of the space? Try the shortname "Progra~1". Still works up including win8 as a shortcut to Program Files.
On the other hand, if you need to use quotes, Try generating a string first, and then passing it to your function... that way you can debug the output string before processing it.
-
Feb 26th, 2013, 09:58 AM
#4
Re: Writing to Text File - Need to Escape Quotes
Do you mean because of the space? Try the shortname "Progra~1". Still works up including win8 as a shortcut to Program Files.
On the other hand, if you need to use quotes, Try generating a string first, and then passing it to your function... that way you can debug the output string before processing it.
EDIT: darn, I need to trust my instincts more... I had already typed "try triple quotes", but I couldn't test it right so I thought I had misremembered. But I wasn't testing with a function call and that threw off my test. Anyways, congrats on figuring it out!
-
Feb 26th, 2013, 10:02 AM
#5
Re: Writing to Text File - Need to Escape Quotes
odd, somehow it got reposted when I edited it. Sorry about that.
-
Feb 26th, 2013, 10:08 AM
#6
Re: Writing to Text File - Need to Escape Quotes
Try
Code:
Dim CommandText = <X>"C:\Program Files\7-Zip\7z.exe" a C:\xampp\htdocs\out\<%= Form1.UniqueIDNum %>.zip c:\0\downtime.pdf</X>.Value
-
Feb 26th, 2013, 10:10 AM
#7
Re: Writing to Text File - Need to Escape Quotes
Also try this (not my code)
Code:
Imports Microsoft.Win32
Public Enum ZipOverWriteOptions
''' <summary>
''' Overwrite
''' </summary>
''' <remarks></remarks>
''' <summary>
''' Skip
''' </summary>
''' <remarks></remarks>
End Enum
Public Class My7Zip
Public Property OverWriteOption As ZipOverWriteOptions = ZipOverWriteOptions.aoa
''' <summary>
''' File to extract files from
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ArchiveFileName As String = ""
''' <summary>
''' Password for ArchiveFileName
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Password As String = ""
''' <summary>
''' Path to extract Archive File contents
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Destination As String
Private mExecutablePath As String = ""
''' <summary>
''' Location of 7 Zip executable
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property ExecutablePath As String
Get
Return mExecutablePath
End Get
End Property
Private mExecutable As String = ""
''' <summary>
''' 7 Zip executable name
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property Executable As String
Get
If mExecutable.Contains("\") Then
Return mExecutable
Else
Return IO.Path.Combine(mExecutablePath, mExecutable)
End If
End Get
End Property
Public Property Recurse As Boolean = True
Private mIsInstalled As Boolean = True
''' <summary>
''' Is 7 Zip installed
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property IsInstalled As Boolean
Get
Return mIsInstalled
End Get
End Property
Private Sub GetExecutablePath()
Dim reg As RegistryKey
reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\7-Zip", False)
If reg IsNot Nothing Then
mExecutablePath = reg.GetValue("Path").ToString
mIsInstalled = IO.File.Exists(Executable)
Else
mIsInstalled = False
End If
End Sub
Private mReturnCode As Int32 = 0
Public ReadOnly Property ReturnCode As Int32
Get
Return mReturnCode
End Get
End Property
''' <summary>
''' Use hardcoded 7-Zip executable file name
''' </summary>
''' <remarks></remarks>
Public Sub New()
mExecutable = "7z.exe"
GetExecutablePath()
End Sub
''' <summary>
''' Specify executable or executable with path
''' </summary>
''' <param name="ExecutableName"></param>
''' <remarks></remarks>
Public Sub New(ByVal ExecutableName As String)
mExecutable = ExecutableName
If Not ExecutableName.Contains("\") Then
GetExecutablePath()
Else
mExecutablePath = IO.Path.GetDirectoryName(ExecutableName)
End If
End Sub
Public Function Extract() As Boolean
Dim RecurseArchive As String = If(Me.Recurse, "-r", "-r-")
Dim OverWriteOption As String = " -" & Me.OverWriteOption.ToString
Dim Result As Boolean = False
Dim startInfo As New ProcessStartInfo(Me.Executable)
startInfo.WindowStyle = ProcessWindowStyle.Hidden
If Me.Password.Length > 0 Then
startInfo.Arguments = <X>e "<%= Me.ArchiveFileName %>" -p"<%= Me.Password %>" -o"<%= Me.Destination %>" <%= RecurseArchive %><%= OverWriteOption %></X>.Value
Else
startInfo.Arguments = <X>e "<%= Me.ArchiveFileName %>" -o"<%= Me.Destination %>" <%= RecurseArchive %><%= OverWriteOption %></X>.Value
End If
Dim TheProcess = Process.Start(startInfo)
While Not TheProcess.HasExited
Application.DoEvents()
End While
mReturnCode = TheProcess.ExitCode
Result = mReturnCode = 0
Return Result
End Function
End Class
Usage
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Extraction As New My7Zip()
If Extraction.IsInstalled Then
Extraction.ArchiveFileName = "C:\Backups\ReadCSV.7z"
Extraction.Password = "password"
Extraction.Destination = "C:\Test7"
Extraction.OverWriteOption = ZipOverWriteOptions.aoa
If Not Extraction.Extract() Then
MessageBox.Show("Failed to extract data, error code " & Extraction.ReturnCode.ToString)
Else
MessageBox.Show("Done")
End If
End If
End Sub
End Class
-
Feb 26th, 2013, 10:40 AM
#8
Re: Writing to Text File - Need to Escape Quotes
 Originally Posted by Vladamir
Sorry about this...the instruction said to use double quotes, when in reality I needed to use triple quotes....translation, "Short circuit between the seat and the keyboard."
Code:
objTmpWriter.WriteLine("""C:\Program Files\7-Zip\7z.exe"" a C:\xampp\htdocs\out\" & _
Form1.UniqueIDNum & ".zip c:\0\downtime.pdf")
A literal double quote is escaped with another double quote. That's why it requires 3 double quotes at the beginning of your string: the 1st one is to to indicate this is a string, the 2 following it is for a literal double quote and its escape character. In those cases where literal quotes present in a string, I find it's less confusing to build the string using string.format function.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|