How to view through a byte order mark in batch using findstr

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zask
Posts: 26
Joined: 14 Dec 2015 17:58

How to view through a byte order mark in batch using findstr

#1 Post by zask » 16 Apr 2017 14:22

Hello, I have a trick that many may not know

If you copy and paste this in the beginning of your batch files, it will
Entirely encode the contents of the script with a byte order mark. however it is possible to
View through the byte order mark in hex editors.

This is the file that has the byte order mark.

ÿþ
cls
Echo :TEST
Pause

I'm having an issue, I need to
Use findstr to view in batch and exe files with raw text, I wanted to be able to make it
Possible to view within these files with the byte order mark, the only issue is that I can't view
Trough the Chinese looking characters, what I need is either a way to get around the byte order mark with findstr or at least make it possible to not encode the key word ":TEST" in the bom file.

This is the file that searches if the phrase ":TEST" exist within the batch/exe files.

@echo off
for %%C in (*.exe *.bat) do (
findstr /c:":TEST" "%%~C" > nul && (echo yes) || (echo no) )
Pause

Thanks, I've asked on other websites on the internet but no one can really gives me a response on this topic.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to view through a byte order mark in batch using findstr

#2 Post by penpen » 17 Apr 2017 03:23

I'm a little bit confused:
If i understand your task right, then you are nearly completely giving the answer to it yourself.
You may consult the findstr help.

Code: Select all

@echo off
for %%C in (*.exe *.bat) do (
   findstr /r /c:"^" "%%~C"
)
:see findstr help for further information about "/r" and "^"
findstr /?
Pause


penpen

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: How to view through a byte order mark in batch using findstr

#3 Post by eax22 » 17 Apr 2017 05:01

zask wrote:I need to use findstr to view in batch and exe files with raw text.


I am not sure I understand you, but as far as I know you can't view the contents of exe files in a reasonable way using a text editor, the closest you'll come to viewing exe files is through programs like OllyDbg.

zask
Posts: 26
Joined: 14 Dec 2015 17:58

Re: How to view through a byte order mark in batch using findstr

#4 Post by zask » 17 Apr 2017 13:55

eax22 wrote:
zask wrote:I need to use findstr to view in batch and exe files with raw text.


I am not sure I understand you, but as far as I know you can't view the contents of exe files in a reasonable way using a text editor, the closest you'll come to viewing exe files is through programs like OllyDbg.


Mybad I should have explained better, okay let's say I had a folder with a bunch of batch files, each batch file is encoded with the byte order mark

By placing this in the beginning of the batch file, if you save the file and reopen it,
All of the code from the script turns into some weird looking Chinese characters that basically disguise the code, but the code will still work without error.

ÿþ
cls

I want to use a different batch file that searches into other batch files (the one with the byte order mark) by using findstr to view the key word ":TEST" inside of the files with the byte order mark, how ever since the characters are turned Chinese like, it interferes with findstr and prevents me from finding the phrase ":TEST". I need a way to view through the byte order mark with findstr in batch files that have the BOM.

zask
Posts: 26
Joined: 14 Dec 2015 17:58

Re: How to view through a byte order mark in batch using findstr

#5 Post by zask » 17 Apr 2017 14:13

eax22 wrote:
zask wrote:I need to use findstr to view in batch and exe files with raw text.


I am not sure I understand you, but as far as I know you can't view the contents of exe files in a reasonable way using a text editor, the closest you'll come to viewing exe files is through programs like OllyDbg.


Okay I tested it and it works! Thank you exactly what I needed.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to view through a byte order mark in batch using findstr

#6 Post by penpen » 17 Apr 2017 16:23

zask wrote:how ever since the characters are turned Chinese like, it interferes with findstr and prevents me from finding the phrase ":TEST".
Yes it interferes with notepad (and any other text editor that recognizes boms),
but no it doesn't interfere with findstr ("bomTest.bat"):

Code: Select all

@echo off
setlocal enableExtensions

