JavaScript: Escape (encode?) special characters in filenames - character-encoding

I have a JavaScript that runs an external program (mediainfo) on local files. However, some of my files have characters that mess up my code.
The code:
objMedInfo = JSON.parse(proc.execSync('mediainfo "' + currentfilename + '" --output=JSON').toString());
The problem is that if currentfilename contains a quotation mark or some other extended characters, the command fails.
Is there a way to escape or encode currentfilename to prevent this? It's not feasible to rename all the potential files.

Use execFileSync instead of execSync. This allows you to supply the arguments in an array, rather than putting everything in a string that has to be parsed by the shell.
objMedInfo = JSON.parse(proc.execFileSync('mediainfo', [currentfilename, '--output=JSON']).toString());

Related

Using Umlaut or special characters in ibm-doors from batch

We have a link module that looks something like this:
const string lMod = "/project/_admin/somethingÜ" // Umlaut
We later use the linkMod like this to loop through the outlinks:
for a in obj->lMod do {}
But this only works when executing directly from DOORS and not from a batch script since it for some reason doesn't recognize the Umlaut causing the inside of the loop to never to be run; exchanging lMod with "*" works and also shows the objects linked to by the lMod.
We are already using UTF-8 encoding for the file:
pragma encoding, "UTF-8"
Any solutions are welcome.
Encode the file as UTF-8 in Notepad++ by going to Encoding > Convert to UTF-8. (Make sure it's not already set to UTF-8 before you do it).

Error in manipulating sting with "\" in Lua

local filepath = "c:\\temp\\parentdir"
local dirname = string.match(filepath, "[^\\]+$")
print(dirname)
Above code is giving correct output: parentdir
Then if I use same code but only single "\" as below
local filepath = "c:\temp\parentdir"
local dirname = string.match(filepath, "[^\\]+$")
print(dirname)
I'm getting this error
"lua: main.lua:1: invalid escape sequence near '"c: emp\p'"
please help. How we can solve this issue programatically
It isn't a issue, becaouse you not provide c:\temp\parentdir literally, but c:{tab}emp{unknown_character}arentdir. To provide c:\temp\parentdir you can use:
"c:\\temp\\parentdir"
'c:\\temp\\parentdir'
[[c:\temp\parentdir]]
If you read c:\temp\parentdir from file or input you not have to add second \, because this is only in code escape character. More about here.
Second solution is to use / instead \.

ios sdk URL filename, remove the url addon special character "\"

In iOS sdk (swift). Lets say i have a three files "mary'sCat.mp3", "mary\s.mp3", "mary\\s.mp3"
(the special character \ is part of the real filename)
When I use the below code to get the urls
FileManager.default.contentsOfDirectory(at: documentDir, includingPropertiesForKeys: nil)
and use the below code to get filename
fileUrl.standardizedFileURL.lastPathComponent
I will have "mary\'sCat.mp3", "mary\\s.mp3", "mary\\\\s.mp3"
So.. is there any way i can remove the system addon special characters \ in a correct way? so i can get back to original file name "mary'sCat.mp3", "mary\s.mp3", "mary\\s.mp3"? I noticed that when using XCode output window you wont see the addon special character, but when you actually see it in debug watch, u will see the addon special character. That is nightmare when doing String compare as below
let mediaUrl_filename = "mary\\s.mp3" \\<-- this value from url
let db_filename = "mary\s.mp3" \\ <-- this value from sqlite
if mediaUrl_filename == db_filename {
print("It is equal")
}
So is there any way to solve this problem?
Actually '\' is used as escape sequence in String objects so while comparing the file name if the file name is 'mary\s.mp3' you will have to write like this
if mediaUrl_filename == "mary\\s.mp3"{
print("It is equal")
}
I suggest that you replace the '\' with other character like '_' in the file name to ignore such confusions.

F# FSI - Is there a limit to the script length? String Literal appears too long

On transferring a F# file -- Jira.fs to a script file -- Jira.fsx
I am encountering a problem with a constant string that uses triple quotes and within it contains double quotes (example below)
Jira.fs = success
Jira.fsx = fails with error FS0010: Unexpected symbol ':' in expression. Expected '}' or other token.
Warnings include - Warning: line too long, ignoring some characters
Is there a limit to .fsx scripts?
If so is there a recommended way around this? Just read in the string from a file?
Code below: thanks
[<Literal>]
let childIssueSchema = """ {"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"18043","self":"https://atlassian.au.MyCompany.net/jira/rest/api/latest/issue/18043","key":"DPMITPRODU-141","fields":{"progress":{"progress":0,"total":0},"summary":"APC - Calculator link on AOL ","customfield_10560":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/10340","value":"Yes","id":"10340"},"customfield_11067":null,"timetracking":{},"customfield_11066":null,"issuetype":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issuetype/6","id":"6","description":"A user story","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/sales.gif","name":"User Story","subtask":false},"customfield_10562":null,"customfield_11069":null,"customfield_11068":null,"customfield_11160":"4837","customfield_11161":null,"customfield_11660":null,"timespent":null,"reporter":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/user?username=lkaligotla","name":"lkaligotla","emailAddress":"lkaligotla#MyCompany.com.au","avatarUrls":{"16x16":"https://atlassian.au.MyCompany.net/jira/secure/useravatar?size=small&avatarId=10102","48x48":"https://atlassian.au.MyCompany.net/jira/secure/useravatar?avatarId=10102"},"displayName":"Lakshmi Kaligotla","active":true},"created":"2013-12-31T14:39:09.457+1100","updated":"2014-01-02T09:32:57.023+1100","customfield_10041":null,"priority":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/priority/3","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/priority_major.gif","name":"Medium","id":"3"},"description":"The current Age pension Calculator Icon must lead to a landing page with content and Start Calculator Button \r\n\r\nOn Click of start Calculator button calculator Home Page with instructions and start calculator button ","customfield_10002":null,"customfield_10003":null,"customfield_10040":null,"issuelinks":[{"id":"13177","self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issueLink/13177","type":{"id":"10010","name":"CrossProjectLink","inward":"part of","outward":"contains","self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issueLinkType/10010"},"inwardIssue":{"id":"18036","key":"DPMITPROJ-35","self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issue/18036","fields":{"summary":"APC - Calculator","status":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/status/10003","description":"User has placed this item in the queue","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/status_visible.gif","name":"In Queue","id":"10003"},"priority":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/priority/3","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/priority_major.gif","name":"Medium","id":"3"},"issuetype":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issuetype/5","id":"5","description":"A big user story that needs to be broken down.","iconUrl":"https://atlassian.au.MyCompany.net/jira/download/resources/com.pyxis.greenhopper.jira:greenhopper-webactions/images/ico_epic.png","name":"Epic","subtask":false}}}}],"customfield_10000":null,"customfield_10765":null,"subtasks":[],"customfield_10767":null,"status":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/status/10016","description":"","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/status_open.gif","name":"Backlog","id":"10016"},"labels":[],"workratio":-1,"project":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/project/DPMITPRODU","id":"11433","key":"DPMITPRODU","name":"DPMIT-Products","avatarUrls":{"16x16":"https://atlassian.au.MyCompany.net/jira/secure/projectavatar?size=small&pid=11433&avatarId=11680","48x48":"https://atlassian.au.MyCompany.net/jira/secure/projectavatar?pid=11433&avatarId=11680"}},"environment":null,"customfield_10053":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/10030","value":"Yes","id":"10030"},"aggregateprogress":{"progress":0,"total":0},"customfield_10050":"A link is available and on click leads to Calculor Landing page\r\nhttps://adviseronlineportal.com.au/Agepensioncalculator","components":[],"comment":{"startAt":0,"maxResults":0,"total":0,"comments":[]},"timeoriginalestimate":null,"customfield_10461":null,"customfield_10460":null,"customfield_11963":[{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/11441","value":"False","id":"11441"}],"customfield_10360":null,"votes":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issue/DPMITPRODU-141/votes","votes":0,"hasVoted":false},"customfield_10261":null,"customfield_10262":null,"customfield_10263":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/10061","value":"Yes","id":"10061"},"fixVersions":[],"resolution":null,"resolutiondate":null,"aggregatetimeoriginalestimate":null,"customfield_10161":null,"customfield_10160":null,"duedate":null,"customfield_10020":null,"customfield_10060":"4793","watches":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issue/DPMITPRODU-141/watchers","watchCount":1,"isWatching":false},"customfield_10162":null,"worklog":{"startAt":0,"maxResults":0,"total":0,"worklogs":[]},"assignee":null,"attachment":[],"aggregatetimeestimate":null,"versions":[],"timeestimate":null,"customfield_10030":null,"customfield_10031":null,"aggregatetimespent":null}} """
type ChildJsonSchema = FSharp.Data.JsonProvider< childIssueSchema >
I don't think there is a limit on the size of a script file (not sure what would happen around 2GB, but that does not sound like a realistic scenario), but for some reason (not sure why!) there is a limit on a single line length.
This is a bit unfortunate when you just want to copy & paste sample for a JSON type provider, but if you're using """ quotes, then you can just have a newline character in the string and it will still be treated as a single constant string:
[<Literal>]
let childIssueSchema = """{"foo":1,
"bar":42}"""
type ChildJsonSchema = FSharp.Data.JsonProvider<childIssueSchema>
That said, in case you have long and complex samples, it might be better to save them to files and just point the type provider to a file. Assuming you have childIssue.json in the same folder as the source file, you should be able to write:
type ChildJsonSchema = FSharp.Data.JsonProvider<"childIssue.json">

Backslash read and write and F# interactive console

Edit: whats the difference between reading a backslash from a file and writing it to the interactive window vs writing directly the string to the interactive window ?
For example
let toto = "Adelaide Gu\u00e9nard"
toto;;
the interactive window prints "Adelaide Guénard".
Now if I save a txt file with the single line Adelaide Gu\u00e9nard . And read it in:
System.IO.File.ReadAllLines(#"test.txt")
The interactive window prints [|"Adelaide Gu\u00e9nard"|]
What is the difference between these 2 statements in terms of the interactive window printing ?
As far as I know, there is no library that would decode the F#/C# escaping of string for you, so you'll have to implement that functionality yourself. There was a similar question on how to do that in C# with a solution using regular expressions.
You can rewrite that to F# like this:
open System
open System.Globalization
open System.Text.RegularExpressions
let regex = new Regex (#"\\[uU]([0-9A-F]{4})", RegexOptions.IgnoreCase)
let line = "Adelaide Gu\\u00e9nard"
let line = regex.Replace(line, fun (m:Match) ->
(char (Int32.Parse(m.Groups.[1].Value, NumberStyles.HexNumber))).ToString())
(If you write "some\\u00e9etc" then you're creating string that contains the same thing as what you'd read from the text file - if you use single backslash, then the F# compiler interprets the escaping)
It uses the StructuredFormat stuff from the F# PowerPack. For your string, it's effectively doing printfn toto;;.
You can achieve the same behaviour in a text file as follows:
open System.IO;;
File.WriteAllText("toto.txt", toto);;
The default encoding used by File.WriteAllText is UTF-8. You should be able to open toto.txt in Notepad or Visual Studio and see the é correctly.
Edit: If wanted to write the content of test.txt to another file in the clean F# interactive print, how would i proceed ?
It looks like fsi is being too clever when printing the contents of test.txt. It's formatting it as a valid F# expression, complete with quotes, [| |] brackets, and a Unicode character escape. The string returned by File.ReadAllLines doesn't contain any of these things; it just contains the words Adelaide Guénard.
You should be able to take the array returned by File.ReadAllLines and pass it to File.WriteAllLines, without the contents being mangled.

Resources