How do I get the directory where the executable is located? - visual-studio-2010

I got the filename like this:
_TCHAR filename[_MAX_PATH];
GetModuleFileName(NULL,filename,sizeof(filename));
How do I remove the filename from this full path? Should I use regex?

You can use the Windows shell API function PathRemoveFileSpec to do this. Example usage is listed on the linked page.

Since you use VS++, you can use:
_splitpath and _wsplitpath functions to break apart path

Related

Is there a way to compile Pascal program and put the generated files in a specific folder?

So I am trying to compile Pascal programs and everything is find; however, I would like to put the generated files after each compilation is a separated folder. I am looking of something like this: fpc "Destination Folder" "program.pas".
Thanks
From Alphabetical listing of command line options
-FE<x> Set exe/unit output path to <x>
-FU<x> Set unit output path to <x>, overrides -FE
So something like fpc program.pas -FEc:\output should work. I don't have fpc installed so I cannot verify. If you try it and get errors that you can't work through post them.
This one works for me:
fpc hello.pas -o"Web/hello.cgi"
I was using ubuntu, notice there is no space between the argument -o and the beginning of the path "Web/..."

Ruby - Search and collect files in all directorys

I'm trying to search for a certain file type within all directories on my unix system using a ruby script. I understand the following code will search all files ending with .pdf within the current directory:
my_pdfs = Dir['*pdf']
As well as:
my_pdfs = Dir.glob('*.pdf').each do |f|
puts f
end
But how about searching all directories and sub-directories for files with the .pdf extension?
Check out the Find module:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/find/rdoc/Find.html
Using Dir.glob is less than ideal since globbing doesn't handle recursion nearly as well as something like find.
Also if you're on a *nix box try using the find command. Its pretty amazingly useful for one liners.
Maybe something like:
pdfs=Dir['/**/*.pdf']
?
Not using Linux right now, so don't know if that will work. The ** syntax implies recursive listing.

RUBYLIB Environment Path

So currently I have included the following in my .bashrc file.
export RUBYLIB=/home/git/project/app/helpers
I am trying to run rspec with a spec that has
require 'output_helper'
This file is in the helpers directory. My question is that when I change the export line to:
export RUBYLIB=/home/git/project/
It no longer finds the helper file. I thought that ruby should search the entire path I supply, and not just the outermost directory supplied? Is this the correct way to think about it? And if not, how can I make it so RUBY will search through all subdirectories and their subdirectories, etc?
Thanks,
Robin
Similar to PATH, you need to explicitly name the directory under which to look for libraries. However, this will not include any child directories within, so you will need to list any child sub-directories as well, delimiting them with a colon.
For example:
export RUBYLIB=/home/git/project:/home/git/project/app/helpers
As buruzaemon mentions, Ruby does not search subdirectories, so you need to include all the directories you want in your search path. However, what you probably want to do is:
require 'app/helpers/output_helper'
This way you aren't depending on the RUBYLIB environment variable being set a certain way. When you're deploying code to production, or collaborating with others, these little dependencies can make for annoying debugging sessions.
Also as a side note, you can specify . as a search path, rather than using machine-specific absolute paths.

Checking if a file exists in the user's home directory

How would I, say, determine if the file ~/.my_proj_config exists on any OS in Ruby?
A call to Dir.home is a OS independent way to get to the home directory for the user. You can then use it like
File.exists?(File.join(Dir.home, ".my_proj_config"))
This works in Ruby 1.9, but note that the call to expand_path is required on some systems (e.g. Windows):
File.exists?( File.expand_path "~/.my_proj_config" )
Use the class File and its method exist?.
Take a look at the Pathname class, specifically the realpath function - This will get you the full (expanded) path to your file.
http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/classes/Pathname.html#M001991
You then use the File class along with exists? method to find out if that exists. You shouldn't need to use realpath if you use this method, however.
http://www.ruby-doc.org/core/classes/File.html#M000045

is there a global function in windows to lists the files in a dir?

which function windows is calling to list the files in a directory ?
thanks
You can use this functions to do a directory listing with the WIN32 API.
FindFirstFile, FindNextFile and FindClose
A full example can be found here
Listing the Files in a Directory WIN32 API
For a portable approach check this post:
"Get list of files in a directory using c/c++"
Didn't exactly get your question but won't using "dir" as system command do the job?
Correct me if I am wrong, coz the question itself wasn't very clear to me.
Edit - Hi John, this is the source code for listing directories.
http://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx
Hope this helps.

Resources