Pass parameter in bat file to run from desktop short cut - batch-file

I want to run a bat file from a desktop shortcut and pass a parameter to it.
I can run it from DOS with no problem.
When I try to run it from a desktop shortcut, I can enter the parameter, but it does not get passed.
I enter a file name like 20200103.txt and the bat file takes the .txt extension off so that only the first part of the file is used later in the bat file
Here are the first couple lines that I am using in the bat file
SET /I %1 DTE = %1
set %~n1 = %1%
ECHO %1
ECHO %1%
ECHO %~n1%
Is there any way to include a document when I submit a question?

When you are passing an argument for a file in a shortcut, you need to surround it with "" so if you want to pass 20200103.txt as arg 1 to the batch file you would put in the shortcut path\thing\thing\file "20200103.txt". Also, your code may not be working properly because /I is not an option is the "set" command. Like compo said, try checking out set /?

Related

%~1 and %1 Automatically Removing Everything after the first Space

So I am creating a batch file to store a selected folder's path into a text file to refer to it later on using another batch script...
I created registry entries to include a right-click context menu for any folder which triggers this specific batch file.
Basically When you right click a folder and click "Send to Script" it is supposed to copy the whole path / location of the right clicked folder.
To do so I am using the following command:
SET TargetDir=%~1
I also tried using %1and I also tried using the following code with delimiters
FOR /f "delims=;" %%a in ("%~1") do (
#echo %%a
)
The problem is that CMD is automatically trimming everything after the first space and since this is a path I am copying, I want to keep all the spaces and the path as is
Ex. If I use the command on a Folder such as "C:/folder/subfolder" the copyng is done correctly But If I use the command on a Folder such as "C:/folder/sub folder" the copying is done incorrectly and will only give me "C:/folder/sub" removing all the rest found after the first space detected.
Registry Entries
[HKEY_CLASSES_ROOT\Directory\shell\send-to-script]
"MUIVerb"="Send To Script"
"SubCommands"="sendscript"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\sendscript]
#="Send To Script"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\sendscript\command]
#="C:\\scripts\\pathtotext.bat %1"
Thanks for your help
You should change your registry key [...\sendscript\command] to
#="C:\\temp\\blob.bat \"%1\""
And in your batch you should use
REM The DisableDelayedExpansion is for preserve "!" in path names
setlocal DisableDelayedExpansion
SET "TargetDir=%~1"
setlocal EnableDelayedExpansion
(
echo target is !TargetDir!
) > C:\scripts\target.log
if "reseverdFolder" == "!TargetDir!" echo This folder is reserved
The batch file is not correct registered. For example look with regedit on value of registry key:
HKEY_CLASSES_ROOT\rtffile\shell\open\command
The value is displayed as:
"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" "%1"
So there are two arguments which are both enclosed in double quotes:
Argument 0 is the application to run with full path, file name and file extension enclosed in double quotes as path contains a space character.
Argument 1 is the name of the RTF file referenced with %1 passed by Windows Explorer with full path, file name and file extension which of course can also contain a space or any of the characters &()[]{}^=;!'+,`~ which require also enclosing entire argument in double quotes as output by cmd.exe on running it in a command prompt window with cmd /? on last help page.
So you need in your *.reg file used to import into Windows registry:
#="\"C:\\scripts\\pathtotext.bat\" \"%1\""
This string value is displayed in registry editor as:
"C:\scripts\pathtotext.bat" "%1"
And then you can use %1 or %~1 in your batch file as explained by help of command CALL output on running in a command prompt window on execution of call /?.
SET "TargetDir=%*"
Since the parameter apparently being supplied is C:\folder\sub folder then %~1 selects only the first supplied parameter of what cmd sees as two parameters.
%* means "the whole tail"
and echoing %* should show you exactly what cmd is seeing.

Pass filename to windows batch (.bat) script when double clicking so that it will run in octave

I'm new in using batch scripts, and moderately experienced with octave. I have a lot of data files I examine with octave functions and I am trying to set up so that I can double click on files with a custom extension to directly open octave functions. Think "when I double click on this text file, it opens in notepad."
To do this I've written a very basic .bat file and I've associated my .data files to open using this .bat file. The .bat file looks like this:
C:\Octave\Octave-4.2.1\octave.vbs --no-gui --persist --eval myOctaveFunction.m
pause
I've tested it with a hard coded filename inside "myOctaveFunction." but instead i'd like to actually pass the data file name to myOctaveFunction when I double click on the data file. How do I do this? And, are batch scripts even the right way to do this?
Thanks for your help.
Try this batch file, which will echo a few specific items:
#echo off
echo My directory %cd%
echo Batchfile Name %0
echo File to run %1
pause
So %1 parameter will provide you with the filename itself.
You can also use it like this.
#echo off
echo My directory %cd%
echo Batchfile Name %~dpnx0
echo file to run %~dpnx1
pause
So in Short, this should work if you are running this the way I am thinking you do.
C:\Octave\Octave-4.2.1\octave.vbs --no-gui --persist --eval myOctaveFunction('%1')
pause

