Loops in the Batch files with examples

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed). Previous article: Replaceable parameters in batch file.

WinXP-dir-p

The very simple loop in the batch script can be created by using GOTO command and a label. The syntax is – GOTO followed by the name of the line/label. The label is defined by the colon character at the beginning of the line. The label can be above or below GOTO statement. The code fragment below shows example of infinite loop.

:: LOOP.BAT
:: A batch file example using a infinite loop.
:TOP
DIR
GOTO TOP

The first two lines are remark statements giving the details of the program including the filename and a brief description of the program. The third line marks the line (creates a label) that a GOTO command can possibly link to. The fourth line will perform a directory listing. This is just an example command to illustrate a task that can be performed using the batch loop. You might like to indent the DIR command to emphasize that it is inside a loop. The last line is the command to jump to the line with the name TOP and to continue processing. Since there is no IF statement to test some condition and possibly to jump out of this loop, the batch file will continue to run indefinitely or until you press the CTRL+C or the CTRL+Break combinations on your keyboard.

Loop.bat output under Windows 8.1, Ctrl+C pressed
Loop.bat output under Windows 8.1, Ctrl+C pressed

:: LOOP2.BAT
:: Loop reads multiple replaceable parameters
:: with a SHIFT command inside the loop.
@Echo Off
:TOP
Copy %1 E:\
Shift
Goto TOP

LOOP2.BAT is a more useful program that copies a list of files specified after batch file to the E: drive. When the LOOP2.BAT program runs out of files to copy, the program will not stop. The batch script will attempt to continue until you use CTRL+C or CTRL+Break to terminate the program. To stop the batch script automatically, you need to add IF statement, but we will not discuss it here.

The first three lines are remark statements indicating the name of the batch file and a brief description of the batch file. The fourth line turns off command echoing. Remember that the @ sign turns off the echoing of the ECHO OFF command. The fifth line is the label that the GOTO command will jump to. The sixth line copies the file defined by the first replaceable parameter to the E: (usually flash drive) drive. The seventh line utilizes the SHIFT command to replace %0 with %1, %1 with %2, and so forth. The last line, GOTO TOP, is used to jump to the line labeled TOP and continue processing. Obviously the downside to this looping structure is that it needs more control. As mentioned above, one way to add this control is through the IF statement.

Next article: IF statement in DOS batch file.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.

Replaceable parameters in batch file

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: Stopping a DOS batch file.

console-wishmesh
Photo: freeimages.com

With the information covered so far we are able to use batch files to make our lives easier, and help our day to day computing operations. However, we could make our batch scripts more sophisticated by avoiding the big limitation that we have been faced with so far. That is – our files are hard-coded (written in stone, so to speak) and if we want to manipulate a file, we have to change the batch script again and again.

What if we wanted to perform a particular operation on a different file each time? We can use replaceable parameters to help us with this task. The two important factors in using replaceable parameters are – how to code them in the batch file, and second – how to give the values for the replaceable parameters to the batch file. The latter is rather quite easy. Replaceable parameters are to be entered after the batch file name and before pressing the Enter key. For example:

C:\>batch-params.bat Par1 Par2 Par3 Par4 Par5

Either a space or a comma character separates each parameter. Both are treated the same. Now, how many replaceable parameters does the above line have? The obvious answer would be five. This would be wrong. DOS counts the name of the batch file itself as a parameter, so there are 6 replaceable parameters on this line. Much like other programming languages DOS names them %0, %1, %2, %3, %4, %5 rather than %1 through %5.

DOS uses replaceable parameters as if they do not exist – for example, everywhere you see a replaceable parameter, the DOS sees the value of that parameter. When a batch script executes with ECHO ON, the DOS precedes each command in the batch file prompt. DOS treats the commands in the batch file exactly like it would if you entered them from the DOS prompt.

:: batch-params.bat
:: Example with Replaceable Parameters.
ECHO %1
ECHO %2
SHIFT
ECHO %2

The first 2 lines are remark statements that show the name of the file and a brief description of the program. The third line echo’s the first replaceable parameter entered on the command line. If replaceable parameter was not entered, the command will display the Echo is on message (just like with simple ECHO command). The fourth line shows the 2nd parameter. The fifth line shifts the argument. The line 6 acts like line 4, but because of SHIFT, it and echo’s third argument. SHIFT command allows to use more than 10 parameters / arguments in the batch file:

SHIFT changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one. In other words, the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. More in MS Help site.

The SHIFT command discards the first parameter %0, moves the remaining parameters down one value, and brings in a new value if one exists. Therefore after the SHIFT command gets read, the value in %1 is moved into %0, and the value in %2 moves into %1 and so on and so forth.

