DOS system path - dos

Is there a way to programmatically, through a batch file (or powershell script), put all folders in c:\Program Files into the system variable PATH? I'm dependent on the command line and really want to just start a program from the command line.
Yes, I'm jealous of Linux shells.

Passing in "C:\Program Files" as a parameter into this batch file:
#echo off
FOR /D %%G IN (%1\*) DO PATH "%%G";%path%

Doing this is very likely to break your computer, in the sense of invoking DLL Hell. As you invoke each executable, the OS will look through each directory in PATH to find each DLL or even EXE referenced by that executable. It becomes highly likely that the OS will find the wrong ones as you add more directories to the PATH.
So, a best practice is to avoid increasing the PATH, and even to decrease it. Rather than implicit dependencies, make them explicit.
Instead, I recommend this approach:
Create a bin directory within your user home directory
Add that bin directory to your user PATH variable
Create a Windows CMD script in the bin directory for each application that you want to invoke from the command line (same name as the executable that you would type)
In each script, invoke SetLocal, add the application's install directory (under %ProgramFiles%) to the PATH, then invoke the executable with the arguments from the command line
Remove the relevant directory from the PATH, so that this script becomes the only way to invoke the executable

Related

Get relative path of a parent's subfolder

I had a massive batch file which I split into several smaller ones, with one master file calling each of the smaller files individually. For neatness I put the individual scripts in a subfolder from the project folder (the master script is in the project folder).
This has however has caused an issue - I can't work out how to change some paths in the new individual scripts. Here is approximately what layout is like:
Project
|---MasterScript.bat
|
|---Scripts
| |---scriptA.bat
|
|---Exes
| |---program.exe
| |---config.xml
So the master script calls each of the batch scripts - A and script A calls program.exe with argument /config config.xml.
The issue is how to address program.exe and config.xml.
I unfortunately have just hacked these scripts together without really knowing how batch file paths are resolved, and so have literally no idea how to write relative paths using the parent folder etc. relationships.
So essentially I am asking in general how to write relative batch paths and specifically how to write these paths.
%~dp0 expands to drive and path of argument 0 which is the batch file itself. This file path reference expands always to a path with a backslash. Try it out with a batch file containing only
#echo off
echo Batch file path is: "%~dp0"
pause
How to reference arguments (parameters, options) of a batch file without or with a modifier is described in help output on running call /? in a command prompt window on several display pages.
Short relative path tutorial:
Paths starting with name of a directory or a file name are relative to current directory.
Paths starting with .\ are also relative to current directory.
Paths starting with ..\ reference the parent directory of current directory.
Paths starting with just \ are relative to root directory of current DRIVE.
For example \Windows references the directory Windows in root of current drive independent on which directory is the current directory.
.\ and ..\ can be also used one or more times nearly anywhere within a path. .\ and ..\ can't be used left to drive letter and colon as this would mean a directory in current or parent directory is specified which contains a colon in name which is not possible.
The usage of ..\..\ or even more ..\ in sequence helps to reference a path 2 or more directories upwards in directory structure relative to current directory.
On just running applications it is absolutely valid to use ~dp0..\Exes\program.exe.
It is also no problem to reference other directories or files with such a mixture of absolute and relative path components in arguments passed to an application or script.
But it is often helpful if the real absolute full path is determined from a pure relative path or a mixture of absolute and relative path before passing a directory or file name with path to an application or script for getting better readable warning and error messages containing the passed directory/file name.
The command FOR can be used to get full path of a directory or file from a relative path or a mixture of absolute and relative path.
Example:
#echo off
for /F %%I in ("%~dp0..\Exes") do set "FullExesPath=%%~fI"
echo Path to project executables: "%FullExesPath%"
pause
Note: There is no backslash at end of this resolved path. On using %FullExesPath% in a file reference the backslash must be typed between this directory reference and the file name which makes the string easier to read.
For details on command FOR run in a command prompt window for /? and read carefully the output help pages.

Windows CMD Shorten file path

