0

here is my problem. I make a batch and want to move all spaces between words from sentence. I did this on one way. But i want to set space as variable, and use that variable in set command. After cheking definition of variable, and even good echoing of this space variable, i cannot move it in last sentence. Somebody can tell me why?

@echo off

::why this doesnt work???


(set /p word2=i will make )<nul>textfile.txt

<nul (set/p word3=all strings be )>>textfile.txt

<nul (set/p word4=one longstring)>>textfile.txt

echo(

type textfile.txt

echo(

echo(

::seting space

set sp=a b

set sa=%sp:a=%

set space=%sa:b=%

if defined space (echo space is defined) else (echo space is not defined)

echo(

echo lets see:%space%%space%%space%something%space%something%space%%space%, ...works

echo(

set /p inputalltext=<textfile.txt

echo %inputalltext%...input from textfile

set noplaceforspace=%inputalltext: =% ...as you see as normal,no space

echo %noplaceforspace%

::so, problem starts here. why are here  places if space is defined and good echoing

::of space variable??

setlocal enabledelayedexpansion

set nogoodresult=%inputalltext:!space!=%

echo %nogoodresult%%space%%space% here i dont want space between words

echo(

pause
Zvonimir
  • 53
  • 6

2 Answers2

1

Please look at my example:

@echo off&setlocal
set "space= "
set "sentence=this is a long sentence with many words "

setlocal enabledelayedexpansion
set "sentence=!sentence:%space%=!"
echo "%sentence%"
Endoro
  • 34,892
  • 8
  • 45
  • 61
  • thanks man; i changed places of % and !. works fine. so, sentence variable must be developed before space variable, or i am wrong? – Zvonimir Apr 09 '13 at 23:07
  • @Zvonimir Not exactly. It's a matter of the sequence of events. Your original code `%inputalltext:!space!=%` wold first attempt to replace the literal `!space!` in `inputalltext` by [nothing]; then `echo` the result, substituting the value of `var` for any !var! remaining. You could check this by changing the line `...word4=one longstring...` in your original code to `...word4=one long!space!string...`. `!var:%anothervar%!` replaces `%anothervar%` first then, because `delayedexpansion` has been invoked, `!var:anothervarsvalue!` is evaluated, – Magoo Apr 10 '13 at 02:23
  • So,i will try explain to me by myself. Please correct me, if you not correct me, i will never understand!!! I am talking about correct version now( !var%somevar&! ). Before setlocal enabledelayedexpansion there are some variables. Those some variables are set before time of setlocal enabledelayedexpansion. All of them i can use, and it is clear for me. After activating setlocal enabledelayedexpansion happens next: In a command "set nogoodresult=!inputalltext:%space%=!" computer first want to put "space" in "inputalltext", by defolting of program logic with no meter of % or ! ?? – Zvonimir Apr 10 '13 at 07:56
  • then he is looking for % or ! variables. If he find % variable, first he expand it, and all variables with %, after that he expands variables with !. So enabledelayed means later expand variables with ! ?????? – Zvonimir Apr 10 '13 at 07:59
  • First the parser expands %space% and later ("delayed") !inputalltext! was expanded but NOT parsed. Please mark the question "answered". – Endoro Apr 10 '13 at 08:02
  • i will mark all question answered, just need some fifteen of something to get authority to mark. now i got 9 of that – Zvonimir Apr 10 '13 at 08:09
  • ok, works. i can mark it answered. i will mark it, just wait for some comment more. it is good for me to get more answers because of language hendikep. so i can better understand terminology( words ) you use. You two guys are very helpfull. – Zvonimir Apr 10 '13 at 08:17
  • So first happend parsing,then expand %space%, then expand !inputalltext!. No need to parsing it because he stay last? – Zvonimir Apr 10 '13 at 08:20
  • First expand %variables%, then parse it. Please look [here](http://stackoverflow.com/a/4095133/2098699) to see, how the parser works. – Endoro Apr 10 '13 at 08:26
  • only one question more: word "expand" means "make variable be active on its place now" ???? – Zvonimir Apr 10 '13 at 08:29
  • Expand means, the string %space% was replaced with a real ` `. If you mark your answers, please dont forget [this](http://stackoverflow.com/a/15799616/2098699) – Endoro Apr 10 '13 at 08:34
0

if you are running a regular batch you should be able to do "sentence sentence" as long as you have the"" around it. the system should enter a space

CMS_95
  • 339
  • 3
  • 13