Parse string using batch script - string

How can i parse string using batch script?
Goal is to save in a array everything under Import: and strip out "#head"
for example --> "//MPackages/Project/config/abc.txt" and "//Packages/Project/config/cde.txt"
test.txt
Version: 4.5.0
Import:
//MPackages/Project/config/abc.txt #head
//Packages/Project/config/cde.txt #head
View:
//MPackages/Project/config/ac.txt #head
//Packages/Project/config/de.txt #head
MY try
#echo off
set buildlog="devel.p4inc"
setlocal EnableDelayedExpansion
for /F "tokens=*" %%A in (devel.p4inc) do (
if /i "%%A"=="Import:" set "import=true"
IF DEFINED import (echo %%A)
)

#echo off
set "vf=version.txt"
setlocal enableDelayedExpansion
set counter=1
for /f "usebackq tokens=1 delims=#" %%a in ("%vf%") do (
set "line=%%a"
if "!line:View=!" neq "!line!" if "!in!" equ "true" (
set in=false
rem echo ###
)
if "!in!" equ "true" (
set "_!counter!_=%%a"
set /a counter=counter+1
)
rem echo !line!
if "!line:Import=!" neq "!line!" (
set in=true
rem echo --
)
)
set _
try this.It should set the desired variables in numbered list like _1_ ; _2_ ; ...

