Problem with this code.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Problem with this code.

#1 Post by einstein1969 » 08 Sep 2015 12:17

Hi to all,

I have problem with this code. The cmd go at 100% of cpu and hang.
This on windows 7
can anyone confirm?

Code: Select all

@echo off
setlocal EnableDelayedExpansion
if "%~1" neq "" goto %1

:main
 type nul >t1.txt
 start "" /WAIT "%~0" loop1 ^^^>t1.txt | start "" /WAIT "%~0" loop2 ^^^<t1.txt

exit/b

:loop1
  set "fill=#"
  for /L %%i in (1,1,10) do set "fill=!fill!!fill!"

  set /a num=1
  for /l %%f in () do (
    set /a "num+=1
    set msg=!num!!fill!
    title sending... !num!
    echo !msg:~0,1021!
    if "!num!"=="5000" (
        title quitting...
        set msg=quit!fill!
        echo !msg:~0,1021!
        ping localhost -n 4 >nul
        exit /b
      )
  )

:loop2
set /A num=1
for /L %%N in () do (
   set "v="
   set /P "v="
    if not defined v (
      echo v not defined
      ping localhost -n 2 >nul
    ) else (
      set /A "num+=1
      echo(^>'!v:~0,20!'...[!num!]
    )
    if /I "!v:~0,4!"=="quit" (
       title quitting...
       ping localhost -n 4 >nul
       exit /b
    )
)


EDIT:have insert a row in the code "type nul >t1.txt". There was an error.
EDIT2: Removed %%R in code.

einstein1969
Last edited by einstein1969 on 09 Sep 2015 08:31, edited 1 time in total.

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Problem with this code.

#2 Post by Meerkat » 09 Sep 2015 04:47

I am no good in error detecting, but...

1) From this line, where does the %%R came from?

Code: Select all

start "" /WAIT "%~0" loop1 %%R ^^^>t1.txt | start "" /WAIT "%~0" loop2 %%R ^^^<t1.txt


2) Sorry, but I think exit /b just cannot break for /l %%? in () do (...). Just the Exit does. :roll:

Example:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set x=0
for /l %%I in () do (
   set /a x+=1
   echo.!x!
   if !x!==100 exit /b
)


Output:

Code: Select all

1
2
3
4
5
.
.
.
100

...it does stop, but does not close the CMD window. :roll:

Maybe there are some more errors.

Meerkat

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Problem with this code.

#3 Post by einstein1969 » 09 Sep 2015 08:30

Meerkat wrote:I am no good in error detecting, but...

1) From this line, where does the %%R came from?

Code: Select all

start "" /WAIT "%~0" loop1 %%R ^^^>t1.txt | start "" /WAIT "%~0" loop2 %%R ^^^<t1.txt


I forgot to take it off ... I correct code in the initial post.

Meerkat wrote:2) Sorry, but I think exit /b just cannot break for /l %%? in () do (...). Just the Exit does. :roll:

I did not understand what you mean (I may have a problem in the translation). Can you explain?

Einstein1969

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Problem with this code.

#4 Post by Meerkat » 09 Sep 2015 08:44

From this sample code (take note of the exit /b):

Code: Select all

@echo off
setlocal enabledelayedexpansion

set x=0
for /l %%I in () do (
   set /a x+=1
   echo.!x!
   if !x!==100 exit /b
)


It prints from 1 to 100, but does not close the CMD window... :(

Now, when you do this: (Just EXIT...)

Code: Select all

@echo off
setlocal enabledelayedexpansion

set x=0
for /l %%I in () do (
   set /a x+=1
   echo.!x!
   if !x!==100 EXIT
)

It prints 1 to 100, then closes the CMD window.

So this the same code as yours, but exit /b get replaced by exit:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
if "%~1" neq "" goto %1

:main
 type nul >t1.txt
 start "" /WAIT "%~0" loop1 ^^^>t1.txt | start "" /WAIT "%~0" loop2 ^^^<t1.txt

exit/b

:loop1
  set "fill=#"
  for /L %%i in (1,1,10) do set "fill=!fill!!fill!"

  set /a num=1
  for /l %%f in () do (
    set /a "num+=1
    set msg=!num!!fill!
    title sending... !num!
    echo !msg:~0,1021!
    if "!num!"=="5000" (
        title quitting...
        set msg=quit!fill!
        echo !msg:~0,1021!
        ping localhost -n 4 >nul
        exit
      )
  )

:loop2
set /A num=1
for /L %%N in () do (
   set "v="
   set /P "v="
    if not defined v (
      echo v not defined
      ping localhost -n 2 >nul
    ) else (
      set /A "num+=1
      echo(^>'!v:~0,20!'...[!num!]
    )
    if /I "!v:~0,4!"=="quit" (
       title quitting...
       ping localhost -n 4 >nul
       exit
    )
)


Off topic, I am just curious about this part:

Code: Select all

start "" /WAIT "%~0" loop1 ^^^>t1.txt | start "" /WAIT "%~0" loop2 ^^^<t1.txt

What is happening here? :)

Meerkat

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Problem with this code.

#5 Post by einstein1969 » 09 Sep 2015 13:48

was so simple ... :oops: I was getting mad :evil:

Meerkat wrote:Off topic, I am just curious about this part:

Code: Select all

start "" /WAIT "%~0" loop1 ^^^>t1.txt | start "" /WAIT "%~0" loop2 ^^^<t1.txt

What is happening here? :)

Meerkat


I do not know :) , I was doing tests to see how it worked a pipe. The initial code was:

Code: Select all

 start "" "%~0" loop1 ^>t1.txt
 start "" /WAIT "%~0" loop2 ^<t1.txt


einstein1969

Post Reply