The SHIFT command really has two major purposes. First, by moving the parameters down into a lower replaceable parameter, a single replaceable parameter may be used for all coding by forcing the batch script to loop through the code. Second, it allows the batch file to handle more than nine replaceable parameters. The total number of replaceable parameters is constrained by the 127-character command line limitation (for old OSes), and starting Windows XP or later you can use longer command line – up to 8191 characters. A space, comma or semicolon is also a requirement to separate each replaceable parameter.

If you start our example batch file with the parameters: text1, text2, text3 and text4, you will see the following output:
C:\>batch-params.bat text1 text2 text3 text4
C:\>ECHO text1
text1
C:\>ECHO text2
text2
C:\>SHIFT
C:\>ECHO text3
text3
C:\>

And, note: we do not access fourth parameter (%4), so it is not shown in the console. Also, see the screenshot from Windows 8.1 CMD.exe:

Showing output example from the batch file with parameters
Showing output example from the batch file with parameters

Next article: Loops in the Batch files with examples.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.

VMware vSphere Client could not connect to vCenter version 5.1.0

Recently we had an issue, where we were unable to connect to one of our VMware ESXi vCenter’s (v5.1.0) with vSphere Client. Everything worked, all virtual machines were up, except vCenter.

Digging deeper, the log files revealed the following:
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] SQL execution failed: cleanup_events_tasks_proc
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] Execution elapsed time: 69482 ms
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] Diagnostic data from driver is 42000:1:9002:[Microsoft][SQL Server Native Client 10.0][SQL Server]The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases
2014-09-19T09:01:09.494+03:00 [02156 warning 'Default'] [VdbStatement] Bind parameters:
2014-09-19T09:01:09.494+03:00 [02156 error 'Default'] [Vdb::IsRecoverableErrorCode] Unable to recover from 42000:9002
2014-09-19T09:01:09.494+03:00 [02156 error 'Default'] [VdbStatement] SQLError was thrown: "ODBC error: (42000) - [Microsoft][SQL Server Native Client 10.0][SQL Server]The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases" is returned when executing SQL statement "cleanup_events_tasks_proc"
2014-09-19T09:01:09.494+03:00 [02156 error 'vpxservicesvpxdEventTaskCleanup'] [VpxdEventTaskCleanup] Error during Event/Task Cleanup: "ODBC error: (42000) - [Microsoft][SQL Server Native Client 10.0][SQL Server]The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases" is returned when executing SQL statement "cleanup_events_tasks_proc"
2014-09-19T09:01:09.494+03:00 [02156 warning 'VpxProfiler'] VpxUtil_InvokeWithOpId [TotalTime] took 69482 ms

So we need to clean up the Transaction log for the MS SQL server, or clean up some tables… so we found an article from VMware support – KB1025914.

From VMware, to purge the data in the VPX_EVENT table:

1. Connect to Servername\SQL Database and log in with the appropriate credentials.
2. Click databases to expand and select VIM_VCDB > Tables.
3. Right-click the dbo.VPX_PARAMETER table and select Open.

Note: If you are using SQL Server 2008, right-click the dbo.VPX_PARAMETER table and click Edit Top 200 Rows.
4. Modify event.maxAge to 30, and modify the event.maxAgeEnabled value to true.
5. Modify task.maxAge to 30, and modify the task.maxAgeEnabled value to true.

Note: To improve the time of the data cleanup, run the preceding steps in several intervals. To do this, ensure to keep the default value of event.maxAge and task.maxAge and perform step 6 to run the cleanup. Then, reduce the event.maxAge andtask.maxAge value by 60 and run the cleanup. Repeat the steps until the value is reached to 30 for the final cleanup process.
6. Run the built-in stored procedure:
a. Go to VIM_VCDB > Programmability > Stored Procedures.
b. Right-click dbo.cleanup_events_tasks_proc and select Execute Stored Procedure.

This purges the data from the vpx_event, vpx_event_arg, and vpx_task tables based on the date specified for maxAge.
c. When this has successfully completed, close SQL Management Studio and start the VMware Virtual Center Server service.
To ensure that the default Statistics Level is set to 1:

1. Using the vSphere Client, log into vCenter Server as an administrator.
2. Go to Administration > vCenter Server Settings > Statistics.
3. Under Statistics Intervals, ensure the Statistics Level column is set to 1.
4. To change the value, select the Interval Duration, click Edit and select Level 1 from the dropdown

To download SQL Management Studio 2008 R2 follow this Microsoft SQL Server 2008 R2 RTM – Management Studio Express.

Everything went smooth, except, we didn’t know the SQL server name, and SQL Server 2008 R2 didn’t help much. Later we found, that the name is “.\VIM_SQLEXP”, see the screenshot below.