Checking a null variable in a Windows package

I am working on a Windows batch file that will upload three text files to SQL Server. If something goes wrong in production, I want to be able to redefine file names. So I’m thinking about doing something like this.

bcp.exe MyDB..MyTable1 in %1 -SMyServer -T -c -m0 bcp.exe MyDB..MyTable2 in %2 -SMyServer -T -c -m0 bcp.exe MyDB..MyTable3 in %3 -SMyServer -T -c -m0 

I would like to be able to enter default names for all three files that will be used if positional parameters are not provided. The idea would be to fulfill

 myjob.bat 

no parameters and use it by default or execute

 myjob.bat "c:\myfile1" "c:\myfile2" "c:\myfile3" 

and use these files. I could not figure out how to determine if% 1,% 2 and% 3 exist and / or are null. I also do not know how to set these values ​​conditionally. Is it possible? Any suggestions would be appreciated.

+63
windows batch-file
Apr 08 '09 at 18:48
source share
4 answers

To check for the presence of a command line command-line option, use empty brackets:

 IF [%1]==[] echo Value Missing 

or

 IF [%1] EQU [] echo Value Missing 

The SS64 IF page will help you here. In the section "Does% 1 exist?"

You cannot set a positional parameter, so you should do something like

 SET MYVAR=%1 

Then you can reinstall MYVAR based on its contents.

+114
Apr 08 '09 at 18:54
source share

Both answers are correct, but I'm a little different. You might want to consider a couple of things ...

Run the package with:

 SetLocal 

and complete it with

 EndLocal 

This will cause all of your “SETs” to be valid only during the current session and not leave the left vars around the name “FileName1” or any other variables that you set during the run, which may interfere with the next run of the batch file. So you can do something like:

 IF "%1"=="" SET FileName1=c:\file1.txt 

Another trick is that if you provide only 1 or 2 parameters, use the SHIFT command to move them, so the one you are looking for is ALWAYS on% 1 ...

For example, process the first parameter, slide them, and then do it again. This way you are not hard coding% 1,% 2,% 3, etc.

A Windows batch processor is much more efficient than people giving him credit .. I did some crazy things with it, including yesterday’s calculation, even during the month and year, including Leap Year, and localization, etc.

If you really want to be creative, you can call functions in a batch processor ... But this is really for another discussion ... :)

Oh, and do not name your batch files .bat .. They are now .cmd .. heh ..

Hope this helps.

+28
Apr 08 '09 at 19:17
source share

The right thing is to use the if if statement, which is used to check for the existence of a variable. For example:

 IF DEFINED somevariable echo Value exists 

In this particular case, use the negative form:

 IF NOT DEFINED somevariable echo Value missing 

PS: the variable name should be used without the characters "%".

+27
Dec 22 2018-11-11T00:
source share
 rem set defaults: set filename1="c:\file1.txt" set filename2="c:\file2.txt" set filename3="c:\file3.txt" rem set parameters: IF NOT "a%1"=="a" (set filename1="%1") IF NOT "a%2"=="a" (set filename2="%2") IF NOT "a%3"=="a" (set filename1="%3") echo %filename1%, %filename2%, %filename3% 

Be careful with quotation marks, although they may or may not need them in your variables.

+13
Apr 08 '09 at 19:03
source share



All Articles