1.

Solve : "Search and replace" script?

Answer»

Hi,

I have a text file call Test.txt built like this:

"aaaaaaaaaaaa"
"bbbbbbbbbbbb"
"cccccccccccccccc"

Is there a way with a batch file, to remove all the " (double quote) character in the text file.

Thanks for your help.

benqc.You can remove quotes from a variable with the following:

:: Remove quotes
SET v_string=###%v_string%###
SET v_string=%v_string:"###=%
SET v_string=%v_string:###"=%
SET v_string=%v_string:###=%

Unfortunately the above will fail if the
variable is NULL

or:
:: A routine that will reliably remove quotes from a variable's contents.

:: This routine will only affect items that both begin AND end with
:: a double quote.
:: e.g. will turn "C:\Program Files\somefile.txt"
:: into C:\Program Files\somefile.txt
:: while still preserving cases such as Height=5'6" and Symbols="[emailprotected]#


::BEGIN FUNCTION::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF

:: Removes the outer set of double quotes from a variable.
:: Written by Frank P. Westlake, 2001.09.22, 2001.09.24
:: MODIFIED by Simon Sheppard 2002.06.09

:: Usage as a function within a script:
:: CALL :DeQuote VariableName
::
:: Calling as a function from another batch file:
:: CALL DeQuote.cmd VariableName
::
:: If the first and last characters of the variable contents are double
:: quotes then they will be removed. This function preserves cases such as
:: Set Height=5'6" and Set Symbols="[emailprotected]#
::
:: If a variable is quoted twice and has delimiters then you will
:: need to run the function twice to remove both sets.
:: Set var=""Two Quotes;And,Delimiters=Fails""
::
:: If the variable name itself contains spaces the routine will fail
:: e.g. %v_my_variable% rather than %my variable%

:DeQuote
SET DeQuote.Variable=%1
CALL Set DeQuote.Contents=%%%DeQuote.Variable%%%
Echo.%DeQuote.Contents%|FindStr/brv ""^">NUL:&&Goto :EOF
Echo.%DeQuote.Contents%|FindStr/erv ""^">NUL:&&Goto :EOF

Set DeQuote.Contents=####%DeQuote.Contents%####
Set DeQuote.Contents=%DeQuote.Contents:####"=%
Set DeQuote.Contents=%DeQuote.Contents:"####=%
Set %DeQuote.Variable%=%DeQuote.Contents%

Set DeQuote.Variable=
Set DeQuote.Contents=
Goto :EOF
::END FUNCTION::::::::::::::::::::::::::::::::::::::::::::::::::::::

DellThis Batch-Script removes all " characters in the file:

@Echo off

set datei=c:\test.txt
for /f %%a in ('type %datei%') do call :sub1 %%a

set datei=
set var=
goto :eof

:sub1 %%a
set var=%1
echo %var:"= %

:eof


hope it helps
uli

I give another for loop for free :-) :-)Along the same lines, how can you search a file and replace one word with another.

I have a file (test.txt) with the following:

NAME=Debra

And I want to run a batch file that changes this line to read:

NAME=BrianI need to make a batchfile in which the user may enter the text that is to be searched and replaced for in a given text-file.

Any suggestions?u can use set /p to prompt set a variable

Code: [Select]@echo off
set txtfile=D:\test.txt
set newfile=D:\new_test.txt
if exist "%newfile%" del /f /q "%newfile%"
set /p search=Search String=
set /p replace=Replace With=
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
call set newline=%%newline:%search%=%replace%%%
call echo %%newline%% >>%newfile%
)
EXCELLENT, Fen Li.One more thing:

Can I ask for user input to "textfile" and "newfile" by using the same script (set /p) ?yes, same as prompt Search & Replace Variable

set /p textfile=Open File (FullPath)=
set /p newfile=Save As (FullPath)=
I also need to make a multiple search/replace in the batchfile.

If I copy the search/replace-script the program only adds more lines.

How can I make a "Search1" and "Search 2" and then "Replace1" and "Replace2"?

I also have difficulties with LONG paths and filenames in the script. Seems like the set-command does not support long filenames e.g. "D:\TESTING 1 and 2\Long filename directory".



Discussion

No Comment Found