Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file. You can have up to 9 parameters passed in this way. It can be solved with reading from a temporary file a remarked version of the parameter.
Then reading the rem parameter output from the file, but carefully. After removing the extra characters in param1 , you got it. The "pause" displays what the batch file has done and waits for you to hit the ANY key.
Save that as xx. This batch file takes care of all the necessary parameters, like copying only files, that are newer, etc. I have used it since before Windows. If you like seeing the names of the files, as they are being copied, leave out the Q parameter. A friend was asking me about this subject recently, so I thought I'd post how I handle command-line arguments in batch files.
This technique has a bit of overhead as you'll see, but it makes my batch files very easy to understand and quick to implement. As well as supporting the following structures:. The jist of it is having the :init , :parse , and :main functions. It won't modify the original parameters because the function is being call ed with a copy of the original parameters.
Everyone has answered with really complex responses, however it is actually really simple. This can be very useful for certain variables in a script, such as a name and age. So, if I have a script like this:. Inspired by an answer elsewhere by Jon, I have crafted a more general algorithm for extracting named parameters, optional values, and switches.
Let us say that we want to implement a utility foobar. It requires an initial command. It has an optional parameter --foo which takes an optional value which cannot be another parameter, of course ; if the value is missing it defaults to default.
It also has an optional parameter --bar which takes a required value. Lastly it can take a flag --baz with no value allowed. Oh, and these parameters can come in any order. Then place the batch file in, let's say, the system32 folder, go to your Java class file, right click, Properties, Open with Its not just simple to write but its simple to maintain as well so if later some other person or you read your script after long period of time, it will be easy to understand and maintain.
You can also set some environment variables in advance. It can be done by setting them in the console or setting them from my computer :. Of course you can combine all approaches. Check also arguments syntax , shift. The second time the end of the batch file is encountered, the batch script is exited.
You can create a batch program that calls itself. However, you must provide an exit condition. Otherwise, the parent and child batch programs can loop endlessly. To run the checknew. If the parent batch program accepts two batch parameters and you want it to pass those parameters to checknew. I know of several occasions where these seemingly useless "features" proved very handy.
Keep in mind, though, that these "features" may vary with the operating systems used. Windows and XP add even more options. Do note, however, that labels are case sensitive, so you may not want to use this technique for "string type" arguments.
Wrap your strings in quotes or square brackets. This is the least reliable method. So it works with and without quotes, and also with no args. I've been struggling recently with the implementation of complex parameter switches in a batch file so here is the result of my research. None of the provided answers are fully safe, examples:. The only way to ensure all above scenarios are covered is to use EnableDelayedExpansion and to pass the parameters by reference not by value using variables.
Then even the most complex scenario will work fine:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to check command line parameter in ". Ask Question. Asked 10 years, 11 months ago. Active 1 year, 4 months ago. Viewed k times. I always get error: GOTO was unexpected at this time. Can anyone tell me what am I doing wrong here?
Improve this question. Ross Ridge Add a comment. Active Oldest Votes. Improve this answer. Skip R 2 2 silver badges 14 14 bronze badges. This works!! Only "else" is not accepted. I get error: 'else' is not recognized as an internal or external command, operable program or batch file.
So I added another IF for "not equal to -b" case. Thanks for the quick answer.
0コメント