Edit (Find and Replace) .ini File with Batch - batch-file

I have searched for an answer to this question and I haven't understood the solution. I am trying to do a find and replace on a .ini file using batch. What Im trying to do exactly is edit php.ini and find all instances of the string "5.4.3" and replace it with "5.6.14".
I was trying to use this based on an answer I saw on stack overflow:
`set "file=C:\wamp\bin\php\php5.6.14\php.ini"
:loop
findstr %old% "%file%" >nul || (
type "%file%"|repl "5.4.3" "5.6.14" >"%file%.tmp"
move "%file%.tmp" "%file%" >nul
)
ping -n 120 localhost >nul
goto :loop`
The execution hangs and I'm not sure why. Any help would be appreciated.

If you cannot add anything (such a jrepl.bat) to the machine, you can do it yourself by using string replacement. I am not suggesting against jrepl.bat. The works of dbenham are usually very good.
=== repit.bat
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "THEFILE=C:\Users\pwatson\y\log.txt"
FOR /F "usebackq tokens=*" %%s in (`TYPE "%THEFILE%"`) DO (
SET THESTRING=%%s
ECHO !THESTRING:5.4.3=5.6.14!
)
EXIT /B
If you want to save this into a new file, use redirection.
CALL repit.bat >log.txt.new

Related

Batch file to edit .ini file but do not delete blank space

I am new to StackOverflow. I want to run a batch file to find and replace a single string in an .ini file. I tried several solutions given on stackoverflow and other sites too.
A few of them are working - but delete my other lines having "space" or ";".
Here is the string that I want to find and change in my file RDConfigSettings.ini
CommunicationMode:1
I want it vice-versa:
if it is "CommunicationMode:1" then change it to "CommunicationMode:0"
if it is "CommunicationMode:0" then change it to "CommunicationMode:1"
Here is the whole content of my RDConfigSettings.ini file
;0 for Staging, 1 for Pre-Production, 2 for Production
RDEnviroment:2
;0 for disable, 1 for Enable
RDServiceLogs:0
;0 for disable, 1 for Enable
ClientValidation:0
;Validate Management Server Certificate -- 0 for Yes, 1 for No
ValidateCertificate:0
;Proxy Configuration -- 0 for Direct Internet Access, 1 for Access via Proxy
ProxyConfig:0
ProxyIP:[Proxy IP]
ProxyPort:[Proxy Port]
;0 for Https, 1 for Http
CommunicationMode:1
;Port Range Setting in below field
PortBegin:11100
PortEnd:11120
;ManagementServerURL
Registration:https://rdm.smartbioplus.com/rdm-device-app/registration
Keyrotation:https://rdm.smartbioplus.com/rdm-key-management-app/keyRotation
Telemetry:https://rdm.smartbioplus.com/rdm-telemetry-app/telemetry
Domain:rdm.smartbioplus.com
URL_Port:443
Could anyone help me? THis is my code:
#echo off
set "file=E:\CSC Softwares\MorphoRdServiceL0Soft\RDConfigSettings.ini"
:loop
findstr "^CommunicationMode:0$" "%file%" >nul || (
type "%file%"|repl "^CommunicationMode:1" "CommunicationMode:0" >"%file%.tmp"
move "%file%.tmp" "%file%" >nul
)
timeout 120 >nul
goto :loop
Moreover, it will be a great help if someone can add an Command with administrative rights that will stop a particular service "MORPHO_RD_Service" before replacing the string and then after replace the string, start the same service again.
You have code to switch from 1 to 0, but no code to switch from 0 to 1.
Below code alternates between 1 and 0 with each run of the loop.
I also changed to jrepl (more modern and powerful). It isn't necessary (though possible) to process piped data and redirect the result to another file. The /f switch gives the inputfile to process, the /o switch gives the outputfile. By giving it a single -, it uses the same filename as the input file (and overwrites it with the new(changed) data).
#echo off
set "file=t.txt"
:loop
findstr "^CommunicationMode:" "%file%" & REM this line for troubleshooting only
findstr "^CommunicationMode:0$" "%file%" >nul && (
call jrepl "CommunicationMode:0" "CommunicationMode:1" /f "%file%" /o -
) || (
call jrepl "CommunicationMode:1" "CommunicationMode:0" /f "%file%" /o -
)
timeout 1 >nul
goto :loop
Don't forget to adapt the data file name and the timeout to your needs.
Without the need for an external utility such as jrepl, which is great for some things, but not needed for such a task:
#echo off
setlocal enabledelayedexpansion
set "file=E:\CSC Softwares\MorphoRdServiceL0Soft\RDConfigSettings.ini"
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>"%file%"') do (
set "line=%%j"
if "!line!" == "CommunicationMode:1" (
set "line=!line:1=0!"
set "hold=!line!"
) else if "!line!" == "CommunicationMode:0" (
set "line=!line:0=1!"
set "hold=!line!"
)
echo(!line!>>"!file!"
)
echo Changed to !hold!
pause

