Articles

MS Dos Batch script stop loop on no error

September 28, 2010 at 07:46:40
Specs: Windows 7

I am trying to create a batch script as below:

1. Scan the number of files inside a predefined directory and initilize the number to a variable.
2. Create a loop which will execute a command a number of times base on the variable initilized.
3. Loop will only stop when the command is successful executed with no errors shown on the MS Dos screen.


See More: MS Dos Batch script stop loop on no error

Report •


#1
September 28, 2010 at 09:53:27

Your question must be better defined about item #3 as the command may execute fine by testing its return code or parsing its output whose format has to be described carefully.

By the way there is no DOS in Windows 7 since it is a NT kernal system and to code your batch you need to use NT batch commands interpreted by cmd.exe NOT command.com.


Report •

#2
September 28, 2010 at 20:25:29

Yes thanks for correcting. I was trying to refer to cmd.exe. With regards to #3, I am expecting the command executed to always return the code" Cannot add ......". It will only be successful when no return code is shown in the cmd screen and thats when the loop will stop

Report •

#3
September 29, 2010 at 07:15:45

@echo off & setlocal

set _DIR=Here the Predefined Directory

cd %_DIR%
for /F %%j in ('dir /B /A-D ^| find /C /V ""') do set _CNT=%%j
:LOOP
for /L %%j in (1 1 %_CNT%) do THE_COMMAND | find /I "Cannot" || goto :EOF
goto :LOOP

The :EOF label must not be declared since it is the standard exit point for NT batch.

If you want to exclude the hidden files from the computed number replace /A-D with /A-D-H.

Be aware the output string to the console wimdow (the believed DOS screen) must hold the word "Cannot" as "Can not" or "Can't" will not be catched.


Report •

Related Solutions

#4
September 30, 2010 at 02:59:29

First of all, thanks for the kind help. I listed down the exact intention that I have as below:

01. Set working directory DIR1 to "D:\Keys\" ----> This dir will have many files inside
02. Set program directory DIR2 to "C:\Program\avp.exe"
03. Using a loop, retrieve the 1st filename from DIR1 and execute the following command:
"C:\Program\avp.exe" addkey "D:\Keys\01.key"
04. The command above must be replace in this format "DIR2 addkey DIR1 Filename.key". The loop will execute repeatedly until no error msg is shown on the cmd screen
05. Script will then pause for 5 secs before exiting on its own.

I tried modifying the code you taught me to this but it doesn't seems to work. Would be be able to guide me further. Thanks.

@echo ON & setlocal

set DIR1 = "C:\Program Files\Kaspersky Internet Security 2011\avp.com"
set DIR2 = "D:\Software Installers\Kaspersky Internet Security\Keys\"

cd %DIR2%
:LOOP
for /F %%j IN ('DIR2 *.key') DIR1 addkey DIR2 %%j | find /I "Cannot" || goto :EOF
goto :LOOP


Report •

#5
September 30, 2010 at 13:40:53

Sorry, I examined carefully and repeatedly what you posted as the real target you want to achieve and the related code (that is obviously absolutely no meaning), but I can't fully understand what the command has to perform nor its syntax.

If I do nnot get the exact syntax of avp.exe , I am unable to understand how to setup the code. More the directories you set in your script do not seem to match with the description you reported.

Sorry but still to confuse, to get the syntax of avg.exe try type at prompt avp /? or avp -h. Don't blame me as I am not Indiana Jones!


Report •

#6
October 1, 2010 at 09:09:28

Sorry for the misunderstanding. Please ignore all the above queries. I will start off by asking in steps instead. Currently I am trying to create a batch script that will count the number of .reg files in a predefined directory. How do I go about it? I am really new to batch script thats why whatever I code above is not readable LOL. Hope you understand. Thanks again!

Report •

#7
October 1, 2010 at 14:48:27

To count .reg files in a predefined directory you may

@echo off

