3
@ECHO OFF
set /A q=0

for /F "tokens=*"  %%E IN ('findstr /n /r "Finshed With Errors" D:\DataUploadUtil\UploadStatus\TariffUploadLog.txt') do (

set array[!q!]=%%E

set /A q=q+1


echo %%E >>D:\DataUploadUtil\UploadStatus\AllTariffsUploadStatus.txt

set test=%%E

echo %test%              ::error 


)

In above Script I tried to Print test in loop its Showing ECHO IS OFF. I want to print test variable

Thanks

1
  • 1
    This ** IS NOT ** a Bash Script. Bash is a *NIX shell not DOS/Windows. The above is a DOS/Windows Batch File. Commented May 7, 2014 at 18:40

3 Answers 3

5
@ECHO OFF
setlocal enabledelayedexpansion
set /A q=0
for /F "tokens=*"  %%E IN (
  'findstr /n /r "Finshed With Errors" D:\DataUploadUtil\UploadStatus\TariffUploadLog.txt'
 ) do (
  set array[!q!]=%%E
  set /A q=q+1
  echo %%E >>D:\DataUploadUtil\UploadStatus\AllTariffsUploadStatus.txt
  set test=%%E
  echo !test!
)

You need setlocal enabledelayedexpansion to invoke delayedexpansion mode where !var! is the run-time value of var. %var% is always the parse-time value - that is - the value before the loop (or block) is run.

ECHO %%E would also work in this instance (as you've used in appending to all...txt.)

The echo is off report is caused by the fact that test has no value when the for command is parsed so it's executed as echo with no parameter, which generates an error-status report.

If you want to echo a variable that could potentially be empty, exploit a quirk in ECHO by using

ECHO(%potentiallyemptyvar%

Note here that the ( is seen as part of the echo command - it plays no part in nesting of round brackets...

Sign up to request clarification or add additional context in comments.

Comments

1

I think you are missing SETLOCAL / ENDLOCAL lines.. Anytime you use a ! in a variable, you need to use SETLOCAL.. The % is used for standard batch processing.

@ECHO OFF
SETLOCAL enabledelayedexpansion
set /A q=0
for /F "tokens=*"  %%E IN ('findstr /n /r "Finshed With Errors" D:\DataUploadUtil\UploadStatus\TariffUploadLog.txt') do (
set array[!q!]=%%E
set /A q=q+1

echo %%E >>D:\DataUploadUtil\UploadStatus\AllTariffsUploadStatus.txt
set test=%%E
echo !test!              ::error 
ENDLOCAL
)

1 Comment

Again I am getting !test! only not value of test
-1

All you need to do is, add term "ENDLOCAL" after closing the braces.

1 Comment

There is no SETLOCAL in the code. And endlocal can't fix a percent expansion problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.