some questions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

some questions

#1 Post by tcpman » 06 Mar 2014 08:34

i have some questions hope i can find a answer on here

1-fist is there any good quide about for command?
2-what is diffrence between label and a function in batch?
for exmaple

@echo off
echo welcome
goto label1
:label
echo test

:label1
echo test 2
exit

this is label or a function?i think there is no diffrence but i am not sure about it!

Aacini
Expert
Posts: 1910
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: some questions

#2 Post by Aacini » 06 Mar 2014 09:54

A label is a line that start in colon, like this one:

Code: Select all

:label

A label serves as the target destination for two commands: GOTO and CALL. If a GOTO is used, it just transfer the execution to the destination line:

Code: Select all

echo First line
goto label
note that this point can NEVER be reached
so another label should ALWAYS follows a GOTO command
. . .
:label
echo Second line

If a CALL :LABEL is used, then all the following happens:
  • The destination label is named a "subroutine" (or "function" if you prefer)
  • The lines following the label are executed
  • The subroutine must ALWAYS end in a EXIT /B or GOTO :EOF command! At this point, the execution is transfered back to the line that follows the CALL :LABEL command

Code: Select all

echo First line
call :label
echo Third line
goto :EOF

:label
echo Second line
exit /B

In previous example, note that if GOTO :EOF command were omitted, after "Third line" were printed, "Second line" would be printed again. This is usually an error, so:

  • Before the label that start a subroutine, a GOTO :EOF or EXIT /B command must ALWAYS be included!

Note that the same label may be used both as a function (destination for CALL) and as a "simple label" (destination for GOTO), but this is not usual.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: some questions

#3 Post by tcpman » 07 Mar 2014 02:39

wow tnx
does anyone have a guide about for command?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: some questions

#4 Post by jeb » 07 Mar 2014 03:16

For the basics you could look at http://ss64.com/nt/for.html.
The site describes the most internal and external batch commands.

It's a good starting point.

jeb

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: some questions

#5 Post by pieh-ejdsch » 07 Mar 2014 03:36

if you in a call - with Parameter %0 you can check this

Code: Select all

:Sub
 rem if not in Sub than not Exit
if "%~0" equ ":Sub" Exit /b


Phil

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: some questions

#6 Post by Squashman » 07 Mar 2014 04:39

tcpman wrote:wow tnx
does anyone have a guide about for command?

Open up a command prompt and type: for /?

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: some questions

#7 Post by tcpman » 07 Mar 2014 07:41

tnx but i use the cmd for help and that site but still could not understand
for exmaple why this is not working?
for/l %%a in (1,1,10) do rename %%a

i make a batch file in a folder and then i run it it but its not renaming the files on that folder
what part of my code is need to get fixed?

and about functions is this one a function?

@echo off
echo going to function
call:function
echo ...

:function
echo function test
goto:EOF
but why the echo ... is not working?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: some questions

#8 Post by foxidrive » 07 Mar 2014 08:02

tcpman wrote:for exmaple why this is not working?
for/l %%a in (1,1,10) do rename %%a


That is using a for-in-do command and a rename command.
The rename command is not right.

Look at the help for the rename command.


If you describe what you are trying to do then we can describe or show you how to do it.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: some questions

#9 Post by tcpman » 07 Mar 2014 08:16

oh yes sory
for /l %%a in (1,1,10) do rename c:\prodata\*.txt %%a.txt
ok
so for exmaple i want the files in prodara folder be something like this
before runing for command
text.txt
erer.txt
etet.txt
after runing for command
1.txt
2.txt
3.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: some questions

#10 Post by foxidrive » 07 Mar 2014 08:45

tcpman wrote:oh yes sory
for /l %%a in (1,1,10) do rename c:\prodata\*.txt %%a.txt
ok
so for exmaple i want the files in prodara folder be something like this
before runing for command
text.txt
erer.txt
etet.txt
after runing for command
1.txt
2.txt
3.txt


You can't do it with a simple for /L command but a regular for command can list the files

for %%a in (*.txt) do echo "%%a"

and you will need a counter variable that increments the number for each filename.
The regular for command needs some extra handling when you are altering the filenames with the same filespec of *.txt because of a bug.

It's not very useful giving an example to change filenames to 1.txt 2.txt 3.txt ect because that is unlikely to be what you need to do,
and batch files can change markedly when the task details change.

If you describe the actual task then it is easier to provide some sample code.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: some questions

#11 Post by tcpman » 07 Mar 2014 09:01

i just want to learn the for command
ok i think asking for this exmaple is better

ok how we can make a input and then if it was DOSTIPES
turn that to dostips
case sentence!

i saw a exmaple on that but culdnt understand it

and can you answer the function question?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: some questions

#12 Post by foxidrive » 07 Mar 2014 09:35

There are many ways to do a task - and the exact requirements will eliminate some ways and promote other ways.

Here is a way to do your task:

Code: Select all

@echo off
set /p "char=Enter uppercase text: "
type nul > "%char%"
dir /b /l "%char%" >temp.tmp
set /p "var="<temp.tmp
echo Input was "%char%" and is now "%var%"
del temp.tmp "%char%"
pause



If you post your function code then your question may be clearer. Earlier you said that 'echo' wasn't working and I don't see why.

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: some questions

#13 Post by tcpman » 07 Mar 2014 09:41

this is my code

Code: Select all

@echo off
echo going to function
call:function
echo ...

:function
echo function test
goto:EOF


the output is
going to function
function test
now i think the output must be
going to function
...
function test

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: some questions

#14 Post by foxidrive » 07 Mar 2014 09:48

Where you have a blank line in your function code, put this there

Code: Select all

goto :EOF


Does that fix it for you?

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: some questions

#15 Post by tcpman » 07 Mar 2014 10:30

yes tnx

Code: Select all

@echo off
echo going to function
call:function
echo ...

:function
echo function test
PAUSE
goto :EOF

its now work well

Post Reply