Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 225 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
batch: replace a line in a text file

#1
<br />
I'm trying to replace this line:<br />

# forward-socks5 / 127.0.0.1:9050 .
with this one:

forward-socks5 / 127.0.0.1:9050 .
this line belongs to a config file that has to be enabled (UN-commented) by deleting the # sign from the beginning and I could not thought of a better way other than replacing the line with another without the # sign.<br />
any other thoughts or ways would be very useful.

btw, the spaces before the text are there also.<br />I have pasted the text as it was in the original file.

thanks in advance


----------


EDIT:<br />
I have somehow managed to do the line addition and removing using two peaces of code that I've found.
my only problem is that the following code removes every bit of exclamation in the output file!

@echo off

:Variables
SETLOCAL ENABLEDELAYEDEXPANSION
set InputFile=config.txt
set OutputFile=config-new.txt
set _strFind=# forward-socks5 / 127.0.0.1:9050 .
set _strInsert= forward-socks5 / 127.0.0.1:9050 .
set i=0

:Replace
for /f "usebackq tokens=1 delims=[]" %%A in (`find /n "%_strFind%" "%InputFile%"`) do (set _strNum=%%A)
for /f "usebackq delims=" %%A in ("%InputFile%") do (
set /a i = !i! + 1
echo %%A>>"%OutputFile%"
if [!i!] == [%_strNum%] (echo %_strInsert%>>"%OutputFile%")
)
type %OutputFile% | findstr /i /v /c:"%_strFind%">config-new2.txt



I was wondering if there is any way to do both the find/delete/add line in one step (not two steps as mine)...
Reply

#2
Lines containing `!` are corrupted because delayed expansion occurs after FOR variable (`%%A`) expansion. That could be solved by starting out with delayed expansion disabled. Within the loop you save the value of `%%A` in a variable, and then toggle delayed expansion on, process the line, and then toggle it back off.

You do not need to expand the variable within a SET /A computation.

You can do everything in one step by adding an IF statement within your loop.

In fact, you don't even need SET /A or FINDSTR at all. Your IF statement can test if the line matches your search string. You don't really need delayed expansion for your problem.

It is more efficient to enclose the entire loop within parens and redirect to your output file just once.

@echo off
setlocal disableDelayedExpansion

:Variables
set InputFile=config.txt
set OutputFile=config-new.txt
set "_strFind=# forward-socks5 / 127.0.0.1:9050 ."
set "_strInsert= forward-socks5 / 127.0.0.1:9050 ."

:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
)
)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through