Solved where did I go wrong?

January 26, 2019 at 14:58:35
Specs: Windows 10
@ECHO off
set start=-9
set end =0
set str=actareandbigbadbudcatcancardogdigdoseggeraelkfoxfixfaxgungingaghoghughadivyirkink

setlocal enableDelayedExpansion
:Looper
set /A start=%start%+9
set /A end=%end% +9
echo !str:~%start%,%end%!
if not %end%==72 goto :Looper
endlocal


I expected output as

actareand
bigbadbud
catcancar
dogdigdos
eggeraelk
foxfixfax
gungingag
hoghughad
ivyirkink

What I got was

actareand
bigbadbudcatcancar
catcancardogdigdoseggeraelk
dogdigdoseggeraelkfoxfixfaxgungingag
eggeraelkfoxfixfaxgungingaghoghughadivyirkink
foxfixfaxgungingaghoghughadivyirkink
gungingaghoghughadivyirkink
hoghughadivyirkink


See More: where did I go wrong?

Reply ↓  Report •

#1
January 26, 2019 at 18:22:22
lemme try that  

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v LLC=6 | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | MSI Armor RX 570 4GB@1410Mhz core@1.2v/1920MHz


Reply ↓  Report •

#2
January 26, 2019 at 18:30:11
✔ Best Answer
@ECHO off&setlocal enableDelayedExpansion
set str=actareandbigbadbudcatcancardogdigdoseggeraelkfoxfixfaxgungingaghoghughadivyirkink
set count=0
:Looper
echo !str:~0,9!
set str=!str:~9!
set/a count+=1
if !count!==9 pause>nul&exist/b
goto looper

edit: result=

actareand
bigbadbud
catcancar
dogdigdos
eggeraelk
foxfixfax
gungingag
hoghughad
ivyirkink

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v LLC=6 | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | MSI Armor RX 570 4GB@1410Mhz core@1.2v/1920MHz

message edited by hidde663


Reply ↓  Report •

#3
January 26, 2019 at 18:43:24
%variable:~0,9% means from 0, 9 characters


so the loop should be

0,9 actareandbigbadbudcatcancardogdigdoseggeraelkfoxfixfaxgungingaghoghughadivyirkink
0,18 actareandbigbadbudcatcancardogdigdoseggeraelkfoxfixfaxgungingaghoghughadivyirkink
0,27 actareandbigbadbudcatcancardogdigdoseggeraelkfoxfixfaxgungingaghoghughadivyirkink

etc.

you did:

0,9
9,18
18,27 <-- there are not that many characters, thats why you get this weird shape:

mmmmmmmmmmm
mmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmm
mmmmmmmmm

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v LLC=6 | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | MSI Armor RX 570 4GB@1410Mhz core@1.2v/1920MHz


Reply ↓  Report •

Related Solutions

#4
January 27, 2019 at 06:49:11
Appreciate hidde663's answer. He's got it down pat, but I don't really understand this local enabledelayedexpansion business. I will study his answer real soon now, and I must admit his was a speedy response. I guess I need a manual on this enable delayed stuff. Thanks much, hidde663!

Reply ↓  Report •

#5
January 27, 2019 at 10:15:54
To actually answer the question of, "What did I do wrong?" instead of re-writing and making a working version, I'll just tell you the three issues I noticed at a glance. None of them involves your usage of delayed expansion.

1) @ECHO off
Whenever you're wondering what's going on with a batch, the very first action you should take is to remove this line and keep your echo on. Otherwise, you're telling CMD, "What's going on? NO, DON'T TELL ME!"

2) set end =0
Space between "end" and the equals sign means you're not setting %end% but rather %end %, which is like %end%, but with a trailing space. You got away with it because SET /A will accept "+9" as a valid positive number.

3) echo !str:~%start%,%end%!
As hidde663 points out, the substring syntax is variable name, colon, tilde, starting offset, comma, number of characters desired. His implementation gets the first 9 characters and then removes them from %str%. It's effectively identical for this example, but subtly different. The corrected version of your implementation would look like:

echo !str:~%start%,9!

How To Ask Questions The Smart Way


Reply ↓  Report •

#6
January 27, 2019 at 12:46:50
crude addition by me:

RE #4 enabledelayedexpansion allows the usage of !variables!, which update even within for loops and other weird loops within loops. it also allows for variables, within variables like:
!str:~%start%,%end%!

without enabledelayed expansion and the use of !variables!, !str:~%start%,%end%! would have been read as;

%str:~% error
start starts another cmd within the current batch/cmd window
%,% error
end error not reqognized
%% and another error haha

------

but yeah i agree there isnt much explanation from Microsofts side, you kinda have to learn this stuff by practice.

i've used this site a lot tho:
https://www.robvanderwoude.com/sear...

it contains load of premade scripts and explanation to all native commands in windows going back to 95 i believe.

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v LLC=6 | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | MSI Armor RX 570 4GB@1410Mhz core@1.2v/1920MHz


Reply ↓  Report •

#7
February 17, 2019 at 08:00:54
I initially tried to hide the fact that it was really a sudoku puzzle in the permalink, and to make it look nicer, remove the http stuff at the beginning, and convert the dashes to zeroes, and end up with the 81 numbers
in a 9 X 9 framework. I just wish I knew this stuff like you do. Tks a bunch. 10 4, over and out..
bis zum naechsten Mal - schon vergessen und vorbei…,

I'll take a peek at the website your recommended.


Reply ↓  Report •

Ask Question