Page 1 of 1

Question about ^ and !

Posted: 06 Jun 2011 00:16
by Amin
Hi All;
I want to ask about the ^ and the ! used in batch file coding:
i need to understand their usage in general,but the below are just examples:

Exmples:
1-
FOR /F "tokens=* delims==" %%A IN ('SET "" ˆ| FINDSTR.EXE /B "="') DO @ECHO.%%A



2-
set str=15 Trailing Spaces to truncate &rem
echo."%str%"
for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
echo."%str%"

3-
set LF=^




and what is the difference when we use %var:~x,y% and using !var:~x! ?
I didn't use them (^ ,!) before.


Thank you.

Re: Question about ^ and !

Posted: 06 Jun 2011 00:43
by Ed Dyreen
Many questions, well here we go then:
line continuation:

Code: Select all

echo.hel^
lo
pause
exit /b

This is just one of many things you can do with ^, you really need to learn it if you want to do some serious programming.
http://www.dostips.com/forum/viewtopic.php?f=3&t=1855&p=7966&hilit=linefeed#p7966
http://www.dostips.com/forum/viewtopic.php?f=3&t=1842&p=7503&hilit=linefeed#p7503

for the set questions: enter this inside terminal
set /?

Try this it will make you an intermediate in notime, you'll then be able to solve most problems yourself.
MS-DOS/MSDOS Batch Files: Batch File Tutorial and Reference :arrow:
www.^
allenware.com/icsw/icswidx.htm

And also www.^
dostips.com

Looks funny, yes we can only place 2 links per post. :cry:

Re: Question about ^ and !

Posted: 06 Jun 2011 00:49
by nitt
I think by "!"'s are you talking about, what are they called, like delayed expanding variables or something?

Basically, just write

Code: Select all

setlocal enableDelayedExpansion


at the top and then instead of

Code: Select all

%variable%


you now have

Code: Select all

!variable!


They work a little differently, I don't really get exactly but basically if one variable doesn't define correctly you use this kind.

Such as when I tried

Code: Select all

@echo off
for /f %%a in (total.txt) do (
set text=%%a
set /a total+=%text:~6,10%
)
echo %total%
pause


The variable wasn't working correctly so I changed it to the other one:

Code: Select all

@echo off
setlocal enableDelayedExpansion
for /f %%a in (total.txt) do (
set text=%%a
set /a total+=!text:~6,10!
)
echo %total%
pause


If you want to know what that script does, go here.

Re: Question about ^ and !

Posted: 06 Jun 2011 00:53
by Cleptography

Re: Question about ^ and !

Posted: 06 Jun 2011 00:57
by nitt


Well nobody likes to read all that. ;)

If the did then they'd just use set /?.

:D

Re: Question about ^ and !

Posted: 06 Jun 2011 01:02
by Cleptography
I know nitt just stating the obvious, I just don't understand why people don't dig a little before asking questions especially very easy to find answers, I don't understand, there is a difference between I searched for hours and hours now I will consult and expert and then just laziness. :!:
I mean come on you have an entire internet at your finger tips you can find anything it is there just look or at least learn how to properly use your search engine, google is your friend.
intext:"Some Shit I am looking to Find in the Text"
inurl:"Find My words in the url"
intitle:"Find what I need In the Title of the search"
intext:"Find this is the text minus these files out" -php -htm -mp3 -mp4

Re: Question about ^ and !

Posted: 06 Jun 2011 01:25
by orange_batch
Short description of !: works like you would expect variables to work.

It's the %% form that's odd. It expands during interpretation and not execution, so it makes the variable's contents static (which is what causes static behaviour within parentheses, as it's pre-expanded within a process that changes it's contents dynamically). It's really not that hard to understand.

Re: Question about ^ and !

Posted: 06 Jun 2011 01:39
by nitt
orange_batch wrote:Short description of !: works like you would expect variables to work.

It's the %% form that's odd. It expands during interpretation and not execution, so it makes the variable's contents static (which is what causes static behaviour within parentheses, as it's pre-expanded within a process that changes it's contents dynamically). It's really not that hard to understand.


Oh. Well I've never used these until I came here and that was only that one time. :/

Re: Question about ^ and !

Posted: 06 Jun 2011 01:54
by Amin
Thank you All.