1. file_exists and file name in quotes

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

new topic     » topic index » view message » categorize

2. Re: file_exists and file name in quotes

clk824 said...

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})) 
new topic     » goto parent     » topic index » view message » categorize

3. Re: file_exists and file name in quotes

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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: file_exists and file name in quotes

clk824 said...

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}) 
new topic     » goto parent     » topic index » view message » categorize

5. Re: file_exists and file name in quotes

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu