You are not logged in.

#1 15 Jan 2016 22:07

Batcher
Member
Registered: 20 Jul 2015
Posts: 56

Goto line in batch file

File one.bat starts 2.bat a batch file in the same directory and when 2.bat is done how can I go back to file one.bat and search for a specific piece of text and start on the x th number that is found x being a number in num.txt a text file in the same directory.

If this isn't clear let me try to show you a snippet of code

@echo off 
Cls 
:: file one.bat
2.bat
Echo I'm back
Pause
@echo off 
Cls 
::2.bat
Echo doing stuff...
Set /a %var%+1>file.txt
Call file one.bat && search for 2.bat and find the one that is x down and continue on next line
:: so that would echo I'm back

I know this can be done with the call command but I am looking for a way to do this without the call command thanks!

Offline

#2 15 Jan 2016 22:56

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: Goto line in batch file

There is NO WAY to go to a line that have no label! This is a way to do that (based on a label):

@echo off 
setlocal
if defined return goto %return%
Cls 
echo File one.bat
set "return=Ret1"
2.bat

:Ret1
Echo I'm back
Pause
@echo off 
::2.bat
Echo 2.bat: doing stuff...
Set /a %var%+1
one.bat 

Antonio

Offline

#3 16 Jan 2016 01:12

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Goto line in batch file

Why can't you use the call command? It exists for a reason!

The best solution I can come up with is to allow one.bat to take in a label as a parameter and use a goto to skip there.

:: Valid parameters are the words "one" and "two"
@echo off
setlocal enabledelayedexpansion
cls

if not "%~1"=="" goto :%~1

:zero
echo No parameter was given.
goto :eof

:one
echo Parameter ONE was given.
goto :eof

:two
echo Parameter TWO was given.
goto :eof

Offline

#4 16 Jan 2016 04:17

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Goto line in batch file

Posts and threads with crap, useless, bogus, misleading questions should just be deleted.

The internet is becoming full of them - every tech forum - and they are going to be of absolutely no use to any reader in the future.


I'm officially applying for my curmudgeon badge!

Last edited by foxidrive (16 Jan 2016 04:18)

Offline

#5 16 Jan 2016 04:34

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Goto line in batch file

This could be useful if some knucklehead has gone and created a call.bat and stuck it in one of the %PATH% directories or the current directory.

Offline

#6 16 Jan 2016 11:55

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Goto line in batch file

I can't follow the question Shadow Thief, and there's no actual text about the task explaining what it's supposed to do...

This line for example.  What function does it serve?

Set /a %var%+1>file.txt

My rant continues below : smile

Batch scripting has become such a different field, where questions are asked with bogus information, and there is no discussion about the task, and add to that - the examples you see are made up and don't reflect the actual task.

Programming isn't something that lets you guess what the person wants to do - and batch scripting is most effective, simple and straightforward when it is designed around the real task.

Young programmers don't realise this:
- that information about the exact task allows you and I to write robust and efficient code.
- They aren't aware that they waste the time of people who are helping them free of charge.

Offline

#7 16 Jan 2016 15:26

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: Goto line in batch file

@foxidrive: I completely agree with you. When beginners post nonsense questions is a waste of our time trying to understand what they mean. I usually just ignore these questions.

However, now and then these "nonsense" beginner questions include some point that may suggest a new approach or method that may lead to a new or different way to solve a problem. This approach would not even be considered from more experienced users. At least two of my original tricks had emerged from this type of questions, so I consider they as a source of inspiration. Be gentle with they...  neutral ... smile

Antonio

Last edited by Aacini (16 Jan 2016 15:29)

Offline

#8 16 Jan 2016 17:58

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Goto line in batch file

In reply to Antonio:

I may have been unreasonably annoyed, as my health is very poor.

I would have liked to have seen some explanation of why call can't be used, as that has a bearing on the final solution.

Aacini wrote:

@foxidrive: I completely agree with you. When beginners post nonsense questions is a waste of our time trying to understand what they mean. I usually just ignore these questions.

I could just ignore them as you wisely suggest - but I'd end up ignoring 90% of the posts. big_smile

If I had been clearer in my post I would have commented that a great deal of the time it's not the beginners that do this.

However, now and then these "nonsense" beginner questions include some point that may suggest a new approach or method that may lead to a new or different way to solve a problem. This approach would not even be considered from more experienced users. At least two of my original tricks had emerged from this type of questions, so I consider they as a source of inspiration. Be gentle with they...  neutral ... smile

Antonio

Yes, that's a good point and good advice, thanks.

Last edited by foxidrive (16 Jan 2016 18:03)

Offline

Board footer

Powered by