Passing a string parameter as a runtime Argument

I want to open the particular path through the batch file. And I want to pass one node in path at run time. If I give input Google, I want to pass the string argument here. It will open the path
want to pass one node in path at run time . if i give input Google.i
want to pass the string argument here. it will open the path
C:\Program Files (x86)\google\Common
How can I achieve this?
My batch file:
#echo off
echo test variables
set input =
set /p input ="Choice"
echo C:\Program Files (x86)\%input%\Common
cd C:\Program Files (x86)\"%input%"\Common
pause
There are the following issues in your code:
there is a SPACE between the variable name and the = sign at the set commands, so it becomes a part of the variable name; hence the variable input remains empty/undefined;
there are quotes within the path at the cd command, which are forbidden characters there;
Here is the corrected version:
#echo off
echo test variables
set "input="
set /p "input=Choice"
echo "C:\Program Files (x86)\%input%\Common"
cd "C:\Program Files (x86)\%input%\Common"
pause
I put quotes around the path at the cd command. Although not necessary for cd, many other commands cannot handle paths containing spaces if the "" are missing.
To use the first command line argument instead of user input, remove the set commands and replace %input% by %~1. The ~ ensures that any surrounding quotes are removed. Type call /? in the command prompt for details on this.
Are you saying you want to run your batch file like this.
C:\>mybatch.bat Google
If so then your batch file just changes to this.
#echo off
echo test variables
echo C:\Program Files (x86)\%1\Common
cd C:\Program Files (x86)\%1\Common
pause

how to add switches to commands in cmd

hey guys very simple question, I need to know how to add an extension/switch into a command in cmd.
Also how would I do this to a command that I made? (batch file that is called when typed the name of)
Ex.
Traditional
ipconfig /all
Modifed
ipconfig -a
or
ipconfig /a
This is a simple example to go with the answer above.
#echo off
if /i "%~1"=="-a" ipconfig /all
if /i "%~1"=="/a" ipconfig /all
(don't call your batch file ipconfig.bat - never use system command names for a batch file):
Cmd.exe provides the batch parameter expansion variables %0 through
%9. When you use batch parameters in a batch file, %0 is replaced by
the batch file name, and %1 through %9 are replaced by the
corresponding arguments that you type at the command line. To access
arguments beyond %9, you need to use the shift command. For more
information about the shift command, see Shift The %* batch parameter
is a wildcard reference to all the arguments, not including %0, that
are passed to the batch file.
For example, to copy the contents from Folder1 to Folder2, where %1 is
replaced by the value Folder1 and %2 is replaced by the value Folder2,
type the following in a batch file called Mybatch.bat:
xcopy %1\*.* %2
To run the file, type:
mybatch.bat C:\folder1 D:\folder2
Copied from MSDN
create a batch file mytest.cmd with notepad and add the following
rem start parsing out first parameter indicated with %1
set parm1=%1
set arg1=%parm1:~2%
if "%arg1%"=="a" echo A was the parameter
now arg1 will hold a from your example without the - or /
if you run mytest -a you'll see A was the parameter
if you run mytest -b you won't see a thing...
(as a matter of fact you see every single comand that is in the cmd file which is handy for debugging, try adding #echo off at the first line of mytest.cmd to get rid of the noise)
try at the cmd prompt set /?, if /?, for /? or call /? to learn more about the commands available.

DOS batch argument substring in one line

I have a batch file with the following 2 lines and change them into one line of code:
set arg=%1%
"C:\Program Files\TextPad 6\TextPad.exe" -u "D:\www\%arg:~14,-1%"
The context is that I'm using a webpage url-handler as described on
http://msdn.microsoft.com/en-us/library/aa767914%28v=vs.85%29.aspx
Currently I'm doing it by setting the batch file as the url command, so the %1 is passed into that, then converted and then it runs the text-editor. But I'd rather do it all in the url command, so that I don't have to use the batch file any more.
After much trial & error, I found this works:
cmd.exe /v:on /c set arg=%1& start /D"C:\Program Files\TextPad 6" TextPad.exe "D:\www\!arg:~14,-1!"

Resources