[solved] Put an unknown string from a line into a variable:

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

[solved] Put an unknown string from a line into a variable:

#1 Post by ByteAnalyser » 17 Jul 2010 08:12

---::[ ForeworD ]::---
First of all, greetz dear DosTips Admin(s) for keeping up this cool website. Second thanks a lot to all scripting enthusiasts for their job / good answers and tutorials / guides!

---::[ SituatioN ]::---
I got some files. Those files are more or less like that: (hashlog.txt)

Code: Select all

System tool intern hash account report:
The needed string for Jack is              CK-84-010-a1a067cf-6264006c-8955cb-7fba
The needed string for Daneel Olivaw is     CK-84-010-54602-c3f247c72f-15a7d2dbf
The needed string for Jeff Jibba Oracle is CK-84-010-657f8b8da628ef-83cf69101b6817150a


---::[ AnalysiS ]::---
01) the output language ("The needed string for") may vary so no delims.
02) usernames are always different and may contain multiple 0x20 (space char)
03) the strings start always with "CK-84-010-" what follows vary and may contain multiple "-" char.

---::[ What did I do? ]::---
I already fully read DOS - String Manipulation, DOS - String Operations from DosTips.com and studied find and findstr commands. Even searching the forum itself before posting, but... Unfortunately I'm new to scripting as many other here I think, but I were not able to find a solution to my following problem:

---::[ What do i need? ]::---
Considering the above hashlog.txt example, I need to set a variable containing only:

Code: Select all

CK-84-010-54602-c3f247c72f-15a7d2dbf

01) goto a given line, in that case "go on line 2" (The needed string for Daneel Olivaw is CK-84-010-54602-c3f247c72f-15a7d2dbf)
02) look in this line the pattern "CK-84-010-" and when the pattern is found:
03) set a variable which includes the pattern+everything until the end of the string.

---::[ The most important thinG ]::---
Please, if anyone is willing to help me, I really do very appreciate it very much if you are going to show (even if it is a long) a very easy, step-by-step code and explain perhaps every line or at least tell on which internet page I may find the exact explanation.

---::[ ConclusioN ]::---
!!!Thank you very much in advance!!!
Respect2aLL
kind regards to the readers and
Best Regards to the helpers
(p.s.: hope this request-post was not too long/complicated :)
Last edited by ByteAnalyser on 17 Jul 2010 13:08, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Put an unknown string from a line into a variable:

#2 Post by aGerman » 17 Jul 2010 10:04

I've too less time at the moment. Please try the code and tell me if it works or not. Explanation comes later ...

Code: Select all

@echo off &setlocal
set "textfile=hashlog.txt"
set /a lineNumber=3

for /f "delims=: tokens=1*" %%a in ('findstr /n /c:"CK-84-010-" "%textfile%"') do (
  if %%a==%lineNumber% (
    set "line=%%b"
    call :findHash
  )
)
echo.%hash%
pause
goto :eof

:findHash
  set "line=%line:CK-84-010-=§%"
  for /f "delims=§ tokens=2" %%a in ("%line%") do set "hash=CK-84-010-%%a"
goto :eof



Regards
aGerman

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: Put an unknown string from a line into a variable:

#3 Post by ByteAnalyser » 17 Jul 2010 11:13

Wow! UnbelivIncredible. aGerman, thank you very much for the fast response. (Mir rinnt schon das Wasser im Mund for Freude ROFL)

It worked just fine. What can I say... thank you very much!

I do not like to become annoying, but I really am curious to learn and especially to understand quite every single command, reserved words, and operators used in your code (let's say a kind of tutorial or in-depth-guide).
Once again thank you very much for your efforts; I do appreciate it! :)

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Put an unknown string from a line into a variable:

#4 Post by alan_b » 17 Jul 2010 11:55

"@" means silent command - do not display this
"echo off" means do no display anything more until the command "echo on", but you still see "echo off" at the top of the screen.
"@echo off" this line is also not displayed

Run CMD.EXE and on the command line these commands tell you how to do things
SET /?
FOR /?
CALL /?
etc etc.
every DOS command I can think of will give help by the /? argument

