1. file_exists and file name in quotes
- Posted by clk824 Dec 31, 2010
- 1185 views
I have file name with spaces inside and it is enclosed in quotes. This is standard rule. file_exists return false with this quotes, but without quotes system_exec not work. Seems to be bug in file_exists?
eu4, windows
2. Re: file_exists and file name in quotes
- Posted by DerekParnell (admin) Dec 31, 2010
- 1147 views
I have file name with spaces inside and it is enclosed in quotes. This is standard rule. file_exists return false with this quotes, but without quotes system_exec not work. Seems to be bug in file_exists?
eu4, windows
Are you saying you are trying something like this ...
sequence filename filename = "\"My File With Spaces.txt\"" if file_exists(filename) then system_exec("somecmd " & filename)
And file_exists() always reports the file as missing?
If so, I think that this is not a bug. There are many library routines that take a file name as a parameter, and none of them (as far as I can remember) strip off enclosing quotes.
Note that the system_exec() routine does not take a file name as a parameter, instead it takes a command line as a parameter and it is the command line that needs quoted files.
May I suggest that you code more like this instead ...
sequence filename filename = "My File With Spaces.txt" if file_exists(filename) then system_exec(sprintf(`somecmd "%s"`, {filename}))
3. Re: file_exists and file name in quotes
- Posted by clk824 Jan 01, 2011
- 1096 views
I get value of environment variable which contains full path of executable file in quotes. In my case value is: "C:\Program Files\Notepad++\notepad++.exe"
My code:
sequence editor = getenv("EDITOR") if not file_exists(editor) then editor = getenv("WINDIR") & "\\notepad.exe" end if system_exec(editor)
I always get runned notepad. If i enter in console %editor%, proper editor runned and i think system_exec must work just the same.
4. Re: file_exists and file name in quotes
- Posted by DerekParnell (admin) Jan 01, 2011
- 1071 views
I get value of environment variable which contains full path of executable file in quotes. In my case value is: "C:\Program Files\Notepad++\notepad++.exe"
Well, it seems like you have defined the EDITOR symbol to have quotes around the file name. If this is how you want it to stay then you will need to strip off the quotes.
function dequote_env(sequence name) object res res = getenv(name) if sequence(res) then if length(res) >= 2 then if res[1] = '"' and res[$] = '"' then res = res[2..$-1] end if end if end if return res end function sequence editor = dequote_env("EDITOR") if not file_exists(editor) then editor = dequote_env("WINDIR") & "\\notepad.exe" end if system_exec(sprintf(`"%s"`, {editor})
5. Re: file_exists and file name in quotes
- Posted by clk824 Jan 01, 2011
- 1103 views
Thanks for your code DerekParnell, but my question is more theoretical thаn practical. I have solution for my this concrete problem but i really don't expect what i need use dequoted file name with one function and quoted with another. I was check this issue in Python and got the same result from function os.path.exists. My misunderstanding. Thanks all.