Batch-File: Find String inside String does not work

I have tried to use the answer mentioned from here: [Find Substring in String] (Batch file: Find if substring is in string (not in a file))
I try to adapt the solution mentiones in the commands, so that I have my SearchVal saved inside a variable so this can be changed during runtime.
Minimal example:
set searchVal="cde"
set str1="abcdef"
setlocal enabledelayedexpansion
if not "x!str1:%searchVal%=!"=="x%str1%" echo It contains my subs
endlocal
pause
In my opinion this little batch should display that the strings contains my subs, however nothing is shown and I do not know why as I directly make use of the solution that should be working.
EDIT
Thanks to the commands I found my mistake.
In my current situation I look at files inside a folder and save the filename inside an array while doing a for-loop:
for /f "tokens=1 delims=" %%G in ('pathToFolder\*.properties /b') do (
if not "%%~G:%searchVal%=!"=="%%~G" echo It contains my subs !ID_Properties!
set filename[!ID_Properties!]=%%~G
set /a ID_Properties+=1
)
... where ID_properties is just a counter and searchVal my string I am looking for. Does anyone know how I can use the %%G inside the loop in the correct way so the search works as before?
Your for-loop syntax is not correct it seems like a mixture between executing a dir command and looping through files. I'll stick with the dir command option and using usebackq.
#echo off
setlocal EnableDelayedExpansion
set searchVal=cde
set ID_Properties=0
for /f "usebackq tokens=1 delims=" %%G in (`dir pathToFolder\*.properties /b`) do (
set file=%%G
if not "!file:%searchVal%=!"=="!file!" (
echo It contains my subs !ID_Properties!
set filename[!ID_Properties!]=!file!
set /a ID_Properties+=1
)
)
Filling the array is only done if the file contains your searchVal; don't know if this was/is you intention.
a For loop might be a bit much, depending on what you plan to do with the output. findstr is maybe a shorter option:
findstr /im "cde" *.properties && echo - found || echo not found
and add /s if you require recursive search through subdirectories:
findstr /ims "cde" *.properties && echo - found || echo not found

how to set a variable for the value of " ping 8.8.8.8 | find /c "TTL=" " [duplicate]

