This seems like an easy question, but I can't seem to get how to do this. Here's my situation.
I made an executable file in a folder in my PATH. For simplicity's sake, let's say that it is named create_hello and looks at the place from which it was called and creates a file named hello.txt. The file-creation is handled by the EXE. The problem is getting the place from which the EXE was called.
Here's an example.
root
|- folder
Imagine I'm calling create_hello from the command line in root/folder. This is what I expect to see.
root
|- folder
|- hello.txt
I've tried accessing the environment arguments passed to the EXE file, but it shows the path where the EXE file is located, not from where the EXE file was invoked.
I'd like it to work in mainly Windows.
I don't think the language is relevant here, but if it turns out to be relevant, I'm using Rust.
std::env::current_dir returns the current working directory:
directly from current_dir doc page:
use std::env;
// We assume that we are in a valid directory.
let path = env::current_dir().unwrap();
println!("The current directory is {}", path.display());
Related
Have PowerShell script in resource folder and want to use this script in shared library .groovy script which is under vars folder. Is it there a way to do so??
To run a PS script from the resources dir of your shared library, assuming psScript1.ps1 is in your-shared-library/resources/:
powershell "${libraryResource 'psScript1.ps1'}"
This works great, however, it doesn't let you pass in parameters. I can't seem to find a one-line way to do it.
Building on #christopher's answer, here's a two-line solution:
writeFile file: 'tempFile.ps1', text: "${libraryResource 'psScript1.ps1'}"
powershell './tempFile.ps1 -yourParam $yourParam'
In my testing, I found the ./ to start the temp file path to be required.
You can use libraryResource(file-in-resources) (docs) to load a file from the library resources as a text. That you can write to your workspace with writeFile (docs) and execute.
As the library is checked out somewhere next to your workspace you can maybe execute it directly.
I am completing Zed Shaw's Learn Python the Hard Way and am stuck on a concept in the appendix command line crash course in Windows PowerShell.
My problem is with the move (mv) command, specifically moving a file farther back in the path (hope that makes sense). Here is what I did:
I created a directory called temp, and within that directory created a .txt file called awesome.txt and another dirctory called newplace. Then, I write the command "mv awesome.txt newplace" and the awesome.txt file is moved to the directory newplace. Great!
The problem is that I want to move the file awesome.txt back to its original place in the directory temp. When I change my working directory to the directory newplace "cd newplace" and then type "mv awesome.txt temp" the file awesome.txt does not move back to the directory temp, but instead converts from a .txt file to a "file" and stays put in the newplace directory.
Folders like this are nested inside each other:
c:\temp
c:\temp\newplace\
When you cd around, you go into a folder (, e.g. cd temp:
c:\temp\ (o_o)
c:\temp\newplace\
And you can only see things in the same folder you are in. So you can move into newplace because that name makes sense where you are. But when you are in newplace
c:\temp\
c:\temp\newplace\ (o_o)
You can't move to temp because you don't know where it is. You don't have an index of every directory name on the entire computer that you can shortcut to, you only have two options: something in the same place you are, or something with an absolute path - a full name of where it is. c:\temp\.
So mv awesome.txt temp tries to put it inside temp where you are -> c:\temp\newplace\temp\ -> that doesn't exist, so it assumes you're moving it to a new name in the same place.
You would need mv awesome.txt c:\temp\ to specify it properly.
Except there's a sneaky shortcut, anywhere you are, there is automagically a path called .. which means the folder one <-- thatway from where I am.
So you could mv awesome.txt ..\ to push it back one level in the path, without needing to know exactly where that is. This is probably what Zed Shaw is expecting.
I am currently trying to write a batch program that installs a module named SetConsolePath.psm1 at the correct location. I am a beginner with Batch and I have absolutely no powershell experience.
Through the internet, I have learned how to display PSModulePath with powershell -command "echo $env:PSModulePath.
How can I, via .bat file, move SetConsolePath.psm1 from the desktop to the location displayed by powershell -command "echo $env:PSModulePath?
Thank you in advance, and I apologize for my lack of experience.
Before I answer, I must out that you do not want to copy PowerShell module files directly to the path pointed by PsModulePath. You really want to create a folder inside PSModulePath and copy the files there instead.
The prefix env in a Powershell variable indicates an environment variable. $env:PSModulePath is actually referring to the PSMODULEPATH environment variable. On the command line, and in batch files, environment variables can be displayed by placing the name between percent symbols. (In fact, you could have displayed this value by typing echo %PSMODULEPATH% instead.)
To reference the desktop folder, have a look at this answer, which shows you how to use another environment variable, USERPROFILE.
Therefore, to copy the file from the desktop directory to the path specified in PSModulePath, you would do this:
COPY "%USERPROFILE%\Desktop\SetConsolePath.psm1" "%PSMODULEPATH%"
And, as I warned earlier, you really should copy the file to a folder underneath PsModulePath. So what you really want is:
IF NOT EXIST "%PSMODULEPATH%\MyNewFolder" MKDIR "%PSMODULEPATH%\MyNewFolder"
COPY "%USERPROFILE%\Desktop\SetConsolePath.psm1" "%PSMODULEPATH%\MyNewFolder"
So i had this issue that occurred when I ran a Matlab script. Here is an a simple example that illustrates it:
So its important to outline the file structure:
MainFolder
script.m
SubFolder
a1.csv
a2.csv
a3.csv
now say i have a script like this:
-> script.m
dir
it would simply print out the files in the folder.
Now the wierd thing, if i run the script in the Subfolder like this:
>>script
it will do this:
>> a1.csv a2.csv a3.csv
but if i do this in the folder:
>>run('C:\Users\....\MainFolder\script.m')
it will only print out
>> script.m
So obviously it is acting as if i ran it form MainFolder rather than SubFolder.
What is the point of this functionality?
The dir command shows the directory contents of Matlab's current directory, not that of where the script is located. So the script showed you the directory contents of wherever you happened to be in the Matlab command prompt when you called that script.
To get what you want, use this in the script:
dir(fileparts(mfilename('fullpath')))
Use pwd to see current dir
Use cd to change directory
Use path to see if your project folders are included in the path
Use which to see you are calling the right *.m file (in case there is multiple .m files with same name on the path)
Merry Christmas to everybody. I'm having a dilemma with a perl script. In my script, I call another program with a system call, but I got this error:
Can't exec "./Classificador/svm_classify": No such file or directory at Analise_de_Sentimentos_mudanca.pl line 463.
I don't know if there is a problem in having my program in a different directory than the called program.
Another curious thing is that this script used to run normally in Ubuntu 10.10. But now I've changed to Mint 14. Is it missing some library?
Best wishes,
Thiago
The relative pathname ./Classificador/svm_classify is interpreted relative to the user's current directory, not the directory containing the perl script. You need to do one of the following:
The user must cd to the directory containing the perl script before running it.
The perl script should call chdir() to set the current directory to the directory where it's stored.
Put the absolute pathname in the script, instead of ./.
Does this "./Classificador/svm_classify" exists ?
Check the following :
1) to go the directory where this file lays - Analise_de_Sentimentos_mudanca.pl
2) run :
ll Classificador/svm_classify
3) show us the results