This is what I had in mind:
#echo off
setlocal EnableDelayedExpansion
set "buildlog=devel.p4inc"
set idx=0
for /F "usebackq" %%A in ("%buildlog%") do (
if defined import (
set "config=%%A"
if "!config:~0,2!"=="//" (
set "config[!idx!]=%%A"
set /a idx += 1
) else set "import="
) else if /i "%%A"=="Import:" set "import=true"
)
rem // display config array
set config[

Related

How I can search a string in a .txt?

I want to search a string in a .txt .
After identifying the line need to add another string at the end of the line...
Please Help
Example:
(
echo 3-Trip-7000-23
echo 6-Lunch-600-2
echo 7-Breakfast-15-5
) > FILE.txt
If i type Lunch it prints in that case Lunch-600-2 because search the string "Lunch" in the file, how do I have to code that... Im in windows
set variables at the top, i made this for automating rom building for decompiled framework
#echo off
setlocal enableextensions enabledelayedexpansion
set search=search for this string
set file=searchfile.txt
set addstring=add this string to the line
set count=0
for /F "usebackq tokens=*" %%A in ("%file%") do (
set /A count+=1
Echo.%%A | findstr /C:"%search%">nul && (set magicnumber=!count!)
)
set count=0
for /F "usebackq tokens=*" %%A in ("%file%") do (
set /A count+=1
if !count! == %magicnumber% echo %%A%addstring% >>tmp.txt
if !count! == %magicnumber% set line=%%A
if not !count! == %magicnumber% echo %%A >>tmp.txt
)
del /Q %file%
ren "tmp.txt" "%file%"
echo "%search%" is on line %magicnumber% and line has been changed to "%line%%addstring%"

Outputting string to a text file from a batch program [duplicate]

I have a file report.txt having comma separated values like (1,2,3,4). I am checking if the file is not blank then assign the 4 variables with values in the file. But the variables are not set. Any help why this is happening?
setlocal enabledelayedexpansion
for /f %%i in ("Report.txt") do set size=%%~zi
if %size% gtr 0 (
for /F "tokens=1-4 delims=," %%A in ("Report.txt") do (
set "var1=%%a"
set "var2=%%b"
set "var3=%%c"
set "var4=%%d"
)
set var
)
echo %var1%
You've enabled delayed expansion, but you aren't using it. In order to use delayed expansion, you need to use !variable! instead of %variable%. Additionally, the variable specified in for loops is case-sensitive, so you either need to set var1 equal to %%A or use %%a as the loop variable.
setlocal enabledelayedexpansion
for /f %%i in ("Report.txt") do set size=%%~zi
if %size% gtr 0 (
for /F "tokens=1-4 delims=," %%a in (Report.txt) do (
set "var1=%%a"
set "var2=%%b"
set "var3=%%c"
set "var4=%%d"
)
set var
)
echo !var1!
setlocal enabledelayedexpansion
for /f %%i in ("Report.txt") do set size=%%~zi
if %size% gtr 0 (
I changed the code as below and it started working. Thank you guys for your time. Appeciate it.
for /F "tokens=1-4 delims=," %%A in (%cd%\Report.txt) do (
set "var1=%%A"
set "var2=%%B"
set "var3=%%C"
set "var4=%%D"
)
set var
)
echo !var1!

How do you sort list of strings in batch?

Hi i have a script that searches for certain directories based on a string provided by the user, he then presents the directories meeting the user.s inout and offers to cd to them.
#ECHO off
setlocal enableextensions
:start
set scriptname=%0%
set PAUSE_ON_ERROR=yes
rem #
rem ###### SECTION TO CUSTOMIZE #####
rem #
rem #
rem # This is the list of directories where instances of TECSYS iTopia installed under
rem # JBoss are located.
rem #
set dir_to_search_in=C: C:\TecsysDev\iTopiaControlPanel\trunk
rem #
rem ###### END SECTION TO CUSTOMIZE #####
rem #
set argNb=0
for %%x in (%*) do Set /A argNb+=1
if %argNb% GTR 1 (
goto :showUsage
) else if %argNb% == 0 (
set ENV_NAME=*
) else (
set ENV_NAME=*%~1*
)
:MainBlock
set scriptname=%0%
cd /d %~dp0
for /f "usebackq delims=" %%D in (`cd`) do set scriptdir=%%D
for /f "usebackq delims=" %%D in (`cd`) do set CURRENT_DIR=%%D
cd "%scriptdir%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
set JBOSS_DIR=%JBOSS_DIR%\..\..\..\..\..
cd "%JBOSS_DIR%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
cd "%CURRENT_DIR%"
rem Make sure that we have the findstr utility installed.
set findstr_found=
for /f "usebackq" %%f in (`echo x ^| findstr x 2^>nul`) do set findstr_found=%%f
if "X%findstr_found%" == "X" (
echo The findstr utility is not found.
goto pauseforError
)
call :getJbossVersion
rem prepare the list of special JBoss environments to exclude
if /i "%JBOSS_VERSION%" lss "5" (
set env_to_exclude=all default minimal
) else if /i "%JBOSS_VERSION%" lss "6" (
set env_to_exclude=all default minimal standard web
) else (
set env_to_exclude=all default minimal jbossweb-standalone standard
)
rem find the environment directories
for %%f in (%dir_to_search_in%) do (
for /f "delims=" %%G in ('dir /b /ad "%%~f\jboss*"') do (
if exist "%%f\%%G\server\%ENV_NAME%" (
for /f "delims=" %%H in ('dir /b /ad "%%~f\%%~G\server\%ENV_NAME%"') do (
call :concat %%~f\%%~G\server\%%~H
)
)
)
)
if "X%environment_home_list%" == "X" (
echo Invalid %1 environment requested.
goto :pauseforError
)
rem display the folders
setlocal enabledelayedexpansion
rem checking if a unique instance of the environment exists
Set Count=0
for %%t in (%environment_home_list%) do (
Set /A Count+=1
)
if %count% == 1 (
for /f "tokens=1 delims= " %%d in ("%environment_home_list%") do (
set chosenEnv=%%d
goto :cdToEnv
)
)
:chooseEnvironment
Set Count=1
echo.
echo Please choose an environment:
echo.
echo 0^) Exit
for %%z in (%env_home_list%) do (
echo !Count!^) %%z
Set /A Count+=1
)
rem reading user input
SET /P userChoice=
set /a Test=userChoice
if !Test! EQU 0 (
if not !userChoice! EQU 0 (
echo Please enter a valid number
goto :chooseEnvironment
)
)
rem checking if the user wants to exit
if %userChoice% EQU 0 (
goto :END
)
Set /A Count-=1
rem making sure the input is valid
if %userChoice% LSS 1 (
echo Please enter a valid choice
goto :chooseEnvironment
)
if %userChoice% GTR %count% (
echo Please enter a valid choice
goto :chooseEnvironment
)
rem matching the user input with the right element from the environment list
for /f "tokens=%userChoice% delims= " %%d in ("%environment_home_list%") do (
set chosenEnv=%%d
)
:cdToEnv
rem taking the environment name to change the prompt
for /f "usebackq delims=" %%D in (`dir /b %chosenEnv%*`) do set chosenEnvName=%%D
rem setting the right path to cd to.
set chosenEnvHome=%chosenEnv%\deploy\%chosenEnvName%.ear
if not exist %chosenEnvHome% (
set chosenEnvHome=%chosenEnv%\deploy\%chosenEnvName%.war
)
if not exist %chosenEnvHome% (
set chosenEnvHome=%chosenEnv%
)
set prompt=%chosenEnvName%^>
cmd /k cd /d %chosenEnvHome%
goto :End
:concat
rem defining our list of apps installed excluding special jboss folders
set is_env_to_exclude=
for %%L in (%env_to_exclude%) do (
for /f "usebackq delims=" %%U in (`echo %1 ^| findstr %%L`) do (
set is_env_to_exclude=yes
)
)
if "X%is_env_to_exclude%" == "X" (
set environment_home_list=%1 %environment_home_list%
set env_home_list=%~n1 %env_home_list%
)
goto :eof
:getJbossVersion
for /f "usebackq tokens=2" %%v in (`echo. ^| %JBOSS_DIR%\bin\run.bat -V ^| ^
findstr "JBoss" ^| findstr /i /v "BootStrap"`) do (
set JBOSS_VERSION=%%v
)
goto :EOF
:showUsage
echo Usage: tish [environment]
echo ^|
echo +--- installed environment
:pauseforError
if "%PAUSE_ON_ERROR%" == "yes" pause
:End
My only problem is that i want the folders placed in env_home_list to be displayed alphabatically, icannot do the sort directly in the dir command, because i want to order alphabetically all the folder found and not group them by their parent folder. And i insist that i am not using a file here but only a list.
You may use the fact that Batch environment variables are automatically keept in sorted order by their names, so you need to insert the value of the variable inside the name of the variable. I suggest you to do that using the standard array naming convention and insert the value as subscript, inside the brackets:
#echo off
setlocal EnableDelayedExpansion
set list=b c a
rem Separate list elements into an array, use X as a dummy value for array elements
for %%a in (%list%) do (
set elem[%%a]=X
)
rem Process the elements in sorted order:
for /F "tokens=2 delims=[]" %%a in ('set elem[') do echo %%a
Note that this method eliminate duplicated elements and keeps just the last value. If this is a problem, you need to also insert a counter after the value (inside the brackets).

