0

Thanks Nate. It works.
But now i need output variable for this script to use later.
There are 2 outputs i need. First is count variable as integer output, second is string variable which is equal to last string character in "word".
So, i need this 2 variable as you see in last row of finished script before "press any key to continue...".
1 last integer variable and 1 last string variable.

I need these 2 variables for later scripting in this script.
How i can have it? Here is a script which works from yesterday.

@ECHO OFF
:input
set /p word=input your word:
if not defined word goto input
(ECHO %word%)> tempfile.txt
FOR %%x IN (tempfile.txt) DO ( SET /A lenght=%%~zx - 2 )

del tempfile.txt
echo %word% got %lenght% characters
setlocal enabledelayedexpansion

for /l %%m in (1,1,!lenght!) do (
  set /a count=%%m
  set /a index=%%m-1
  call echo !count! %%word:~!index!,1%%
) 

(call echo %%word:~!index!,1%%)>tf.txt


for /f "tokens=*" %%a in (tf.txt) do (
set line=%%a
set char=!line:~0,1!

)

pause

echo %count% %char%

endlocal

pause
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Zvonimir
  • 29
  • 6
  • is it good now for others to understand? – Zvonimir Apr 06 '13 at 08:31
  • the forum soft did obstuct me formatting your question better. please do it yourself. – Endoro Apr 06 '13 at 09:07
  • look on this finished code. works when endlocal is at the end. i got 2 variables( one last integer and one last string ). but not working if i put endlocal after create tf.txt. can you make work on this way if endlocal is in next line after creation of tf.txt? – Zvonimir Apr 06 '13 at 14:26

2 Answers2

2

There are many ways documented here that show how to do this, however endlocal & set global=local is my favourite.

Judging by what I think you want...

setlocal enabledelayedexpansion
for /l %%m in (1,1,!lenght!) do (
    set /a count=%%m
    set /a index=%%m-1
    call echo !count! %%word:~!index!,1%%
    set laststring=%word:~!index!,1%
) 
(
    endlocal
    set count=%count%
    set laststring=%laststring%
)
BDM
  • 3,760
  • 3
  • 19
  • 27
1

Try this technic to tear down the wall of setlocal enabledelayedexpansion / endlocal:

@echo off&setlocal
set "word=abcdefghij"
set /a lenght=10

for /l %%m in (1,1,%lenght%) do (
    set /a $count=%%m
    set /a index=%%m-1
    setlocal enabledelayedexpansion
    call echo !$count! %%word:~!index!,1%%
    call set "$laststring=%%word:~!index!,1%%"
    for /f %%i in ('set $') do (if "!"=="" endlocal)& set "%%i"
)
echo( 
echo !$count! !$laststring!
echo %$count% %$laststring%

(source)

Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 1
    I only can say, that you make it again. do you sleep on book instead of pillow? you are genious. please if you can send me via mail brief explanation of every piece of your code i will be very greatfull to you. mail is zeusaluminium@gmail.com . – Zvonimir Apr 07 '13 at 07:56
  • i mean thoroughly and slow explanation. i was thinking that "brief"=slow and thoroughly, so type in google and find out that i use this word for wrong purpose:). learn every day:) – Zvonimir Apr 07 '13 at 08:09
  • `delayed expansion` doesn't mean "slow", it means "later" :) – Endoro Apr 07 '13 at 08:27
  • if i enebledelayedexpansion does this mean that all variables with !(exclamation mark) will developed at the same time in eni position in that block of enabledelayedexpansion?? – Zvonimir Apr 07 '13 at 08:49
  • delayed expansion means, the parser doesn't interpret `!variable!`s. Example: `@echo off ` `setlocal ` `set "uglyvar=^^&!&dir" ` `setlocal enabledelayedexpansion ` `echo !uglyvar! ` `pause ` `echo %uglyvar% ` ` ` – Endoro Apr 07 '13 at 09:00
  • who is parser in your code? – Zvonimir Apr 07 '13 at 09:12
  • Please see [here](http://stackoverflow.com/a/4095133/2098699), how does the parser work. – Endoro Apr 07 '13 at 09:21
  • thanks, i need at least two days to put these things in my head. so on tuesday i will be back on this article if will have something to ask. you helped me a lot with your time and examples;) – Zvonimir Apr 07 '13 at 09:34