How to use the dot command (.) in my custom command? - batch-file

I have written a simple batch file mycmd.bat, to open up explorer. I've added the file in the appropriate path. The content of the file is:
explorer
Now I want to use this bat file to open up any directory. So I navigate to the required directory and run my command:
D:\MyFolder>mycmd .
If I have Visual Studio Code installed then if I wanted to open the directory with VS Code then I'd do:
D:\MyFolder>code .
I want similar approach for my custom command.
Now if I run D:\MyFolder>mycmd . it opens up my Libraries folder. Not the folder I am at right now which is D:\MyFolder.
(For me Libraries is the default folder that opens up when I open explorer by clicking the yellow explorer icon at my taskbar.)

You do not need a batch-file for that. Navigating to the directory and typing (into the commandline I assume you to take to navigate) explorer . opens the explorer view of that folder.
An alternative if you are running this in a batch-file you could also use explorer %cd%. The environmental variable cd stores the path of the current callers (Thanks for the correction by WasteD!) directory.
So it uses the value from where you called the file i.e. the directory you are currently in or after you used cd anotherFolder it will use this new path.
To change your "custom command" you could also add "%~1" to the same line explorer currently stands in:
explorer "%~1"
This will take the first argument of your batch-file -> in this case . without potential surreounding quotes and add it after the explorer command as first argument.

Related

How to fix my batch file to open the appropriate program?

I'm attempting to write a batch file that will move to the specified directory and then run the command to open my desired program. Specifically I want it to run the command HardwareSimulator so it will open the software nand2tetris provides.
I've gotten it to move to the directory I want, but the opening is my issue. Code is displayed below. I'm guessing start isn't the correct command since when I run, it just runs an infinite loop of opening cmd prompts.
My second question would be: can I only go into sub-directories of where my batch file is already stored? It would be easier to store it in my desktop, so I can just click it whenever, but I can't seem to make it back out of a directory and then go down into another.
start cmd
pushd \nand2tetris\projects\P1Codes
start HardwareSimulator
pause
You can use ..\ to go back a directory.
For instance, if you had nand2tetris in your downloads folder you could get to it from the desktop with this script. Also make sure to to include the file extension.
pushd ..\Downloads\nand2tetris\projects\P1Codes
start HardwareSimulator.exe
pause

Command Line Installation with SCCM 2012

I have a few applications that I am trying to deploy with SCCM 2012 but the installations are failing through the application catalog. So what I have for the deployment type is a script installer. I have "cmd.exe" (Without quotations) in the Installation program field and "Installer.bat" in the installation start in field.
When I look at the ccmcache folder, all the contents over that application are there but the following error displays the Software Center:
0x8007010B(-217024629)
I have done some reading online and the "10B" is a common command line error for invalid directory. I have tested the batch file when hard coding a path but my question is, how can I edit the batch file or SCCM to pull from the CCMCache path where the files are downloaded to on the local client? Currently the Batch File is simply:
#echo off
ApplicationName.exe
Do I need to edit the file to cd into the CCMCache folder where the files are placed? How can I get the batch file to run the executable that is downloaded to the CCMCache folder?
Thank You!
You need to have the full path to the installation in your script
#echo Off
\\path to .exe
The way the command is written will not be able to find the .exe file. You need to add the full unc path to the .exe into your .cmd file. You should have your installation .exe and .cmd file in the same location on the distribution share
Recommended Solution:
Before starting, since you are only launching an exe with your batch file, I would recommend just using your ApplicationName.exe as your command line parameter in SCCM instead of using a batch. This will eliminate the need to engineer any further.
Modifying the existing solution to work:
If you do still want to use a batch file, keep a few things in mind. The syntax you are using to launch the batch file will not work. I would recommend just using the batch file name "installer.bat" as your command line. If you still want to preface the batch with the cmd.exe, you absolutely need to use the /c switch with it
cmd.exe /c installer.bat
If you don't use /c the console host will only open to a promopt and not execute your batch.
This is not an ideal solution though because using "cmd.exe /c" will set your working directory to the location of cmd.exe (ie "C:\windows\system32") and since your content is staged in ccmcache, you will need to specify its location within your batch. To do this, you would use %~dp0 variable which gives you the directory the current batch is running from. This means modifying your batch to read
#echo off
%~dp0ApplicationName.exe

How to get Visual Studio to recognise a network drive mapping

