string manipulation, display a whole line or cut and display the part

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 132
Joined: 26 Aug 2017 06:11

string manipulation, display a whole line or cut and display the part

#1 Post by nnnmmm » 09 Aug 2024 05:19

Code: Select all

@ECHO OFF
SET K1=M:\REGISTRY.TXT
SET DS1=30
SET DS2=75
REM *********************************************************************
SetLocal EnableExtensions EnableDelayedExpansion
REM -------------------------------------------------------------
ECHO %K1%
ECHO.
SET CC=0
FOR /F "DELIMS=" %%V IN ('TYPE "%K1%"') DO (
   SET /A CC=!CC!+1

   IF !CC! GEQ 1 IF !CC! LEQ 9 SET DD= !CC!
   IF !CC! GTR 9 SET DD=!CC!

   SET VV=%%V
   ECHO !DD! !VV:~%DS1%,%DS2%!
   IF !CC! EQU 35 GOTO SCREEN_FULL
)

ECHO.
SET    VAR=DUMMY
SET /P VAR=INPUT:
IF %VAR%==DUMMY GOTO :END1
ECHO.
:SCREEN_FULL
a file REGISTRY.TXT has
HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
HKEY_USERS\S-1-5-21-790525478-796845957-725345543-1003\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Favorites
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\ClassicStartMenu
HKEY_CLASSES_ROOT\Applications
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer
HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\map2A\command
HKEY_CLASSES_ROOT\exefile\shellex\ContextMenuHandlers\PintoStartScreen\1111111111\222222222222\333333333\AAAAAAAAAAA\BBBBBBB\CCCCCCCCCCC
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
HKEY_CLASSES_ROOT\Applications\titanfall2.exe
HKEY_CLASSES_ROOT\Valve.Source


my question is
if the line is not long like "HKEY_CLASSES_ROOT\Applications", if the line is less than 70, display the whole line content
if the line is long then use DS1 and DS2 and cut a part and display from 30 to 75
SET DS1=30
SET DS2=75
would you fix the batch for me?
i only need some display to identify the registry with my eyes
it is better displaying the end part of a line as much as possible than the front part for me to identify the registry, then i can feed the registry to nircmd to get there

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

Re: string manipulation, display a whole line or cut and display the part

#2 Post by aGerman » 09 Aug 2024 09:26

Not tested:

Code: Select all

   if "!VV:~0,69!"=="!VV!" (
     echo !DD! !VV!
   ) else (
     echo !DD! !VV:~%DS%,%DS2%!
   )
Note that you have to update your DS2 value to 45 as it defines the number of characters after DS1.

Steffen

nnnmmm
Posts: 132
Joined: 26 Aug 2017 06:11

Re: string manipulation, display a whole line or cut and display the part

#3 Post by nnnmmm » 09 Aug 2024 19:58

thanks it worked greatly
i made a mistake saying, i meant displaying 75 characters from the position 30

is there a way to make 69 as a variable manipulation without introducing a new variable?

SET /A DS1=30
SET /A DS2=75
from if "!VV:~0,69!"=="!VV!" (
to some kind of if "!VV:~0,%DS2%-1!"=="!VV!" (

%DS2%-1 didnt work inside !!, just for my learning purpose
the script was simple enough so using a direct number 69 was not a problem, i have been using this half finished script for maybe 20 years without if "!VV:~0,69!"=="!VV!", my eyes adapted ok identifying the cut of registries

lastly
how would you implement displaying 75 characters from the last part of a line
in !VV:~%DS1%,%DS2%!
i hope to see if this would work out better

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

Re: string manipulation, display a whole line or cut and display the part

#4 Post by aGerman » 10 Aug 2024 04:30

nnnmmm wrote:
09 Aug 2024 19:58
is there a way to make 69 as a variable manipulation without introducing a new variable?
No.
FWIW in your initial post you wrote "if the line is less than 70". Was that meant to be "<= 70" rather than "< 70"?
nnnmmm wrote:
09 Aug 2024 19:58
%DS2%-1 didnt work inside !!, just for my learning purpose
Because SET /A is for calculations that you have to perform upfront.
However, see above. Not sure if you even need to do so.
nnnmmm wrote:
09 Aug 2024 19:58
how would you implement displaying 75 characters from the last part of a line
The help text of SET tells you about the use of negative numbers for that purpose.
You can always print just the last 75 characters. The whole string is used if it is shorter than 75.

Steffen

Post Reply