Regards
Alan

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: Put an unknown string from a line into a variable:

#5 Post by ByteAnalyser » 17 Jul 2010 12:45

Thanks a lot alan_b for replying. I know about the "/?" but i thought more something like:

set "line=%line:CK-84-010-=§%"
Why is the first blue double score before the _line=_ and not after. What difference in function is between the 2 equal signs? What does the ":" char stand for? The paragraph char (§) a special char?? What? I know a little about the basics but why do I need this string "&setlocal"?

That's because I said I don't want to become annoying, but for me this is a very important thing and I really need to KNOW what the single chars/commands do. Again an example I know by hearth almost all I need about the @echo off and findstr and pause function and call another part of the code, but once again: is :eof a system call? What is %%a %%b, why not %a%?

Sorry if I wasn't clear before... :)
Again thanks a lot aGerman and alan_b...

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Put an unknown string from a line into a variable:

#6 Post by aGerman » 17 Jul 2010 12:56

Have a look at the REM lines:

Code: Select all

@echo off &setlocal
REM don't show the prompt and limit changes of the environment to the current batch process

set "textfile=hashlog.txt"
set /a lineNumber=3
REM declare the variables

for /f "delims=: tokens=1*" %%a in ('findstr /n /c:"CK-84-010-" "%textfile%"') do (
REM option /n prepends a line number and a colon
REM   example- "1:System tool intern hash account report:"
REM "delims=: tokens=1*" means that you will find the line number in %%a
REM   and the rest of the line in %%b

  if %%a==%lineNumber% (
  REM if the current line number equals the predefined line number then ...
 
    set "line=%%b"
    REM ... assign the text ...
   
    call :findHash
    REM ... and call subroutine ":findHash"
  )
)

echo.%hash%
REM print the variable to the screen

pause

goto :eof
REM jump to the end of file (quit the batch execution)


:findHash
REM subroutine to process the found line

  set "line=%line:CK-84-010-=§%"
  REM substitude "CK-84-010-" by a single character ("§" in this case)

  for /f "delims=§ tokens=2" %%a in ("%line%") do set "hash=CK-84-010-%%a"
  REM split the line on the new character (§) and
  REM   prepend CK-84-010- to the second half of the line

goto :eof
REM jump to the end of file (quit the execution of the subroutine)


%line:CK-84-010-=§% means that in variable %line% the string "CK-84-010-" will replaced by "§". I used the paragraph sign because I thought you probably don't have the paragraph sign in your text file.

goto :eof means goto End Of File

%%a, %%b, ... are dynamic variables (only used in a FOR loop)

Regards
aGerman

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: [solved] Put an unknown string from a line into a variab

#7 Post by ByteAnalyser » 17 Jul 2010 13:14

Thank you so much +aGerman, Du hast mein ganzen Respekt!

... this last post was entirely/exactly what I was looking for.
Thanks to all DosTips.com Community and Members! :D

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: [solved] Put an unknown string from a line into a variab

#8 Post by ByteAnalyser » 17 Jul 2010 14:43

Oh I forgot something important. If I add:
"Thanks to aGerman\DosTips.com for part of the code"
can I add your code snippet into one of my simple scripts? This is the last question and then this thread is done.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [solved] Put an unknown string from a line into a variab

#9 Post by aGerman » 17 Jul 2010 15:10

Of course. It is posted in a public forum and I didn't add a copyright. (BTW Wouldn't that be useless?)

Regards
aGerman

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: [solved] Put an unknown string from a line into a variab

#10 Post by ByteAnalyser » 17 Jul 2010 16:35

aGerman wrote:Of course. It is posted in a public forum and I didn't add a copyright. (BTW Wouldn't that be useless?)

First: thanks :)
Second: Yes, it would be definitely be useless. Anyway I just wanted to show you my gratitude and that I am honest, as the final script, if I'm able to finish it, will be copyLEFT and free to modify, redistribute and use... That's it that's all :) (--> nice movie, too lol)

Post Reply