set _DIR=here your directory
cd %_DIR%
for /F %%j in ('dir /B /A-D-H *.reg ^| find /C /V  ""') do set _CNT=%%j
echo. There are %_CNT% .reg files in %_DIR%

Try the above and if OK shoot your next question.

Report •

#8
October 1, 2010 at 23:33:51

Thanks! Here is the 2nd question. I have a folder with many .key files and nothing else. I need the script to do the following:

To be done in a loop till %counter% is equals to %_CNT%. Inside the loop will do this:
1. Retrieve the filename of 1st file in %_DIR% and initilize %fileName% to filename.
2. Execute a command with this syntax - c:\aes.exe addkey %_DIR%%_fileName%
3. Once the command has been executed, the result displayed on the cmd screen must be store inside a variable say %result%.
4. Using a IF statement, check if %result% contains "Cannot add key file......" and if it does, delete the file with the filename from %fileName% before looping again.
5. If the %result% contains nothing, exit out of the loop.


Report •

#9
October 2, 2010 at 04:09:55

I need the following explanations:

1) Assumed %_CNT% is set by counting .reg files in %_DIR%, how do I set the value of %counter%?

2) How are .key files and their folder related to the requested processing?

3) Which command? What you report looks like the "command tail" of the application (avp.exe?) not its operating code (with full pathname!).

4) Does the file that fails have to be DELETED from its folder or just ignored selecting the next one in the loop?

5) Does the loop to be ended as soon as the first succesful match is found?

In this game I am playing the role of the Artificial Intelligence of the computer that needs algorithmic instructions, but A.I. needs exaustive and precise directives too.

This is the spice of scripting/programming.

By the way since the thread is becoming too long and diving I suggest you contact me by mailing using the private message system. To switch to e-mail channel is more efficent for a private tutorial as this is. Not to say I live in Milano (Italy) and a noticeable time lag seems to divide us.


Report •

#10
October 10, 2010 at 09:28:57

warriors says:

I have a folder with many .key files and nothing else. I need the script to do the following:

To be done in a loop till %counter% is equals to %_CNT%. Inside the loop will do this:
1. Retrieve the filename of 1st file in %_DIR% and initilize %fileName% to filename.
2. Execute a command with this syntax - c:\aes.exe addkey %_DIR%%_fileName%
3. Once the command has been executed, the result displayed on the cmd screen must be store inside a variable say %result%.
4. Using a IF statement, check if %result% contains "Cannot add key file......" and if it does, delete the file with the filename from %fileName% before looping again.
5. If the %result% contains nothing, exit out of the loop.

I think IVO has provided excellent help. I am providing an alternate script in case it is needed.

# Script Key.txt
# Input directory
var str dir
cd $dir
# Collect a list of .key files in $dir.
var str list, file, output
lf -n "*.key" > $list
# Loop through the file list.
while ($list <> "")
do
  # Get the next file name.
  lex "1" $list > $file
  stex "^/^l]" $file > null
  # Execute c:\aes.exe addkey $dir $file command.
  system "C:/aes.exe" " addkey " (" \"+$dir+"\"") (" \"+$file+"\"") > $output
  # Does $output contain "Cannot add key file" ?
  if ( { sen -c "^Cannot add key file^" $output } > 0 )
    # Delete the file.
    system del ("\""+$file+"\"")
  else
    # Exit.
    exit 0
  endif
done


Script in biterscripting. Copy and paste the script into file "C:/Scripts/Key.txt". Copy and paste this command into biterscripting.


script "C:/Scripts/Key.txt" dir("C:/test")

It will operate on the .key files in directory "C:/test" as specified. It will exist the first time it does not find "Cannot add key file".

Test first in a test folder.


Report •

#11
October 11, 2010 at 03:06:54

Thanks for the alternative solution. Ivo has answered my qn with perfection already. Thanks anyway!!

Report •


Ask Question