-2

I have a text file with one string per line (only a couple lines total). This file needs to be searched and each string stored. The script will ultimately prompt the user to choose one of the stored strings (if more than one string/line is present in the file), but the for loop is iterating an extra time when I don't want it to.

I'm using a counter to check the number of iterations, and I also read that it's probably a NL CR regex issue, but the finstr /v /r /c:"^$" in the for file-set like in this post Batch file for loop appears to be running one extra time/iteration doesn't work for me (but I probably don't understand it correctly).

The "pref" term is because the strings are to be eventually used as a prefix of files in the same folder...

@echo off
setlocal enabledelayedexpansion

set /a x=1
for /f %%a in (sys.txt) do (
  set pref!x!=%%a && echo %^%pref!X!%^% && set /a x+=1
)

echo last value of x = !x!

for /L %%a in (1,1,!x!) do (
  echo !pref%%a!
)

REM The rest would be to prompt user to choose one (if multiple) and 
REM   then use choice as a prefix with a ren %%a %prefX%%%a

If the "sys.txt" contains three lines with strings A, B, C respectively, then the output I currently get is:

pref1
pref2
pref3
last value of x = 4
A
B
C
ECHO is off.

ECHO is off. is not desired, clearly.

Community
  • 1
  • 1
Glycoversi
  • 77
  • 8
  • 2
    So just `set /a x=-1` immediately after the `for` loop... – SomethingDark Apr 06 '17 at 02:58
  • @SomethingDark Nice. Didn't think of that. But I have had an issue using the rename command before where it add a prefix to the first file of a group twice. So I was hoping to understand that better.. – Glycoversi Apr 06 '17 at 03:01
  • 3
    You have a Base0/Base1 mismatch error. You should start with `set /a x=0` (when you have zero strings) and increment `set /a x+=1` _before_ assign the next string. – Aacini Apr 06 '17 at 04:42
  • The two `%^%` parts around `pref!X!` are useless as they are interpreted as expansion of a variable named `^` which is empty, so the result is `pref!X!` anyway... – aschipfl Apr 06 '17 at 18:41
  • @aschipfl the %^% was the only way I could print pref1 etc. Without printing %pref1%. – Glycoversi Apr 06 '17 at 18:48
  • 1
    `echo pref!X!` is enough to print `pref1`, etc., there is absolutely no need for `%^%`... – aschipfl Apr 06 '17 at 19:57
  • @aschipfl I see. How would I echo the "A" value of the pref1 variable from inside the loop then? – Glycoversi Apr 06 '17 at 20:15
  • 2
    Use `call echo %%pref!X!%%` or `for %%x in (!X!) do echo !pref%%x!`. See [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) for further details... – Aacini Apr 06 '17 at 23:22

1 Answers1

1

You just need to change your increment structure like this. (set it before each line starting from a base of 0)

@Echo Off
Setlocal EnableDelayedExpansion

Set "i=0"
For /F "UseBackQ Delims=" %%A In ("sys.txt") Do (
    Set/A "i+=1"
    Set "pref!i!=%%A"
    Echo(pref!i!)

Echo(last value of i = %i%

For /L %%A in (1,1,%i%) Do Echo(!pref%%A!
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Where is the closing `)` of the `For` loop? – lit Apr 06 '17 at 12:55
  • @lit: It is the last paren in `Echo(pref!i!)` line! I think Compo wrote it specifically this way (adding the not needed left paren in `Echo` command) with the purpose of confuse people... **`:/`** – Aacini Apr 06 '17 at 15:37
  • @lit, as all of my `Echo` commands are immediately followed by the 'not needed' opening parenthesis it should adequately counter the confusion statement which was certainly not purposeful. _(In fact if I didn't have to select a different page on my android keyboard to input an **`=`** I'd have used that in all three places as I usually do)_. – Compo Apr 06 '17 at 15:49