:: create all byte order mark sample files
for /f "tokens=1*" %%a in (" sample.utf8.bom.txt"^

"ÿþ sample.utf16-le.bom.txt"^

"þÿ sample.utf16-be.bom.txt") do (
   echo(%%~a
   echo(cls
   echo(Echo :TEST
   echo(Pause
) >"%%~b"

:: create all byte order mark sample files
for /f "tokens=1*" %%a in (" sample2.utf8.bom.txt"^

"ÿþ sample2.utf16-le.bom.txt"^

"þÿ sample2.utf16-be.bom.txt") do (
   echo(%%~a
   echo(cls
   echo(Pause
) >"%%~b"

:: findstr is working ok (full text)
for %%a in (*.bom.txt) do (
   echo("%%~a"
   findstr /r /c:"^" "%%~a"
   echo(
)

:: findstr is working ok (finding the ":TEST" label)
for %%a in (*.bom.txt) do (
   <nul set /p "=%%~a: "
   findstr /c:"TEST" "%%~a" > nul && (echo yes) || (echo no)
)

pause
goto :eof

Result:

Code: Select all

Z:\>bomTest.bat
"sample.utf16-be.bom.txt"
■ 
cls
Echo :TEST
Pause

"sample.utf16-le.bom.txt"
 ■
cls
Echo :TEST
Pause

"sample.utf8.bom.txt"
´╗┐
cls
Echo :TEST
Pause

"sample2.utf16-be.bom.txt"
■ 
cls
Pause

"sample2.utf16-le.bom.txt"
 ■
cls
Pause

"sample2.utf8.bom.txt"
´╗┐
cls
Pause

sample.utf16-be.bom.txt: yes
sample.utf16-le.bom.txt: yes
sample.utf8.bom.txt: yes
sample2.utf16-be.bom.txt: no
sample2.utf16-le.bom.txt: no
sample2.utf8.bom.txt: no
Drücken Sie eine beliebige Taste . . .

Z:\>


penpen

zask
Posts: 26
Joined: 14 Dec 2015 17:58

Re: How to view through a byte order mark in batch using findstr

#7 Post by zask » 17 Apr 2017 20:24

penpen wrote:
zask wrote:how ever since the characters are turned Chinese like, it interferes with findstr and prevents me from finding the phrase ":TEST".
Yes it interferes with notepad (and any other text editor that recognizes boms),
but no it doesn't interfere with findstr ("bomTest.bat"):

Code: Select all

@echo off
setlocal enableExtensions

:: create all byte order mark sample files
for /f "tokens=1*" %%a in (" sample.utf8.bom.txt"^

"ÿþ sample.utf16-le.bom.txt"^

"þÿ sample.utf16-be.bom.txt") do (
   echo(%%~a
   echo(cls
   echo(Echo :TEST
   echo(Pause
) >"%%~b"

:: create all byte order mark sample files
for /f "tokens=1*" %%a in (" sample2.utf8.bom.txt"^

"ÿþ sample2.utf16-le.bom.txt"^

"þÿ sample2.utf16-be.bom.txt") do (
   echo(%%~a
   echo(cls
   echo(Pause
) >"%%~b"

:: findstr is working ok (full text)
for %%a in (*.bom.txt) do (
   echo("%%~a"
   findstr /r /c:"^" "%%~a"
   echo(
)

:: findstr is working ok (finding the ":TEST" label)
for %%a in (*.bom.txt) do (
   <nul set /p "=%%~a: "
   findstr /c:"TEST" "%%~a" > nul && (echo yes) || (echo no)
)

pause
goto :eof

Result:

Code: Select all

Z:\>bomTest.bat
"sample.utf16-be.bom.txt"
■ 
cls
Echo :TEST
Pause

"sample.utf16-le.bom.txt"
 ■
cls
Echo :TEST
Pause

"sample.utf8.bom.txt"
´╗┐
cls
Echo :TEST
Pause

"sample2.utf16-be.bom.txt"
■ 
cls
Pause

"sample2.utf16-le.bom.txt"
 ■
cls
Pause

"sample2.utf8.bom.txt"
´╗┐
cls
Pause

sample.utf16-be.bom.txt: yes
sample.utf16-le.bom.txt: yes
sample.utf8.bom.txt: yes
sample2.utf16-be.bom.txt: no
sample2.utf16-le.bom.txt: no
sample2.utf8.bom.txt: no
Drücken Sie eine beliebige Taste . . .

Z:\>


penpen


Wow thanks! But I think I've finally realized what you meant now by giving myself the answer, weird, the thing is I've tried this earlier in school and I couldn't get the findstr to work correctly with the byte order mark, i don't know why but Ill try to see why it wasn't working when I get back over there again.

Post Reply