character manipulation in file cmd

this is file.txt
hello
hai
So i wanna create a program that will read this file. and then replace every ALPHABET with the succeeding one. keeping all numbers and other symbols intact.
so the file.txt would become
ifmmp
ibj
current have tried reading every \n and then reading every character in the line, but echoing them would result in it being in different lines
ie
for /f "delims=" %%i in ('^<%path% findstr /n "^"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!"
set "num=-1"
:loop
set /a num=num+1
call set "name2=%%line:~%num%,1%%"
if defined name2 (
rem set /a name2+=1 this statement wont work
echo %name2%
goto :loop )
)
but then the output i get is
echo is off
e
l
l
o
any ideas?
The set /a command does only work with numbers, you can try this:
#echo off&setlocal
set "alfa=abcdefghijklmnopqrstuvwxyza"
set "test=hello hai"
set /a num=0
:loop
call set "char=%%test:~%num%,1%%"
if not defined char goto:eof
call set "alf1=%%alfa:*%char%=%%"
if "%char%" neq " " echo %alf1:~0,1%
set /a num+=1
goto:loop
..output is:
i
f
m
m
p
i
b
j
Here is a ROT13 batch file - what you are describing is a ROT1 translation. Maybe this will work for you as it is, or give you ideas and you can modify it.
#echo off
:: by aacini
:: program works as a filter, that is, it read lines from keyboard (stdin) and send output to screen (stdout). "type filetext.txt|rot13.bat" or "rot13.bat < filetext.txt"
:: with d benham update
#echo off
:: simple rot-13 conversion filter program
setlocal disableDelayedExpansion
set lower=abcdefghijklmnopqrstuvwxyz
set upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
for /l %%a in (0,1,25) do (
set /a "rot13=(%%a+13)%%26"
setlocal enableDelayedExpansion
for %%b in (!rot13!) do for /f "tokens=1-4" %%A in (
"!lower:~%%a,1! !lower:~%%b,1! !upper:~%%a,1! !upper:~%%b,1!"
) do (
endlocal
set "lower%%A=%%B"
set "upper%%C=%%D"
)
)
for /f "delims=" %%a in ('findstr /n "^"') do (
set "line=%%a"
setlocal enableDelayedExpansion
set "line=!line:*:=!"
set output=
if defined line (
set /a len=0
for /l %%b in (12,-1,0) do (
set /a "len|=1<<%%b"
for %%c in (!len!) do if "!line:~%%c,1!" equ "" set /a "len&=~1<<%%b"
)
for /l %%b in (0,1,!len!) do (
set "char=!line:~%%b,1!"
if defined lower!char! for /f delims^=^ eol^= %%c in ("!char!") do (
if "!lower:%%c=%%c!" neq "!lower!" (
set "char=!upper%%c!"
) else set "char=!lower%%c!"
)
set "output=!output!!char!"
)
)
echo(!output!
endlocal
)

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

Resources