Check that file exists: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,804: | Line 1,804: | ||
This needs to be compiled with the gio-2.0 package: valac --pkg gio-2.0 check_that_file_exists.vala |
This needs to be compiled with the gio-2.0 package: valac --pkg gio-2.0 check_that_file_exists.vala |
||
<lang vala>int main (string[] args) { |
<lang vala>int main (string[] args) { |
||
string[] files = {"input.txt", "docs", |
string[] files = {"input.txt", "docs", Path.DIR_SEPARATOR_S + "input.txt", Path.DIR_SEPARATOR_S + "docs"}; |
||
⚫ | |||
foreach (string f in files) { |
foreach (string f in files) { |
||
var file = File.new_for_path (f); |
var file = File.new_for_path (f); |
||
print ("%s exists: %s\n", f, file.query_exists ().to_string ()); |
print ("%s exists: %s\n", f, file.query_exists ().to_string ()); |
||
} |
|||
return 0; |
|||
}</lang> |
|||
A more complete version which informs whether the existing file is a regular file or a directory |
|||
<lang vala>int main (string[] args) { |
|||
⚫ | |||
foreach (string f in files) { |
|||
var file = File.new_for_path (f); |
|||
var exists = file.query_exists (); |
|||
var name = ""; |
|||
if (!exists) { |
|||
print ("%s does not exist\n", f); |
|||
} else { |
|||
var type = file.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS); |
|||
if (type == 1) { |
|||
name = "file"; |
|||
} else if (type == 2) { |
|||
name = "directory"; |
|||
} |
|||
print ("%s %s exists\n", name, f); |
|||
} |
|||
} |
} |
||
return 0; |
return 0; |