I got Gnu Utilities that adds the command sed to windows, but to use it I have to type:
C:\ProgramFiles\GnuWin32\bin\sed.exe <args>
How do I shorten this to just sed <args>?
To run an executable without a full path, it needs to be either in the current directory, or in the PATH environment variable. In the CMD prompt, there are several ways to do this.
The first way is to put C:\ProgramFiles\GnuWin32\bin in your PATH variable, which makes every program in that directory available without a full path.
set "PATH=%path%;C:\ProgramFiles\GnuWin32\bin"
This updates PATH in the current command prompt. If you need to set it for other CMD windows, see How to persistently set a variable in Windows 7 from a batch file?
The second method is to have sed.exe in the current directory. The most obvious way to do that is to change directories.
cd C:\ProgramFiles\GnuWin32\bin
sed
Or you can copy it to your current directory.
copy C:\ProgramFiles\GnuWin32\bin\sed.exe .\
sed
(This works with sed.exe because it's a self-contained utility. Don't try this with a Windows application like excel.exe)
Finally, you can create a "redirect" somewhere in the current directory or the path.
>.\sed.bat echo C:\ProgramFiles\GnuWin32\bin\sed.exe %*
This creates a batch file in the current directory called sed.bat that calls the full sed.exe. You can drop this file into any directory in your PATH.
mklink .\sed.exe C:\ProgramFiles\GnuWin32\bin\sed.exe
This creates a symlink to the sed.exe in the current directory, much like a symlink in Unix or a shortcut in Windows.

How do I get my Windows7 symlink to execute from command line?

I have a couple of batch files I want to use regularly, so I decided I would drop them as symlinks into a binaries folder in my path. The idea being that I would use them like I would any other command, without having to change directories. E.g.
> odbimport -u User -f filename
where odbimport is my symlink to the batch file odbimport.bat.
The process I used to make the symlinks is as follows:
C:\Users\user>mklink C:\utils\odbimport C:\util-files\odbimport.bat
symbolic link created for C:\utils\odbimport <<===>> C:\util-files\odbimport.bat
C:\Users\user>path
Path=C:\....;C:\utils\
C:\Users\user>where odbimport
C:\utils\odbimport
From what I've seen, it looks like I've made the symlink, and the path knows where to find it.
However, after I've made my symlink and attempt to execute, I get:
C:\Users\user> odbimport -u me -f somefile
'odbimport' is not recognized as an internal or external command,
operable program or batch file. "
I've been looking for an answer to this with no success. Everything I find seems to deal more with how to create working symlinks than addressing my issue. The closest thing I found was this. This is essentially my question, except kinda backwards because I don't really want to run the symlinks from Windows Explorer. I have also tried adding .LNK to my PATHEXT variable as in this question.
Add .bat extension to the symlink you create because on Windows .bat extension is necessary to tell the system it's actually an executable batch file. You will still be able to run the file by typing its name only.
mklink C:\utils\odbimport.bat C:\util-files\odbimport.bat
NTSF hardlinks can be used when source and target are on the same volume, the advantage is that such clones may be executed from Windows Explorer unlike symlinks:
mklink /h C:\utils\odbimport.bat C:\util-files\odbimport.bat
fsutil hardlink create C:\utils\odbimport.bat C:\util-files\odbimport.bat

How to start an application in a given directory, using other as its working dir?

For a system we are deploying for our customer we need to run the setup executable from %temp% and have it use for the installation, files in another directory.
This cannot be solved at the application level.
So, basically what I need it to somehow "cheat" the setup.exe located at %temp% to think it ran under another directory.
Any ideas?
I tried doing pushd & popd, that doesn't work because the OS tries to call setup.exe from the data files' directory, not setup.exe.
I also tried calling setup.exe by running a bat from the data files directory, that basically calls it by doing:
%temp%\setup.exe
doing:
cd %temp%
setup.exe
also failed
cd %files_dir%
%temp%\setup.exe
But
1) it's up to setup.exe to use current dir or not. So this command sequence can have no effect.
2) current dir can be changed at any moment (e.g. when system Files Open dialog is called).
You can try to create shrtcut of files in %temp% dir and use them instead of files. Maybe you'll need to play around with file extensions.

Configuring Cyqwin to make using ./ unnecessary

In order for me to run a .exe or a .sh in Cyqwin, I have to put a ./ at the beginning of my line. Is there a way for me to change this so that isn't necessary? This is causing problems when I try and run a test script and it can't find files that are right in the directory I'm working in.
The reason you must use a './' is to specifically tell the shell what file you are trying to execute. Without the leading './', your $PATH environment variable is searched. You can try adding the directories with scripts and executables you commonly use to your $PATH if you'd like. Alternatively, you can add the current directory (.) to your $PATH, but this is a Very Bad Idea as it can lead to unintentional executions.
From the cygwin environment variables doc:
The PATH environment variable is used by Cygwin applications as a list
of directories to search for executable files to run. This environment
variable is converted from Windows format (e.g.
C:\Windows\system32;C:\Windows) to UNIX format (e.g.,
/cygdrive/c/Windows/system32:/cygdrive/c/Windows) when a Cygwin
process first starts. Set it so that it contains at least the
x:\cygwin\bin directory where "x:\cygwin is the "root" of your cygwin
installation if you wish to use cygwin tools outside of bash.

Resources