Question about ^ and !

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Amin
Posts: 20
Joined: 30 May 2011 05:25

Question about ^ and !

#1 Post by Amin » 06 Jun 2011 00:16

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Question about ^ and !

#2 Post by Ed Dyreen » 06 Jun 2011 00:43

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:
Last edited by Ed Dyreen on 06 Jun 2011 01:05, edited 11 times in total.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Question about ^ and !

#3 Post by nitt » 06 Jun 2011 00:49

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.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Question about ^ and !

#4 Post by Cleptography » 06 Jun 2011 00:53


nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Question about ^ and !

#5 Post by nitt » 06 Jun 2011 00:57



Well nobody likes to read all that. ;)

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

:D

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Question about ^ and !

#6 Post by Cleptography » 06 Jun 2011 01:02

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Question about ^ and !

#7 Post by orange_batch » 06 Jun 2011 01:25

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.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Question about ^ and !

#8 Post by nitt » 06 Jun 2011 01:39

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. :/

Amin
Posts: 20
Joined: 30 May 2011 05:25

Re: Question about ^ and !

#9 Post by Amin » 06 Jun 2011 01:54

Thank you All.

Post Reply