Include File that is not in the same directory as script - compilation

I have a NSIS script that is attempting to include a .NSI file that sits in a different folder.
My Problem: When I go to compile my script I get the compile error !include: could not find: "../Utilities.nsi". This file exists and is in the correct location I am specifying(in the parent directory - one step back).
How can I include a file that sits in another directory? I hope its possible.
!include "../Utilities.nsi" # include error: '!include: could not find: "../Utilities.nsi"'
InstallDir "abc"
Name "def"
OutFile "def.exe"
Section
DetailPrint "Hello World"
SectionEnd

The manual says this about !include:
This command will include 'file' as if it was part of the original
script. Note that if a file is included in another directory, the
current directory is still where the script was compiled from (not
where the included file resides). If the compiler can't find the file
it will look for it in every include directory. See !addincludedir for
more information. If the /nonfatal switch is used and no files are
found, a warning will be issued instead of an error."
Also, the examples in the manual do not use quotation marks -- did you try
removing them? Also, "/" => "\".

Related

Unable to load/require file from Lua running from Atom in Windows

I'm trying to use Atom to run a Lua script. However, when I try to load files via the require() command, it always says it's unable to locate them. The files are all in the same folder. For example, to load utils.lua I have tried
require 'utils'
require 'utils.lua'
require 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua'
require 'D:\\Users\\Mike\\Dropbox\\Lua Modeling\\utils.lua'
require 'D:/Users/Mike/Dropbox/Lua Modeling/utils.lua'
I get errors like
Lua: D:\Users\Mike\Dropbox\Lua Modeling\main.lua:12: module 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua' not found:
no field package.preload['D:\Users\Mike\Dropbox\Lua Modeling\utils.lua']
no file '.\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
no file 'D:\Program Files (x86)\Lua\5.1\lua\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
no file 'D:\Program Files (x86)\Lua\5.1\lua\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua\init.lua'
no file 'D:\Program Files (x86)\Lua\5.1\D:\Users\Mike\Dropbox\Lua Modeling\utils\lua.lua'
The messages says on the first line that 'D:\Users\Mike\Dropbox\Lua Modeling\utils.lua' was not found, even though that is the full path of the file. What am I doing wrong?
Thanks.
The short answer
You should be able to load utils.lua by using the following code:
require("utils")
And by starting your program from the directory that utils.lua is in:
cd "D:\Users\Mike\Dropbox\Lua Modeling"
lua main.lua
The long answer
To understand what is going wrong here, it is helpful to know a little bit about how require works. The first thing that require does is to search for the module in the module path. From Programming in Lua chapter 8.1:
The path used by require is a little different from typical paths. Most programs use paths as a list of directories wherein to search for a given file. However, ANSI C (the abstract platform where Lua runs) does not have the concept of directories. Therefore, the path used by require is a list of patterns, each of them specifying an alternative way to transform a virtual file name (the argument to require) into a real file name. More specifically, each component in the path is a file name containing optional interrogation marks. For each component, require replaces each ? by the virtual file name and checks whether there is a file with that name; if not, it goes to the next component. The components in a path are separated by semicolons (a character seldom used for file names in most operating systems). For instance, if the path is
?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua
then the call require"lili" will try to open the following files:
lili
lili.lua
c:\windows\lili
/usr/local/lua/lili/lili.lua
Judging from your error message, your Lua path seems to be the following:
.\?.lua;D:\Program Files (x86)\Lua\5.1\lua\?.lua;D:\Program Files (x86)\Lua\5.1\lua\?\init.lua;D:\Program Files (x86)\Lua\5.1\?.lua
To make that easier to read, here are each the patterns separated by line breaks:
.\?.lua
D:\Program Files (x86)\Lua\5.1\lua\?.lua
D:\Program Files (x86)\Lua\5.1\lua\?\init.lua
D:\Program Files (x86)\Lua\5.1\?.lua
From this list you can see that when calling require
Lua fills in the .lua extension for you
Lua fills in the rest of the file path for you
In other words, you should just specify the module name, like this:
require("utils")
Now, Lua also needs to know where the utils.lua file is. The easiest way is to run your program from the D:\Users\Mike\Dropbox\Lua Modeling folder. This means that when you run require("utils"), Lua will expand the first pattern .\?.lua into .\utils.lua, and when it checks that path it will find the utils.lua file in the current directory.
In other words, running your program like this should work:
cd "D:\Users\Mike\Dropbox\Lua Modeling"
lua main.lua
An alternative
If you can't (or don't want to) change your working directory to run the program, you can use the LUA_PATH environment variable to add new patterns to the path that require uses to search for modules.
set LUA_PATH=D:\Users\Mike\Dropbox\Lua Modeling\?.lua;%LUA_PATH%;
lua "D:\Users\Mike\Dropbox\Lua Modeling\main.lua"
There is a slight trick to this. If the LUA_PATH environment variable already exists, then this will add your project's folder to the start of it. If LUA_PATH doesn't exist, this will add ;; to the end, which Lua fills in with the default path.

Calling MkdirAll with reference to root directory instead of working directory?