This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
Closed 9 months ago.
I'm trying to assign the output of a command to a variable - as in, I'm trying to set the current flash version to a variable. I know this is wrong, but this is what I've tried:
set var=reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion>
or
reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion >> set var
Yeah, as you can see I'm a bit lost. Any and all help is appreciated!
A method has already been devised, however this way you don't need a temp file.
for /f "delims=" %%i in ('command') do set output=%%i
However, I'm sure this has its own exceptions and limitations.
This post has a method to achieve this
from (zvrba)
You can do it by redirecting the output to a file first. For example:
echo zz > bla.txt
set /p VV=<bla.txt
echo %VV%
You can't assign a process output directly into a var, you need to parse the output with a For /F loop:
#Echo OFF
FOR /F "Tokens=2,*" %%A IN (
'Reg Query "HKEY_CURRENT_USER\Software\Macromedia\FlashPlayer" /v "CurrentVersion"'
) DO (
REM Set "Version=%%B"
Echo Version: %%B
)
Pause&Exit
http://ss64.com/nt/for_f.html
PS: Change the reg key used if needed.
Okay here some more complex sample for the use of For /F
:: Main
#prompt -$G
call :REGQUERY "Software\Classes\CLSID\{3E6AE265-3382-A429-56D1-BB2B4D1D}"
#goto :EOF
:REGQUERY
:: Checks HKEY_LOCAL_MACHINE\ and HKEY_CURRENT_USER\
:: for the key and lists its content
#call :EXEC "REG QUERY HKCU\%~1"
#call :EXEC "REG QUERY "HKLM\%~1""
#goto :EOF
:EXEC
#set output=
#for /F "delims=" %%i in ('%~1 2^>nul') do #(
set output=%%i
)
#if not "%output%"=="" (
echo %1 -^> %output%
)
#goto :EOF
I packed it into the sub function :EXEC so all of its nasty details of implementation doesn't litters the main script.
So it got some kinda some batch tutorial.
Notes 'bout the code:
the output from the command executed via call :EXEC command is stored in %output%. Batch cmd doesn't cares about scopes so %output% will be also available in the main script.
the # the beginning is just decoration and there to suppress echoing the command line. You may delete them all and just put some #echo off at the first line is really dislike that. However like this I find debugging much more nice.
Decoration Number two is prompt -$G. It's there to make command prompt look like this ->
I use :: instead of rem
the tilde(~) in %~1 is to remove quotes from the first argument
2^>nul is there to suppress/discard stderr error output. Normally you would do it via 2>nul. Well the ^ the batch escape char is there avoids to early resolving the redirector(>). There's some simulare use a little later in the script: echo %1 -^>... so there ^ makes it possible the output a '>' via echo what else wouldn't have been possible.
even if the compare at #if not "%output%"==""looks like in most common programming languages - it's maybe different that you expected (if you're not used to MS-batch). Well remove the '#' at the beginning. Study the output. Change it tonot %output%==""-rerun and consider why this doesn't work. ;)
This is work for me
#FOR /f "delims=" %i in ('reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion') DO set var=%i
echo %var%

Pull string from text file and rename the file using that text. For /F? Delims?

Hello I am a little out of my depth here so hence the question. I have tried reading past post but due to my unfamiliarity I am unable to adapt other solutions into mine. I also don't really have proper code in place but I'm hoping that because my question seems simple maybe someone can help me or direct me. Thank you.
I have a program that exports EDI data into a file called FOUT_R.edi and I need a batch script that will look into the file, pull the string from line 4 value 3 and line 7 value 3 and then rename the file using the combined values. The values are asterisks delimited. Below is a sample...
ISA*00* *00* *ZZ*JK60 *ZZ* MFG *:
GS*IN*JK60* MFG*20170223*1531*1*X*004010
ST*810*0001
BIG*20170223*237454
CUR*SE*CAD
REF*SI*238972
N1*ST*VEHICULOS COMERCIALES MEXICO S.A. DE C.V*92*065
N1*SU*METAL*92*JK60
DTM*011*20170223
So in the end I want the file name to read
"237454 VEHICULOS COMERCIALES MEXICO S.A. DE C.V.edi"
(.edi being the file extension). Again not sure what I'm doing, my starter scripts look like this
for /f "delims=*" %%fname1 IN (FOUT_R.edi) DO ren FOUT_R.edi %%fname1.edi
but it's obviously wrong it was just a starting spot for me but I realize now that this is out of my league.
Can someone please help or direct me to a site where the most inexperienced scripters can be explained in baby steps?
thank you.
Provided the name in line 7 doesn't contain colons, this should do:
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set "FileName=FOUT_R.edi"
set "NewName="
for /f "tokens=4 delims=:*" %%A in (
'findstr /N "^" "%FileName%" ^|findstr "^4: ^7:"'
) Do Set "NewName=!NewName! %%A"
Echo Ren "%FileName%" "%NewName:~1%.edi"
timeout -1
If the output looks ok, remove the Echo
#echo off
setlocal
set "filename=FOUT_R.edi"
for /F "skip=3 tokens=3 delims=*" %%a in (%filename%) do set "Line4value3=%%a" & goto break1
:break1
for /F "skip=6 tokens=3 delims=*" %%a in (%filename%) do set "Line7value3=%%a" & goto break2
:break2
ren %filename% %Line4value3%%Line7value3%.edi
Sorry, no explanations; look for they yourself: open a command-prompt session and type the command followed by /?.
Note that if the file name may contain spaces, it must be enclosed in quotes.

