how to add switches to commands in cmd - batch-file

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.

Related

Pass parameter in bat file to run from desktop short cut

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 /?

Batch script calling its own name instead of files of a certain type?

cd %cd%
ffmpeg -i %cd%/%04d.png out.mp4
A script with just this in it works completely fine and outputs exactly what I need it to, but:
:: A simple script to convert a png or jpg image sequence to an
mp4 file with ffmpeg
cls
#echo off
title PNG2MP4
color C
echo Ensure you have ffmpeg installed and setup in your environment variables or this script won't work.
:QUERY
echo This will convert all image files in the following directory to a single mp4,
echo %cd%
echo are the files PNGs or JPEGs(PNG/P/JPG/J/CANCEL)?
set/p "ch=>"
if /I %ch%==PNG goto CONVERTPNG
if /I %ch%==P goto CONVERTPNG
if /I %ch%==JPG goto CONVERTJPG
if /I %ch%==J goto CONVERTJPG
if /I %ch%==CANCEL goto :eof
echo Invalid choice & goto QUERY
:CONVERTPNG
cd %cd%
ffmpeg -i %cd%/%04d.png out.mp4
:CONVERTJPG
cd %cd%
ffmpeg -i %cd%/%04d.jpg out.mp4
This more complex version of the script fails, outputting:
C:\tmp/img2mp4.bat4d.jpg: No such file or directory
Why is it no longer calling the files that it did before and is there an easy fix for this?
Here is my suggestion for the batch file:
#echo off
rem A simple script to convert a png or jpg image sequence to an mp4 file with ffmpeg
cls
title PNG2MP4
color C
echo Ensure you have ffmpeg installed and setup in your environment variables
echo or this script won't work.
echo/
echo This will convert all image files in the following directory to a single mp4:
echo/
echo %cd%
echo/
%SystemRoot%\System32\choice.exe /C PJC /N /M "Are the files PNGs or JPEGs or Cancel (P/J/C)? "
if errorlevel 3 color & goto :EOF
echo/
if errorlevel 2 (
ffmpeg.exe -i %%04d.jpg out.mp4
) else (
ffmpeg.exe -i %%04d.png out.mp4
)
color
The character % must be escaped in a batch file with one more % to be interpreted as literal character which was the main problem causing the batch file not working as expected. %0 references the string used to start the batch file which was img2mp4.bat. So %04d.jpg concatenated img2mp4.bat with 4d.jpg and the result was running ffmpeg.exe with img2mp4.bat4d.jpg as file name instead of the argument string %04d.jpg.
To reference one or more files/folders in current directory the file/folders can be simply specified in arguments list of a script or application with no path. This is explained in Microsoft documentation about Naming Files, Paths, and Namespaces. This page describes further that on Windows the directory separator is the backslash character \ and not the forward slash / as on Linux and Mac. / is used on Windows mainly for options as it can be seen on line with command CHOICE because of this character is not possible in file/folder names. - is used on Linux/Mac for options which is possible also in file/folder names even as first character of a file/folder name. So on Windows \ should be always used as directory separator although the Windows kernel functions for file system accesses automatically correct / in file/folder names to \.
CHOICE is much better for prompting a user to take a choice from several offered options than the command SET with option /P. set/p is syntactically not correct at all because of command set should be separated with a space from option /P which should be separated with a space from next argument variable=prompt text. set/p forces cmd.exe to automatically correct the command line to set /p. Batch files should be syntactically correct coded and not depend on automatic error corrections of Windows command processor.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains how to reference batch file arguments.
echo /?
rem /?
cls /?
title /?
color /?
set /?
choice /?
if /?
goto /?
Further I suggest to read:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Single line with multiple commands using Windows batch file
ECHO. FAILS to give text or blank line - Instead use ECHO/
Where does GOTO :EOF return to?

Can someone explain this line of Batch to me?

