VisualStudio - How to escape this chars - visual-studio

I think i need to escape the special chars here:
Process.Start(userSelectedFilePath, "\u0007" & ThisDir.Path & "\u000B" & checkedpath1 & "\u0007")
The result need to be like: userselecfilepath "a blackquoted path\and other folder"
what i'm doing wrong?
thankyou
UPDATE
Solution:
ControlChars.Quote & Path.Combine(ThisDir.Path, checkedpath1) & ControlChars.Quote

Process.Start(userSelectedFilePath, Path.Combine(ThisDir.Path, checkedpath));
Path.Combine
If path1 is not a drive reference (that is, "C:" or "D:") and does not
end with a valid separator character as defined in
DirectorySeparatorChar, AltDirectorySeparatorChar, or
VolumeSeparatorChar, DirectorySeparatorChar is appended to path1
before concatenation.
If path2 does not include a root (for example, if path2 does not start
with a separator character or a drive specification), the result is a
concatenation of the two paths, with an intervening separator
character. If path2 includes a root, path2 is returned.
The parameters are not parsed if they have white space. Therefore, if
path2 includes white space (for example, " c:\ "), the Combine method
appends path2 to path1 instead of returning only path2.
Not all invalid characters for directory and file names are
interpreted as unacceptable by the Combine method, because you can use
these characters for search wildcard characters. For example, while
Path.Combine("c:\", "*.txt") might be invalid if you were to create a
file from it, it is valid as a search string. It is therefore
successfully interpreted by the Combine method.

try this:
Process.Start(userSelectedFilePath, "\\" & ThisDir.Path & "\\" & checkedpath1 & "\\")

Related

The system cannot find the file if its path/name contains a space

Path = split(wscript.scriptFullName, wscript.scriptname)(0)
CreateObject("wscript.shell").run(Path & "Name.txt")
The above script works fine if both the file path and file name contain no spaces.
If either contains a space, the result will be;
Error: The system cannot find the file specified.
How can I fix the error?
The rules are fairly simple:
All strings have to start and end with double quotes to be a valid string.
Dim a
a = "Hello World" 'Valid string.
a = "Hello World 'Not valid and will produce an error.
Any use of variables must use the String Concatenation character & to combine them with strings.
Dim a: a = "Hello"
Dim b
b = a & " World" 'Valid concatenated string.
b = a " World" 'Not valid and will produce an error.
As double quotes are used to define a string, all instances of double quotes inside a string must be escaped by doubling the quotes "" but Rule 1. still applies.
Dim a: a = "Hello"
Dim b
b = """" & a & " World""" 'Valid escaped string.
b = """ & a & " World""" 'Not valid, start of string is not complete
'after escaping the double quote
'producing an error.
Follow these three rules and you won't go far wrong.
With those in mind the above line would need to be;
CreateObject("wscript.shell").run("""" & Path & "Name.txt""")
to generate a string surrounded by literal double quotes.
Useful Links
VBS with Space in File Path
Adding quotes to a string in VBScript
Breaking a String Across Multiple Lines (More on string concatenation).
CreateObject("wscript.shell").run(""""Path & "Name.txt""")
is how.

Regular expression to match special characters within double quotes

My input string is :
"& is here "& is here also, & has again occured""
Using gsub method in Ruby language, is there a way to substitute character '&' which is occuring within double quotes with character '$', if gsub method doesnt solve this problem, is there any other approach which can be used to address this problem.
Since first arguement in gsub method can be a regex, so matched regex will be substituted by the second arguement, getting a right regex for identifying might also solve this problem since it can be substituted in the gsub method for replacing '&' with '$'.
Expected output is as shown :
& is here "$ is here also , $ has again occured"
str = %q{& is here "& is here also , & has again occured"}
str.gsub!(/".*?"/) do |substr|
substr.gsub(/&/, '$')
end
puts str
# => & is here "$ is here also , $ has again occured"
EDIT: Just noticed that stribizhev proposed this way before I wrote it.

single quote escaping in applescript/shell

I'm trying to process this shell script in applescript, but it keeps saying error EOF, because of the single quote in the folder name.
do shell script "cp -R " & a & " " & "'" & d & "/My 'Folder/java/" & "'"
The folder /My 'Folder/ is a legitimate directory.
The variable a = '/Applications/MyProgram/' (and includes the single quotes)
The variable d = /Folders (with no single quotes)
However, shell is getting stuck processing it, im guessing because the folder is enclosed in quotes.
Is there any way to escape this single quote, so it works in applescript using shell? Ive tried multiple backslashes but its not working.
Cheers
N
Always use quoted form of when using arbitrary data as an argument to an command. This will always quote the value even if it doesn't need to be quoted but it's better safe than sorry. When you have a string single quoted, you can only unquote (turn substitution mode back on) with another quote. Unlike AppleScript strings, you can't escape characters inside single quoted strings. So you need to turn substitution mode on, escape a quote and then turn substitution mode back one. For instance "Joe's car" should be quoted as "'Joe'\\''s car'". It's a quoted string "Joe" + escaped quote character + quoted string "s car". But like I started you should use quoted form of, quoted form of "Joe's car" will return "'Joe'\\''s car'"
Your command using quoted form will look like:
do shell script "cp -R " & quoted form of a & space & quoted form of (d & "/My 'Folder/java/")
The problem arises because you have that tick in the filename of course, on the terminal commandline you would use a tick - double-tick tick - double-tick tick sequence to present it.
(From the terminal below)
729$ echo 'My '"'"'Java Folder'
My 'Java Folder
In Applescript, and your command line, it becomes even more complicated, I recommend you echo your commandline, until you get back what you expect.
set res to (do shell script "echo 'My '\"'\"'Java Folder'")
log res
--> (*My 'Java Folder*)
I think you'll have to start out with that, and reconstruct how you escape the rest of the commandline around it, if it can't be just plugged in as it is.
You should also remove single quotes for entities, that doesn't need them, (no spaces). That way your commandline will become easier to both edit and read.
Adding an answer for JavaScript for AppleScript (JAX) users based on answer from McUsr:
debugger
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var source = "/documents/John's Spreadsheet.xls";
var target = "/documents/John's Spreadsheet.csv";
source = source.replace("'", "'\"'\"'", "g");
target = target.replace("'", "'\"'\"'", "g");
var exportScript = "/excel -i";
exportScript += " '" + source + "'";
exportScript += " '" + target + "'";
exportScript += ";true";
try {
app.doShellScript(exportScript);
}
catch (error) {
console.log(error.message);
}
If you don't know what JAX is it's AppleScript but using JavaScript. Open Script Editor and select JavaScript from the dropdown beneath the record button.

Why doesn't File.exist find this file?

I have a variable like book_file_name which stores a filename with path like this:
book_file_name
=> "./download/Access\\ Database\\ Design\\ \\&\\ Programming,\\ 3rd\\ Edition.PDF"
puts book_file_name
./download/Access\ Database\ Design\ \&\ Programming,\ 3rd\ Edition.PDF
=> nil
book_file_name.length
=> 71
When I use File.exists? to check the file, something is wrong.
This is how I use the string:
File.exists?("./download/Access\ Database\ Design\ \&\ Programming,\ 3rd\ Edition.PDF")
=> true
This is how I use the variable:
File.exists?(book_file_name)
=> false
What's wrong with the variable?
The string
"./download/Access\ Database\ Design\ \&\ Programming,\ 3rd\ Edition.PDF"
is in double-quotes, which causes the backslash+space to be replaced with space
This won't happen with a string variable like book_file_name, and won't happen in a string enclosed within single quotes.
I can see the actual book name with path is
'./download/Access Database Design & Programming, 3rd Edition.PDF'
so
File.exists?('./download/Access Database Design & Programming, 3rd Edition.PDF')
File.exists?("./download/Access Database Design & Programming, 3rd Edition.PDF")
book_file_name = './download/Access Database Design & Programming, 3rd Edition.PDF'
File.exists?(bookfilename)
book_file_name = "./download/Access Database Design & Programming, 3rd Edition.PDF"
File.exists?(bookfilename)
will all work just fine... so you're better off not using backslashes.
As you have shown in your code snippets, the string contained in your variable has backslashes in it. You don't need to escape the spaces, but if you do, you only need to escape them with one backslash. As it stands, you are using double backslashes; the first backslash escapes the second, and has no impact on the space.
puts "file name with spaces"
# => file name with spaces
puts "file\ name\ with\ spaces"
# => file name with spaces
puts "file\\ name\\ with\\ spaces"
# => file\ name\ with\ spaces
This explains why your string literal succeeds where your variable fails: the two strings are not equivalent. So just store the same string literal that succeeded (the one with single backslashes) or else the string literal without any backslashes and you should be good to go.

vbscript - Replace all spaces

I have 6400+ records which I am looping through. For each of these: I check that the address is valid by testing it against something similar to what the Post Office uses (find address). I need to double check that the postcode I have pulled back matches.
The only problem is that the postcode may have been inputted in a number of different formats for example:
OP6 6YH
OP66YH
OP6 6YH.
If Replace(strPostcode," ","") = Replace(xmlAddress.selectSingleNode("//postcode").text," ","") Then
I want to remove all spaces from the string. If I do the Replace above, it removes the space for the first example but leave one for the third.
I know that I can remove these using a loop statement, but believe this will make the script run really slow as it will have to loop through 6400+ records to remove the spaces.
Is there another way?
I didn't realise you had to add -1 to remove all spaces
Replace(strPostcode," ","",1,-1)
Personally I've just done a loop like this:
Dim sLast
Do
sLast = strPostcode
strPostcode = Replace(strPostcode, " ", "")
If sLast = strPostcode Then Exit Do
Loop
However you may want to use a regular expression replace instead:
Dim re : Set re = New RegExp
re.Global = True
re.Pattern = " +" ' Match one or more spaces
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("O P 6 6 Y H.", "")
Set re = Nothing
The output of the latter is:
D:\Development>cscript replace.vbs
OP66YH.
OP66YH.
OP66YH.
D:\Development>
This is the syntax Replace(expression, find, replacewith[, start[, count[, compare]]])
it will default to -1 for count and 1 for start. May be some dll is corrupt changing the defaults of Replace function.
String.Join("", YourString.Split({" "}, StringSplitOptions.RemoveEmptyEntries))
Because you get all strings without spaces and you join them with separator "".

Resources