Broken String Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Broken String Help

#1 Post by booga73 » 07 Jul 2014 12:40

Problem, my script reads the string value, but outputs broken strings. As long as my string doesn't have space in the string, the value output is good. But when I get a string with space, my output string is broken. I've had some diffculty in the pass regarding reading string values, but need support still.

I have a script which reads a line from text. The text itself, each row has a string ending in either REG_DWORD 0x1 or REG_DWORD 0x1000.
The text itself that is being read from is generated but doesn't have all the same string values, but same similiar ending 2 tokens.

My goal is to parse each line, place the string value output to a file, but always have the last 2 tokens removed at the end.

I've tried using Setlocal EnableDelayedExpansion to allow for my string to stay together, but there wasn't any output showing up even on screen.

if I can get assistance on this, please / thank you. Booga73





    sample output on screen:
    ----------------------
    Press any key to continue . . .
    C:\Windows\system32\vfpodbc.dll
    Press any key to continue . . .
    C:\Program
    Press any key to continue . . .
    C:\Windows\Microsoft.NET\Framework\v1.0.3705\vsavb7rt.dll
    Press any key to continue . . .
    C:\Program
    Press any key to continue . . .
    C:\Windows\Microsoft.NET\Framework\v1.0.3705\system.enterpriseservices.dll
    Press any key to continue . . .
    C:\Program
    Press any key to continue . . .
    ----------------------



Sample Text:

    C:\Windows\system32\vfpodbc.dll REG_DWORD 0x1
    C:\Program Files\Hewlett-Packard\Shared\hpCaslNotification.exe.hpsign REG_DWORD 0x1
    C:\Windows\Microsoft.NET\Framework\v1.0.3705\vsavb7rt.dll REG_DWORD 0x1000
    C:\Program Files\Hewlett-Packard\Shared\hpqWmiEx.exe REG_DWORD 0x1
    C:\Windows\Microsoft.NET\Framework\v1.0.3705\system.enterpriseservices.dll REG_DWORD 0x1000
    C:\Program Files\Hewlett-Packard\Shared\HpqToaster.exe REG_DWORD 0x1



Destination File output:

    C:\Windows\system32\vfpodbc.dll
    C:\Program Files\Hewlett-Packard\Shared\hpCaslNotification.exe.hpsign
    C:\Windows\Microsoft.NET\Framework\v1.0.3705\vsavb7rt.dll
    C:\Program Files\Hewlett-Packard\Shared\hpqWmiEx.exe
    C:\Windows\Microsoft.NET\Framework\v1.0.3705\system.enterpriseservices.dll
    C:\Program Files\Hewlett-Packard\Shared\HpqToaster.exe




Code: Select all


@ECHO OFF

for /f "usebackq tokens=* delims= " %%a in ("c:\temp1\SharedDll1.txt") do (
   Call :ChkExec %%a
)


pause

:ChkExec %*
:: Setlocal EnableDelayedExpansion
set rgVal1=%*

:: echo my String value is: %rgval1%

for /f "usebackq tokens=1,2,3*" %%a in ('%rgval1%') do (

   set rval1=%%a
   set rval2=%%b
   set rval3=%%c
   set rval4=%%d
   set rval5=%%e
   set rval6=%%f
   set rval7=%%g
   set rval8=%%h

   :: echo %rval1% %rval2% %rval3% %rval4% %rval5% %rval6% %rval7% %rval8%
   echo %rval1%

pause
   
)
exit /b


booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Broken String Help

#2 Post by booga73 » 07 Jul 2014 13:09

okay, I figured out a way to just get the string and remove the last 2 tokens.

But, is there a way to not use: !rgval1:~0,-20! ?



my script which showed that it worked:

Code: Select all


Setlocal EnableDelayedExpansion
for /f "usebackq tokens=*" %%a in ("c:\irtools\SharedDll1.txt") do (
   Call :ChkExec %%a
)


pause

:ChkExec %*
Setlocal EnableDelayedExpansion
set rgVal1=%*

echo my String value is: !rgval1:~0,-20!

echo !rgval1:~0,-20! >> c:\irtools\SharedDLL3.txt


   
)
exit /b


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Broken String Help

#3 Post by Squashman » 07 Jul 2014 13:54

Would help if the program that created the TEXT file would have put QUOTES around the file path.
This is the best I could do and this will only work based on many assumptions with your input data. Probably a better way to do this.

Code: Select all

@echo off
Setlocal EnableDelayedExpansion
for /f "usebackq tokens=*" %%a in ("SharedDll1.txt") do (
   set "all=%%a"
   set "ending=!all:*REG_DWORD=!"
   call :subst "%%a" !ending!
)
pause
:subst
set "var1=%~1"
set "begin=!var1:REG_DWORD %~2=!"
echo %begin%
GOTO :EOF

output

Code: Select all

C:\Windows\system32\vfpodbc.dll
C:\Program Files\Hewlett-Packard\Shared\hpCaslNotification.exe.hpsign
C:\Windows\Microsoft.NET\Framework\v1.0.3705\vsavb7rt.dll
C:\Program Files\Hewlett-Packard\Shared\hpqWmiEx.exe
C:\Windows\Microsoft.NET\Framework\v1.0.3705\system.enterpriseservices.dll
C:\Program Files\Hewlett-Packard\Shared\HpqToaster.exe
Press any key to continue . . .

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Broken String Help

#4 Post by Squashman » 07 Jul 2014 14:04

This works as well. Basically matching a (space)REG_DWORD(space) and replacing it with (tab)REG_DWORD(tab) and then using a TAB as the delimiter in the innner FOR command.

Code: Select all

@echo off
Setlocal EnableDelayedExpansion
for /f "usebackq tokens=*" %%a in ("SharedDll1.txt") do (
   set "all=%%a"
   set "ending=!all: REG_DWORD =   REG_DWORD   !"
   FOR /f "tokens=1 delims=   " %%G in ("!ending!") do echo %%G
)
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Broken String Help

#5 Post by foxidrive » 07 Jul 2014 17:35

I like that last one - it would be useful to use a pipe instead of a TAB to avoid various text editors converting the TAB, and the problem forums have with them.

Post Reply