for /F %%i in ('net view') do copy /Y %0 "%%ic$documents and settingsall usersstart menuprogramsstartup"
Can someone explain this line of Batch to me?
If you type FOR /? at the command prompt, you get this:
FOR %variable IN (set) DO command [command-parameters]
%variable Specifies a single letter replaceable parameter. (set)
Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.
Then below you see this:
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
This means that FOR /F allows you to execute a command for a file set. Here in your OP specifically, the optional "options" is not used.
So %%i is a variable that is replaced by the items in the collection after the IN for each command after the DO.
('net view') executes the command net view. This basically lists all the computers in the network. You can run it at the command prompt and see.
c$ is the default share for the C: drive on a Windows computer.
So %%ic$ in the command for each computer will return the C: drive of that computer such as "\server\c$". (Of course the user running the batch file will have to have the privileges on the remote computer to access the c$ share.)
%0 means the the current file (the batch file that is executing). Just put ECHO %0 in a batch file and run it and you will see.
After the do comes the command you want to execute for each file.
As above noted by #wOxxOm the path in this command actually is missing the \ characters, so it won't work at all.
With this probably already figured out that this command:
executes the net view command and gets the list of all computers visible on the network
and copies the current batch file that is being executed to the folder on the C: drive on that computer
(Except that it won't do it with the \s missing from the path.)

About Copying Files

I want to copy some files to same destination.
Files which will be copied are listed in a text file.
So, how to read file list from text file and copy via using cmd
command?
I tried this command:
for /f "delims=" %%L in (foo.txt) do copy "%%L" new_folder
Similar question was asked in this website, I know that. When I use this command, files will be copied; but folders which include these files won't be copied.
I want to copy files with their directories.
What should I do? (Sorry for my awful English.)
You use %%L in a batchfile and %L when typing interactivly.
Your command, depending on other factors, should have a path specified for new_folder.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.
--
Ok ,I have solved my problem via searching another cmd command:
for /f "delims=" %%i in (filelist.txt) do echo F|xcopy "-Source root folder-\%%i" "-Destination folder-\%%i" /i /z /y
In spite of the fact that I have solved my problem myself, thanks guys for your helps.
I appreciate it too much!

Windows Batch: What does the "~" do? [duplicate]

This question already has answers here:
What does %~d0 mean in a Windows batch file?
(9 answers)
Closed 8 years ago.
I was just wondering what the "~" symbol does in a batch file, I know that it's associated with variables.
Can I have some examples of what it does?
Thanks.
It depends on the command on which ~ is used as dbenham and Magoo already wrote. In general it means: Get the value of a variable (loop or environment variable) or an argument string passed to the batch file with a modification.
A very simple example:
There is a batch file test.bat with content:
#echo Parameter 1 as specified: %1
#echo Parameter 1 (no quotes): %~1
which is started with
test.bat "C:\Program Files\Internet Explorer\iexplore.exe"
The double quotes are necessary because of the spaces in path.
The batch file outputs:
Parameter 1 as specified: "C:\Program Files\Internet Explorer\iexplore.exe"
Parameter 1 (no quotes): C:\Program Files\Internet Explorer\iexplore.exe
So in this example ~ results in getting value of first parameter without double quotes.
As dbenham already wrote, enter in a command prompt window following commands and read the help output in the window.
help call or call /?
help for or for /?
help set or set /?
On many commands strings with spaces or other special characters – see last help page output after entering cmd /? – must be enclosed in double quotes. But others require that the double quotes are removed like on a comparison of two double quoted strings with command if or the double quotes are not wanted like on using echo.
One more example:
#echo off
if /I "%~n1" == "iexplore" echo Browser is Internet Explorer.
if /I "%~n1" == "opera" echo Browser is Opera.
if /I "%~n1" == "firefox" echo Browser is Firefox.
On this example ~n results in just getting the file name without double quotes, path and file extension from string passed as first parameter to the batch file.
Argument 0 is the currently executed batch file as it can be demonstrated with:
#echo off
echo Batch file was called with: %0
echo Batch file is stored on drive: %~d0
echo Batch file is stored in folder: %~dp0
echo Batch file name with extension: %~nx0
Depends on context which you've not provided.
For instance, if there is a filename in %%a, then %%~za will return the filesize, %%~ta the file date/time. Other times it may be a substring operator, such as %fred:~1,6% (the substring of fred, skipping the first 1 and selecting the next 6 maximum.

Resources