Batch programing to search some file in sub folders

I have hundreds of csv files . csv files are stored in folders and sub ​​folders . I want to search fifty csv file whose file names have been determined , for example 1.csv , 2.csv , 3.csv , ... , 50.csv . very troublesome if I searched one by one using the Windows search tool . I would like if the files are found , save in the folder named FOUND . please help to overcome this problem by using the batch programming / bat ? thank you very much
There's a number of approaches one can take, depending on how much automation you require... To help you get started, you may want to look at this it helped me (and indeed continues to do so) when I started learning batch. Furthermore I will provide one possible template for achieving your objective, as I have interpreted it. Perhaps it is not the most elegant or efficient method, but it introduces a number of batch commands that you may or may not have encountered, which in turn may help you develop your own method.
#echo off
setlocal enabledelayedexpansion
echo Please enter a drive letter:
set /p "drive=>"
echo Please enter a search string:
set /p "searchstring=>"
echo %searchstring%>search.txt
set /p search=<search.txt
set /a suffix=0
echo.>>search.txt
:LOOP
for /f "tokens=*" %%i in ("search.txt") do (
set /a suffix=suffix+1
set seq=%search% !suffix!
echo !seq!>>search.txt
)
if !suffix! leq 49 goto LOOP
for /f "tokens=*" %%i in (search.txt) do (
for /f "tokens=*" %%j in ('dir /b /s /a-d %drive%:\"%%i.csv" 2^>nul') do (
if not exist "%~dp0\found" md "%~dp0\found"
move /y "%%j" "%~dp0\found\%%~nxj"
)
)
pause
This is not intended as a definitive solution, though you may find it answers your original query/request. All the best.
Here's another working solution for you..
#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM First Set your directories input and output
SET InputDir=C:\Directory to your CSV files\
SET OutputDir=C:\Directory to your CSV files\FOUND
REM check if the FOUND directory exist, if not, then create it.
IF NOT EXIST OutputDir (
mkdir %OutputDir%
)
REM Grab a scan of the input directory and save it to a temporary file list.
Dir /a /b %InputDir%>"%OutputDir%\Found.txt"
REM Set the files you would like to find.
SET "File1=1.csv"
SET "File2=2.csv"
SET "File3=50.csv"
REM The loop, to process the matching file(s).
FOR %%A IN (%File1%,%File2%,%File3%) DO (
FOR /F "usebackq" %%B IN ("%OutputDir%\Found.txt") DO (
IF %%A==%%B (
copy "%InputDir%\%%A" "%OutputDir%\%%A"
)
)
)
REM Clean up the temp file list.
DEL "%OutputDir%\Found.txt"
Make note, I didn't add quotes to the Input and Output variables, but instead added quotes to the copy portion of the code to compensate for white spaces in your directory path. I tried to keep it simple, so you could follow the logic of how it processed what you are looking for, you can now modify this to your liking.. Have fun. Cheers!

Resources