I have the following Post-Build event defined in my project:
if "$(ConfigurationName)"=="Release" ("$(ProjectDir)PostBuildRelease.bat" "$(TargetDir)")
So when I build the project in release mode, the following .bat file is executed:
PostBuildRelease.bat
CMD
SET parameter=%1
CD %1
ECHO "Copying temporary file..."
COPY FileDeleter.exe temp.exe
ECHO "Merging dependancies..."
"..\..\ILMerge.exe" /out:"FileDeleter.exe" /targetPlatform:"v4" "temp.exe" "Microsoft.WindowsAPICodePack.dll" "Microsoft.WindowsAPICodePack.ExtendedLinguisticServices.dll" "Microsoft.WindowsAPICodePack.Sensors.dll" "Microsoft.WindowsAPICodePack.Shell.dll" "Microsoft.WindowsAPICodePack.ShellExtensions.dll"
ECHO "Removing temporary file..."
DEL temp.exe
All it does is copy the assembly to a temporary location, merge the required dependancies into a final executable FileDeleter.exe, and then removes the temporary file.
This worked fine when the project was saved to a local drive, but after moving the project to a network drive, I get these errors when building in Release mode:
'\\file\IT\Internal Apps\AppDev\Applications\WpfFileDeleter\WpfFileDeleter\bin\Release\'
CMD does not support UNC paths as current directories.
C:\Windows>"Copying temporary file..."
The system cannot find the file specified.
"Merging dependancies..."
'"..\..\ILMerge.exe"' is not recognized as an internal or external command,
operable program or batch file.
"Removing temporary file..."
Could Not Find C:\Windows\temp.exe
CMD complains about not supporting UNC paths.
However, I know that the drive in question (\\File) is actually mapped to the Y:\ drive - so I can navigate to the directory in CMD by targeting Y: instead of \\file\.
The question is - how can I get Visual Studio to recognise this drive mapping by default, after all my projects have been moved to this network drive?
I've learned that batch dosnt like going over a network dir unless you add it as a drive with pushhd. This has worked for me like a charm when working over the network.
PUSHD is an internal command. If Command Extensions are disabled the PUSHD command will not accept a network (UNC) path."
Source:
http://ss64.com/nt/pushd.html
I was able to find a solution to this using this answer:
Remove project from Visual Studio solution
Right-click the solution and "Add existing project"
Navigate to the .csproj file, making sure to navigate to it using the mapped drive, (i.e. Y:\folder\test instead of \\file\folder\test)
Now once the project is added, the TargetDir variable will be set using the mapped drive instead of \\file, meaning that no changes are necessary to the post-build event or the batch file.
Another option, as I am using Team Foundation Services in VS, is to remove all existing local git repositories from Team Explorer -> Manage Connection -> Local Git Repositories and then re-adding the same locations, but again when navigating to the folder, use Y:\ instead of \\file.
Now any project I open from these repositories automatically has their TargetDir paths set using the mapped drive.
The other answer here also worked, but requires changing the post-build event to use the hard-coded mapped path as well as changing the batch file to use PUSHD instead of CD.

Can't run program after set path as environment variable

I have set a new environment variable pointing to msbuild.exe folder.
C:\temp\Test>echo %DOT_NET4%
C:\Windows\Microsoft.NET\Framework\v4.0.30319
Now, if I start a new cmd and run "msbuild.exe", the programs runs ok, but after running this simple bat, the program is not found anymore:
"C:\Program Files\TortoiseSVN\bin\svn.exe" checkout %CHECKOUT% %PATH%
cd %PATH%
nuget restore OpenText.sln
msbuild.exe OpenText.sln
msbuild not recognized as an internal or external command.
Thanks in advance
I am seeing the same thing as Joey in his comment. Your use of CD %PATH% is a red flag that indicates a problem.
PATH is a critical environment variable that contains a delimited list of folder paths where important executables are located. The command processor uses that list to locate programs when an external command is issued without the full path to the command.
If PATH is set correctly, then your command CD %PATH% cannot work. But I suspect you have some script that defines PATH to a specific folder, perhaps where nuget is located. In this case, your CD %PATH% command works, but now cmd.exe has no idea where msbuild.exe is located.
Moral of the story - don't ever use PATH for your own purposes. Pick some other variable name that is not reserved.
First things first, where are you setting the msbuild path? To resolve your issues, please check if you have followed this process:
Open your system control panel, check for advanced system settings.
Click on Environment variables and edit PATH
Add a semicolon followed by the msbuild path.
Open a new command prompt and check if it is working or not.
Please note if you set msbuild in one command prompt, it will not be available in another command prompt.
Another issue which I see when I look at your logic is, you are trying to cd to %PATH% which should be avoided, you are either resetting the Environment variable path to a new path and trying to cd to it or you will land up file name longer issues, please use another variable instead.
In the bat which you are invoking, add a condition at the root level, such that if msbuild is not found, basing on %ERRORLEVEL%, try adding msbuild to path again at batch level, so that you build will proceed.

Windows: script/program for USB key that opens a console, and sets the path and the working directory

The goal behind this question is to create a portable Windows script/program that would help users run (console) programs directly from a USB key (not necessarily through autorun, though). The script/program would thus do the following when double clicked on:
Launch a console (cmd would do) and give a DOS command prompt,
Have the current directory set to a specific directory WorkingDir on the USB key (which contains various [Python] programs),
Set the path so that the user can run a command (python.exe) found in another USB key directory (so that the user can launch various Python programs found in WorkingDir).
The whole thing is based on Portable Python, which is on the USB key. I would also like to be able to simply put the contents of the key on a hard drive and use it from there.
I tried to write a file that contains commands like:
PATH=..\"Portable Python 2.7.2.1\App":%PATH%
cd WorkingDir
cmd
but I'm not sure how to call it so that Windows runs it (and I was therefore not able to see whether these command would work).
I don't know much about DOS and Windows, so any help would be much appreciated!
Actually, in windows you use ; for separating paths :) And you shouldn't use .. like that. You can use %CD% to get current directory and then navigate from it. And don't use quotes. Also, you could put # before any command that you don't wish to be echoed to the console.
You can put this into run.bat (this should work :P):
#PATH=%PATH%;%CD%\..\Portable Python 2.7.2.1\App
#cd WorkingDir
#cmd
And then just double click it and it will open commmand prompt just as you want it. Or maybe you can add autorun.inf file to open it automatically.
You need to create two files:
autorun.inf
[autorun]
open=cmd.exe "Python Console" /k autorun.cmd
action=Open Python Console...
autorun.cmd
#Echo Off
CD %~d0\WorkingDir
Path %Path%;%~d0\Portable Python 2.7.2.1\App
The phrase %~d0 represents the drive the command file resides, namely the flash drive's letter (E:).
Now, I encountered two slight hiccups. My USB drive had a hidden, system, readonly autorun.inf file on it already. I had to unprotect it with the following command before I could edit it.
Attrib autorun.inf -r -s -h
My second hiccup, is that Windows 7 won't autorun from a USB drive. You have to right-click the drive in Explorer and select "Run Python Console..."

Resources