Relative path of a given file from a service application in Delphi - delphi

I have a problem loading a file, as I'm passing a relative path to the function FileExists(Filename: String) and it's returning false, that is, it does not find the file in the directory that I pass.
I have a file named Template.html in the D:\Programming\Delphi\Projects\SendMail directory, and a service written in Delphi whose .EXE is in the D:\Programming\Delphi\Automation directory. I am passing the relative path: .\..\Projects\SendMail\Template.html to FileExists(), but it's returning that the file does not exist.
I think that has something to do with the relative path of a service and the relative path of the application being different. Can anybody help me with this?

As lorenzog said, try specifying the full path.
You can also try to set the currentdir to your likings.
//sets currentdir to your application.exe dir
SetCurrentDir(ExtractFileDir(ParamStr(0)));

You assume that the current directory of the service is the directory the executable is stored in. Call GetCurrentDir to find out the current directory.

My experience has been that services start with a working folder of %SystemRoot%\System32 no matter where the actual executable is located.
The way that I have got around this limitation is to write a registry key during installation of the service (e.g. HKLM\SOFTWARE\MyCompany\MyApp\INSTALL_PATH) that points to what I would like the working folder to be. Then when the service starts, it grabs the data from the registry and uses that value as the base when creating paths to files.

Related

Erlang : exception error: no match of right hand side value {error,enoent} while reading a text file

I am currenly working on an erlang project and stuck in reading the file. I want to read a text file which is in the /src folder where all the erlang and a text file are in the same structure. Then too, I am not being able to read the file despite of specifying file paths. Any help would be appreciated.
start() ->
{ok,DataList} = file:consult("Calls.txt"),
io:format("** Calls to be made **"),
io:fwrite("~w~n",[DataList]).
The data file stores contents like : {john, [jill,joe,bob]}.
Try add folder name to the path or try set full patch to the file:
1> {ok,DataList} = file:consult("src/Calls.txt").
Notes: the error {error,enoent} mean that the file does not exist or you don't have a rights to read/write current file, for this case need set 777 rights or similar.
If you need to use src/call.txt, then this simply means that your IDE (or you) has created a src folder in which the calls.txt file has been placed. At the same time, the IDE is using a path that only includes the top level folder (i.e., the root folder for the IDE project). So src/call.txt must be used in that case. This isn’t a problem with Erlang, or even the IDE. It’s just the way your project is set up.
You can do either of two things. Move the calls.txt file up one level in the IDE file manager, so that it can be referenced as calls.txt, not src/call.txt. You can also just change the path to “calls.txt” before you run it from the command line.
enoent means "Error: No Entry/Entity". It means the file couldn't be found. When I try your code, it works correctly and outputs
[{john,[jill,joe,bob]}]

How can I check the file exists in Network UNC path in ruby?

I have a text box where the user can type the UNC path like '\\server_name\testing\'
How can I check this UNC path exists?
I have tried lot of options from the File and Directory and did not get any clue.
My current code is
def get_files(path)
Dir.entries(path).select {|f| File.file?(f)}
end
When I tried to access get_files('\\server_name\testing\'). I got No such file or directory - No such directory: \\server_name\testing\
When I try to access through windows explorer it exists.
Thanks
Make sure your working directory is set properly, because your path will be considered relative to it. You can use Dir.pwd to get your working directory and Dir.chdir to change it.
\\server_name\testing is path to SAMBA resource, so you can't use standard ruby File IO.
Use smbclient to check path or sambal (wrapper around smbclient).

Trying to read file but not finding the path?

Question/Answer Read a file in groovy into a string is simple, except I get a FileNotFound exception no matter where I move the file, e.g. even to the root directory on a linux box.
If I do:
String cpni = new File('/cpni_p').text
and the file cpni_p resides right in the root directory of the system (i.e. make it as simple as possible), I get a
FileNotFoundException: \cpni_p (the system cannot find the file specified)
How do I fix this? I'm open to direct Java too -- just want to get it to work.
There was a directory level privilege problem.

Relative path in Lua script failing

So, here's one of those too-simple-to-fail bugs that we all hate.
I have a .lua file that, among other things, tries to load an image via Love's newImageData function (and place it into a button):
back_button = buttonmanager.createButton("back", love.image.newImageData("../Images/BackButton.png"), width-200, height-105)
Love fails to load, throwing this error:
./frame.lua:5: Could not open file ../Images/BackButton.png. Does not exist.
I've gone through the stupid-mistakes process ("well, does it actually exist? Where is the folder?" etc) -- the file exists, is in the Images folder, which is one level up -- I can even say "ls ../Images/BackButton.png" from the directory this script sits in, and it outputs BackButton.png as I'd expect.
Is there some weird relative pathing issue I need to watch out for? I tried changing it to an absolute path and it gave me the same error.
The love.filesystem module restricts access to files in certain locations:
This module provides access to Files in two places, and two places only:
* The root folder of the .love-file. (Alternatively a directory).
* The root folder of the write directory.
Is the ../Images directory outside of your game's folder/archive?

Compose path (with boost::filesystem)

I have a file that describes input data, which is split into several other files. In my descriptor file, I first give the path A that tells where all the other files are found.
The originator may set either a relative (to location of the descriptor file) or absolute path.
When my program is called, the user gives the name of the descriptor file. It may not be in the current working directory, so the filename B given may also contain directories.
For my program to always find the input files at the right places, I need to combine this information. If the path A given is absolute, I need to just that one. If it is relative, I need to concatenate it to the path B (i.e. directory portion of the filename).
I thought boost::filesystem::complete may do the job for me. Unfortunately, it seems it is not. I also did not understand how to test wether a path given is absolute or not.
Any ideas?
Actually I was quite misguided first but now found the solution myself. When "base" holds the path A, and filename holds B:
boost::filesystem::path basepath(base), filepath(filename);
if (!basepath.is_complete())
basepath = filepath.remove_leaf() /= basepath;
base = basepath.string();
It works with Linux at least (where it would be very easy to do without boost, but oh well..), still have to test with Windows.

Resources