DOS Batch script to convert string 2 hex - string

How can I convert sring to hex in DOS Batch script?
For example, convert "abcd" to "61626364".
Since, 'a' is 0x61...
I tried to find a solution from web, a day, but could not find my answer.

#echo off
setlocal EnableDelayedExpansion
rem Store the string in chr.tmp file
set /P "=%~1" < NUL > chr.tmp
rem Create zero.tmp file with the same number of Ascii zero characters
for %%a in (chr.tmp) do fsutil file createnew zero.tmp %%~Za > NUL
rem Compare both files with FC /B and get the differences
set "hex="
for /F "skip=1 tokens=2" %%a in ('fc /B chr.tmp zero.tmp') do set "hex=!hex!%%a"
del chr.tmp zero.tmp
echo %hex%
Output example:
C:\> test.bat abcd
61626364

:stringToHex
#echo off
del tmp.hex >nul 2>nul
del tmp.str >nul 2>nul
if "%~1" equ "" (
echo no string passed
exit /b 1
)
echo|set /p=%~1 >tmp.str
::(echo(%~1)>tmp.str
rem certutil -dump tmp.str
certutil -encodehex tmp.str tmp.hex >nul
setlocal enableDelayedExpansion
set "hex_str="
for /f "usebackq tokens=2 delims= " %%A in ("tmp.hex") do (
set "line=%%A"
set hex_str=!hex_str!!line:~0,48!
set hex_str=!hex_str: =!
)
set hex_str=%hex_str:~0,-2%
echo !hex_str!
!!Mind that stackoverflow editor might corrupt tab character.tokens=2 delims= " there should be single TAB after delims.
requires the string passed as an argument.Take a look also at debnham's :hexDump function which you can use instead of certutil.

Related

Batch Script - Find words that contain a certain string withing a file

I have a file with a lot of text.
EG
Hello
This is my file
this is the end of the file
I need a script that will search the file and pull out all words (just the words and not the line into another file) that contain for example the letter e
In this case the new file would look like
Hello
file
the
end
the
file
It may also need to search for as another example bh. (including the full stop) so a file with the following
hello
bh.ah1
my file
the end
would produce a file with
bh.ah1
hope this is enough detail
#ECHO OFF
SETLOCAL
SET "target=%~1"
FOR /f "delims=" %%a IN (q22560073.txt) DO CALL :findem %%a
GOTO :EOF
:findem
SET candidate=%1
IF NOT DEFINED candidate GOTO :EOF
ECHO %1|FIND /i "%target%" >NUL
IF NOT ERRORLEVEL 1 ECHO(%1
shift
GOTO findem
I used a file named q22560073.txt for my testing.
To find the text string, use
thisbatch text
so
thisbatch e
would find the first list and
thisbatch bh.
the second.
(I combined both sample testfiles as q22560073.txt)
the /i in the find command makes the test case-insensitive.
To output to a file, simply use
thisbatch text >"filename"
where the "rabbits ears" are only required if the filenames contains spaces and other problematic characters, but do no harm in any case.
This should work for a target of any alphabetic or numeric combination plus full stop. It will not work with characters that have a special meaning to cmd.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "target=%~1"
FOR /f "delims=" %%a IN (q22560073.txt) DO (
SET "line=%%a"
SET "line=!line:(= !"
SET "line=!line:)= !"
CALL :findem !line!
)
GOTO :EOF
:findem
SET candidate=%1
IF NOT DEFINED candidate GOTO :EOF
ECHO %1|FIND /i "%target%" >NUL
IF NOT ERRORLEVEL 1 ECHO(%1
shift
GOTO findem
revised on further information.
#echo off
set "searchfor=bh."
for /f "delims=" %%i in (t.t) do (
for %%j in (%%i) do (
echo %%j|find "%searchfor%" >nul && echo %%j
)
)
for every line (%%i) do
for every word in this line (%%j) do
if searchstring found then echo word
EDIT to your comment: replace the ( with a space in the line, before processing words
#echo off
setlocal enabledelayedexpansion
set "searchfor=bh."
for /f "delims=" %%i in (t.t) do (
set t=%%i
set t=!t:(= !
for %%j in (!t!) do (
echo %%j|find "%searchfor%" >nul && echo %%j
)
)
You can do this for more characters with additional lines like set t=!t:(= ! (replace ( with )

My batch script bad counting occurrences of string in a file

#echo off
set /a count = 0
for /f "delims=" %%a in ('dir "%~1" /a:-d /b') do call :next "%%a" "%~2"
echo found %count% occurances of "%~2"
pause
GOTO:EOF
:next
set num=
for /f "delims=" %%b in ('find /c %2 ^< %1') do set num=%%b
set /a count=count+num
My code is wrong count of the text specified in the parameter. What's the problem?
As Mark said, find return the number of lines that match in a file, not the number of individual strings in one line. To do so, you need to use another method, for example:
#echo off
setlocal EnableDelayedExpansion
set /a count = 0
for /f "delims=" %%a in ('dir "%~1" /a:-d /b') do (
for /F "delims=" %%b in ('find %2 ^< "%%a"') do call :next "%%b" "%~2"
)
echo found %count% occurances of "%~2"
pause
GOTO:EOF
:next
set num=0
set "line=%~1"
:nextMatch
set "line2=!line:*%~2=!"
if "!line2!" equ "!line!" goto endMatchs
set /A num+=1
set "line=!line2!"
if defined line goto nextMatch
:endMatchs
set /a count=count+num
For example:
C:> type 1.txt
An example of text file.
This example line have two "example" words.
End of example.
C:> test 1.txt "example"
found 4 occurances of "example"
find will count the number of lines that match your string, so searching 'xyyx' for 'x' will count as one match, even though the x's aren't in a row. If that's not what you want, you'll need a different tool.

A Batch file to read a file and replace a string with a new one

I want to create a batch file to read every line of a file in a loop and replace a string with another one. Following is my code snippet:
for /F "tokens=*" %%i in (myfile) do (
set str=%%i
set str=%str: %oldstring% = %newstring%%
echo %str% >> newfile
)
This results in a newfile with 'Echo is off' as many lines as there are in myfile. Seems like str variable holds no value at all when assigned to %%i. Can someone help me?
Try out this small script:
#echo off
set val=50
echo %val%
for /l %%i in (1,1,1) do (
set val=%%i
echo %val%
)
echo %val%
pause>nul
The output is:
50
50
1
Not what you expected, right?
That's because in a for loop, variables aren't updated until the loop has finished. To combat this, you can use setlocal enabledelayedexpansion, and replace the percent signs (%) with an exclamation mark (!):
#echo off
setlocal enabledelayedexpansion
set val=50
echo %val%
for /l %%i in (1,1,1,) do (
set val=%%i
echo !val!
)
echo %val%
pause>nul
The output:
50
1
1
The reason the str variable holds no value (during the for loop) is because it hasn't been set beforehand.
So, with these quick modifications, your script will work...
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (myfile) do (
set str=%%i
set str=!str: %oldstring% = %newstring%!
echo !str! >> newfile
)
By the way, this snippet is assuming that oldstring and newstring won't be set within the forloop, otherwise things will get messy.
Have fun.
having spent some time at this I got the correct way:
#echo off
setlocal enabledelayedexpansion
set oldstring=AF-07295
set /a count=1000
for %%f in (*.*) do (
set /a count=!count!+1
for /f "tokens=*" %%i in (%%f) do (
set str=%%i
call set str=%%str:!oldstring!=!count!%%
echo !str! >> %%~nf.ordnew
)
)
endlocal
setlocal ENABLEDELAYEDEXPANSION
set filein="c:\program files\test1.txt"
set fileout="c:\program files\test2.txt"
set old=#VERSION#
set new=2.0.3
for /f "tokens=* delims=ΒΆ" %%i in ( '"type %filein%"') do (
set str=%%i
set str=!str:%old%=%new%!
echo !str! >> %fileout%
)
working perfect
and isn't removing white spaces at the begining of the lines file

Extract section of a text file

I am trying to write a batch file that will extract lines 6000 to 6999 in a give text file. From googling I have come accross the following code - however this is giving me a blank output file.
#echo off
SetLocal EnableDelayedExpansion
type nul > nodedata.txt
set StartText=6000
set EndText=7000
set Flag=0
for /f "tokens=* delims=" %%a in ('type out.txt') do (
if /i "%StartText%" EQU "%%a" (set Flag=1)
if /i "%EndText%" EQU "%%a" (set Flag=0)
if !Flag! EQU 1 echo %%a >> nodedata1.txt
)
Any ideas as to where I am going wrong?
Here is a quick and simple pure batch solution
for /l %%a in (6000,1,6999) do (
more C:\file.txt +%%a >>C:\extracted.txt
)
You should install unxutils and then see answers for this question... Windows is just not made for text processing...
A Windows user...
This is a Batch solution that run faster...
#echo off
SetLocal EnableDelayedExpansion
set count=0
(for /F "skip=5999 delims=" %%a in (out.txt) do (
echo %%a
set /A count+=1
if !count! equ 1000 goto endLoop
)
) > nodedata1.txt
:endLoop

Using DOS batch script: find a line in a properties file and replace text

Trying to replace a string in a properties file on a certain line by using a batch file. I know that this can be done WITHOUT the use of a temp file, as I have seen it before, but forgotten how to do it.
I know that if I have a var.properties file that contains this:
CLASSPATH=bsh.jar;other.jar
VARTEST=dummy
ANOTHERVAR=default
I am trying to update the CLASSPATH value in the .properties file without changing the order of the properties file.
This is a properties file and so I believe the answer would be related to using:
for /f "tokens=1,2* delims==" %%i in (var.properties) do (
#echo Key=%%i Val=%%j
)
Instead of findstr use find with the /v and /i switches on "classpath". This will OMIT returning the line with classpath in it, then you can echo what you want in the file along w/VARTEST=dummy
SET NEWVAL=CLASSPATH=test.jar
SET FILE=think.properties
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%" ^|FIND /V /I "classpath"`) DO (
ECHO CLASSPATH=%NEWVAL%>>"%FILE%"
ECHO %%A>>"%FILE%"
)
EDIT:
SETLOCAL ENABLEDELAYEDEXPANSION
SET NEWVAL=test.jar
SET OLDFILE=OLD_think.properties
SET NEWFILE=think.properties
SET COUNT=1
MOVE "%NEWFILE%" "%OLDFILE%"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%OLDFILE%" ^|FIND /C /I "classpath"`) DO (
SET LINE=%%A
)
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%OLDFILE%"`) DO (
IF %COUNT% NEQ %LINE% (ECHO %%A>>"%NEWFILE%") ELSE (ECHO %NEWVAL%>>"%NEWFILE%")
SET /a COUNT=!COUNT!+1
)
Basically states,
rename think.properties to OLD_think.properties
read OLD_think.properties and find the line number with string
"classpath" in it and set it to variable LINE
Find all lines in OLD_think.properties, and echo them into think.properties. once you reach the line where your "CLASSPATH" string existed, it inserts the new line you wanted to replace it with, and everything else stays the same.
I finally broke down and accepted a method using a "temp" file. Using delayed expansion with the '!' character solved my question. Much of this success was due to input by mecaflash .
You can call this script with: CALL Script.bat PropKey NewPropValue Filename
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=UseCompression
SET PROPVAL=false
SET FILE=config.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end

Resources