I'm trying to build a service in go that writes to a file with reference to the root directory as opposed to the working directory.
For example, my working directory is /Users/joe/dev/go and I want to create a file test.txt in the directory /Users/joe/generatedFiles/. I want to write a file /Users/joe/generatedFiles/test.txt.
I can't write to this directory unless the folder generatedFiles is created else I get the error "no such file or directory."
To do this I plan on calling os.MkdirAll("Users/joe/generatedFiles/", os.ModePerm) but this isn't giving me the expected functionality. Instead, it creates the folder: /Users/joe/dev/go/Users/joe/generatedFiles/.
I've also tried calling os.MkdirAll("/Users/joe/generatedFiles/", os.ModePerm) with a slash at the beginning of the file path but this is doing nothing. What can I do to write to a file in reference to the root directory?
Use an absolute path to specify the directory:
os.MkdirAll("/Users/joe/generatedFiles/", os.ModePerm)
Note the leading /.

Failing to open a file which should be in the base path

I have a Go project (bazel-remote) that tries to read a yaml file passed in the command line, when built with bazel. This yaml file sits in the same location from where I run the bazel run command.
But it fails to run because os.Open fails with no such file or directory.
I printed the basePath using os.Getwd, because someone suggested that my basePath might be set wrong. But my basePath is set to a location in my /private/var/tmp/ where the bazel objects are created and stored:
/private/var/tmp/bazel/312feba8ddcde6737ae7dd7ef9bc2a5a/execroot/main/bazel-out/darwin-fastbuild/bin/darwin_amd64_static_pure_stripped/bazel-remote.runfiles/main'
How do I set my basePath correctly? Why is my basePath set to where it is?
Binaries started with bazel run are executed in an internal Bazel directory. They'll have access to "runfiles", which are files mentioned in the data attribute of the binary rule or its dependencies. For example, if you have a rule like the one below, you'll be able to read foo.txt, but not bar.txt or other files:
load("#io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "hello",
srcs = ["hello.go"],
data = ["foo.txt"],
)
Note that the working directory of the binary corresponds to the repository root directory, not the directory where the binary is defined. You can debug with os.Getwd and filepath.Walk.
You mentioned you wanted to access a yaml file passed in on the command line though. Presumably, you want to be able to access any file the user passes in, not just files mentioned in the data attribute. For this case, take a look at the BUILD_WORKING_DIRECTORY environment variable (bazel run sets this). That gives the path to the directory where bazel run was invoked. Also, BUILD_WORKSPACE_DIRECTORY is the path to the workspace root directory.

Perl diruse not not recognized

my program takes input from the user of a file directory via command line in windows , and then runs diruse on specified path to gather information about disk space available. For some reason the error keeps saying "diruse.exe is not recognized as an internal or external command". Below is a section of my code, to view it in its entirety please go here. I took the code out of its sub to see if it would work, but it did not.
&argument_checking; #calling sub checking user arguements.
#&reading_directory; # reads user given directory.
chdir($user_directory), or die " Directory $user_directory does not exist"; #change to user directory or exit.
open(DIRUSE, "diruse.exe /* . |"); #opening user given directory path.
foreach my $directory_lines(<DIRUSE>)
{
chomp $directory_lines;
push(#directory_lines, $directory_lines); #pushing directory lines from file into an array
}
close(DIRUSE); #closing
shift(#directory_lines);
shift(#directory_lines);
pop(#directory_lines);
&chop_and_save;
&gui_creator;
That message comes from the command shell, and it means it can't find diruse.exe in the current directory or in any directory in that process's PATH.

Copy Files in NSIS

I am using the following command to copy files.
After setting output path...
File "Documents\*"
This action works flawlessly. There are no issues coping the files in the Documents directory until...
if there is a copy of an existing file (with a different name) in the directory only the first instance of the file gets copied regardless of name.
How do I make it so it will copy ALL files regardless if they are copies of other files?
Correction/better explanation (maybe)
I apologize for the confusion. Allow me to try to restate the problem. The files being extracted by using the FILE command is the issue here. The files consists of original files and copies of the same files (only with a different name).
example: MyDocument.txt and copyOfMyDocument.txt and so on..
When the File command is applied, in order to be extract the files to the current output path, only the first instance of the file is extracted (either the copy or the original... but not both). Again, I am sorry for the confusing but this is the first time I've had to work with NSIS. I need to extract ALL files.
The easiest way to do this will be to put it in a different directory which you've created. Then, if you need to worry about renaming (as the commentators have noted your question doesn't make much sense), you can attack it file by file.
# Extract the files to a directory which can't exist beforehand
CreateDirectory $PLUGINSDIR\extracting
SetOutPath $PLUGINSDIR\extracting
File Documents\*
# Now go through file by file
FindFirst $0 $1 $OUTDIR\*
${While} $1 != ""
${If} ${FileExists} $DOCUMENTS\$1
# This still isn't infallible, of course.
Rename $DOCUMENTS\$1 $DOCUMENTS\$1.local-backup
${EndIf}
Rename $OUTDIR\$1 $DOCUMENTS\$1
FindNext $0 $1
${Loop}
FindClose $0
SetOutPath $INSTDIR # Or somewhere else
RMDir $PLUGINSDIR\extracting
(Note that that's using LogicLib.)
This doesn't become a very neat way of doing it though and if you can avoid it, do.
i thought i understood what you were after, until i started reading the responses; i'll go with my initial interpretation: given a directory named "Documents", with a bunch of files in it (what they're called, and their contents should not matter), you want an installer that will copy the files into some output directory. I've created a test installer for this scenario here, and it works for me. What am I missing in what you're after?

Resources