Garry's Mod Wiki

Revision Difference

file.Exists#528560

<function name="Exists" parent="file" type="libraryfunc"> <description> Returns a boolean of whether the file or directory exists or not. <bug issue="1038"> This will sometimes return false clientside for directories received from the server via a clientside lua file. You can work around this by using <page>file.Find</page> with the path to the directory followed by a wildcard (no trailing forward slash) and see if the directory is inside the returned directories table. ⤶ <example>⤶ <code> ⤶ local _, dirs = file.Find("path/to/dir*", "LUA")⤶ if dirs != nil and table.HasValue(dirs, "dir") then print("Directory found!") end⤶ </code> ⤶ </example>⤶ </bug> ⤶ <bug issue="1038"> This will sometimes return false clientside for directories received from the server via a clientside lua file. You can work around this by using <page>file.Find</page> with the path to the directory followed by a wildcard (no trailing forward slash) and see if the directory is inside the returned directories table (**see second example)**.</bug> </description> <realm>Shared and Menu</realm> <args> <arg name="name" type="string">The file or directory's name.</arg> <arg name="path" type="string">The path of where to look for the file.⤶ * `GAME` Structured like base folder (garrysmod/), searches all the mounted content (main folder, addons, mounted games etc).⤶ * `LUA` or `lsv` - All Lua folders (lua/) including gamesmodes and addons.⤶ * `DATA` Data folder (garrysmod/data).⤶ * `MOD` Strictly the game folder (garrysmod/), ignores mounting.</arg>⤶ <arg name="gamePath" type="string">The path to look for the files and directories in. See <page text="this list">File_Search_Paths</page> for a list of valid paths.</arg>⤶ </args> <rets> <ret name="" type="boolean">Returns true if the file exists and false if it does not.</ret> <ret name="" type="boolean">Returns `true` if the file exists and `false` if it does not.</ret> </rets> </function> <example> <description>Prints whether the `data` folder exists in the base directory.</description> <code>print( file.Exists( "data", "GAME" ) )</code> <output>true</output> </example>⤶ ⤶ <example>⤶ <description>Workaround for issue [#1038](https://github.com/Facepunch/garrysmod-issues/issues/1038).</description>⤶ <code> ⤶ local _, dirs = file.Find( "path/to/dir*", "LUA" )⤶ if dirs ~= nil and table.HasValue( dirs, "dir" ) then print( "Directory found!" ) end⤶ </code> ⤶ </example>