Insert string at the beginning of everyline of a text document? - string

I am trying to insert a certain string with spaces "sBody = " before each line in a text document. I found this answer in other forums, but it does not work? A few questions I have about this script are, should "myfile" be the full filepath? Also, this script was originally made to insert a string at the end of the file, what do I need to take out to make it insert just one string at the beginning?
Thank you for all of your answers!
#echo off > newfile & setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
echo STRING %%a STRING >> newfile
)

The script is creating a new file named 'newfile' reading in the original file 'myfile'. Once it's done you can delete 'myfile' and rename 'newfile' to 'myfile' if you like. This example replaces each <line> in the file with STRING <line> STRING.
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
echo STRING %%a STRING >> newfile
)

sed 's/.*/pattern &/' yourfile.txt -i
works for me, in a single command line

Related

Manipulation on delayed expansion string variable is failing

I am trying to find a particular line from an XML file and extract the value using string manipulation operations.
Below is the code I am trying.
#echo off
setlocal enabledelayedexpansion
::Expected line is "<filename>c:\temp\file1</filename>"
for /f "tokens=*" %%i in ('findstr /i "filename" file1.props') do (
SET LINE=%%i
)
echo !LINE!
SET FILENAME=!LINE:<filename>=!
SET FILENAME=%FILENAME:</filename>=%
ECHO !FILENAME!
And the output is:
<filename>c:\temp\file1</filename>
The system cannot find the file specified.
ECHO is off.
I actually want this value c:\temp\file1
Someone please help me correct the code or please suggest any other simpler way.
The problem is the execution of set. The parser interprets the > and < as redirection, so it will fail with a syntax error. Use quotes to process it as intended (`set "var=value"):
#echo off
setlocal enabledelayedexpansion
REM echo ^<filename^>c:\temp\file1^</filename^>>file1.props
::Expected line is "<filename>c:\temp\file1</filename>"
for /f "tokens=*" %%i in ('findstr /i "filename" file1.props') do (
SET "LINE=%%i"
)
echo !LINE!
SET "FILENAME=!LINE:<filename>=!"
SET "FILENAME=%FILENAME:</filename>=%"
ECHO !FILENAME!
Output is:
<filename>c:\temp\file1</filename>
c:\temp\file1

I need to insert a text above the line containing a particular string "[TOP]" in batch programming

E.g. the file abc.txt contains below text in it and i have to add text "infra" in it above [TOP]:
;plcd
;abcd
valueof=a,b,c
[TOP]
and the output should be like this:
;plcd
;abcd
valueof=a,b,c
infra
[TOP]
How to do this in batch programming.
#echo off
setlocal
set "file=abc.txt"
set "start="
for /f usebackq^ delims^=^ eol^= %%a in ("%file%") do (
if not defined start set "start=1" & break > "%file%"
if "%%~a"=="[TOP]" (>>"%file%" echo(infra)
>>"%file%" echo(%%a
)
For each line with data in the file, if it contains [TOP], echo infra. After it, echo the readed line.

Find and replace a string into a path in batch

I am trying a batch file which replaces a changing text named X by a static text 'English' into the path
Y:\SUT\ISTQB\Board\X\Dashboard\Open
About the position of the X text, it is always surrounded with 'Board' and 'Dashboard'.
In the path below, the current below text for the given path is 'X=Language' and my goal is finally to get 'X=English' each time of course!
Y:\SUT\ISTQB\Board\Language\Dashboard\Open
The string of replacement is read from a file. This file only contains 'English'
Is that possible to find a solution through a pipe with the 'find' command?
Thank you in advance
#ECHO OFF
SETLOCAL
SET mypath=Y:\SUT\ISTQB\Board\X\Dashboard\Open
FOR /f %%i IN (languagefile.txt) DO SET language=%%i
SET mypath=%mypath:\Dashboard\=*%
SET mypath=%mypath:\Board\=*%
FOR /f "tokens=1,3delims=*" %%i IN ("%mypath%") DO SET mypath=%%i\Board\%language%\Dashboard\%%j
ECHO %mypath%
"*" chosen because it cannot occur in a pathname.
Try this:
#echo off &setlocal enabledelayedexpansion
set "string=Y:\SUT\ISTQB\Board\Language\Dashboard\Open"
set "search=%string:*\Board\=%"
for /f "delims=\" %%i in ("%search%") do set "search=%%i"
set "string=!string:%search%=English!"
echo %string%

How to efficiently remove the first few chars of a row in text file?

Example of 2 rows in text file (abc.txt):
PMIP_TSD_2012120323.csv:03/12/2012,22:51:53,CAU TACS,TS,PPT4I_TS22, AJAY,595959,P,Legal Exit,(6 0) AJAY,G1234567M,SERVICES P L,8401352W,
PMIP_TSD_2012111300.csv:12/11/2012,23:20:13,CAU TACS,TS,PPT4O_TS32,ARUMUGAM,620466,P,Legal Exit,(5 0) ARUMUGAM,G686W,SUPERSONIC SERVICES P L,1982W,
I would like every row to start with the date instead of the csv file name. Meaning removing the PMIP......csv: for every row. How can i do this using batch file? I have hundreds of rows, i do not wish to do it manually. My file name is abc.txt and it is located in D:\Int\ KGX
This Batch file do that:
#echo off
cd /D "D:\Int\KGX"
(for /F "tokens=1* delims=:" %%a in (abc.txt) do (
echo %%b
)) > abc-NEW.txt
perl -npi -e 's/[^:]+://' /tmp/abc.txt yields:
03/12/2012,22:51:53,CAU TACS,TS,PPT4I_TS22, AJAY,595959,P,Legal Exit,(6 0) AJAY,G1234567M,SERVICES P L,8401352W,
12/11/2012,23:20:13,CAU TACS,TS,PPT4O_TS32,ARUMUGAM,620466,P,Legal Exit,(5 0) ARUMUGAM,G686W,SUPERSONIC SERVICES P L,1982W,
from your data. Hope that helps...
I can give you my thoughts about the problem
We can use C++ or Java to solve this problem
Open the file
In a loop we get the row in a String variable
use SubString (or some kind of similar functions or utilities) to specify the first index number (in our case 23)
Store the sub string in a new file
finish the loop
The new file is what you want, and hope that helps
This should do it
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=:" %%a in (abc.txt) do (
echo !date!:%%b >>new.txt
)
del abc.txt /f /q
ren new.txt abc.txt

Write batch variable into specific line in a text file

I have a batch file where I need to write a variable into a specific line of a text file and override what is all ready in that line. I have the code to read specific lines from the file maybe I could switch it around to also write?
Reading lines code:
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (variables.txt) do (
set /a N+=1
set v!N!=%%a
)
set variable1=!v1!
set variable2=!v2!
set variable3=!v3!
set variable4=!v4!
I've tried to add echo %variable1% > !v4! something like that but it doesn't work.
I figured it out!! Here is the code for anyone else who might ever need it.
#echo off
setlocal enableextensions enabledelayedexpansion
set inputfile=variables.txt
set tempfile=%random%-%random%.tmp
copy /y nul %tempfile%
set line=0
for /f "delims=" %%l in (%inputfile%) do (
set /a line+=1
if !line!==4 (
echo WORDS YOU REPLACE IT WITH>>%tempfile%
) else (
echo %%l>>%tempfile%
)
)
del %inputfile%
ren %tempfile% %inputfile%
endlocal
Another option might be to overwrite the file entirely. Here's the part to do that:
:saveVars
(
ECHO %v1%
ECHO %v2%
ECHO %v3%
ECHO %v4%
ECHO %v5%
) >variables.txt
GOTO :EOF
That is, if the number of lines is fixed and known beforehand. If not, you might want to store the last value of the increment in your example code and, when saving the variables, use it like this:
:saveVars
SETLOCAL EnableDelayedExpansion
(
FOR /L %%i IN (1,1,%N%) DO (ECHO !v%%i!)
) >variables.txt
ENDLOCAL
GOTO :EOF
I'm assuming here that the v1/v2 etc. variables would be used only for synchronising with the file: when it is read, the lines are stored in those variables, and when any of them (variables) gets changed, you just call the saveVars subroutine immediately. Here's an example how you would use it:
…
SET v2=something
CALL :saveVars
…
SET v4=%somevar%
CALL :saveVars
…
If the file is small, the rewriting should be fast enough.
Not absolutely sure I've understood everything correctly, but if you want to substitute something for an existing part of a text file with a batch script, you'll need to write everything (including the changed part) to a new file first, then delete the original and rename the new file to the original name.
I can't really see a point of reading everything into variables, unless I'm missing something. You could simply iterate over the lines writing them one by one into the new file and replacing the specific line's contents with the substitute text along the way:
setLocal EnableDelayedExpansion
>newFile.txt (
for /f "tokens=* delims= " %%a in (variables.txt) do (
set /a N+=1
if !N! == 4 (ECHO substitute text) ELSE ECHO %%a
)
)
del variables.txt
rename newFile.txt variables.txt
If the substitute text must, in turn, be derived from one of the lines, you could do something like this:
setLocal EnableDelayedExpansion
>newFile.txt (
for /f "tokens=* delims= " %%a in (variables.txt) do (
set /a N+=1
if !N! == 1 SET subst_text=%%a
if !N! == 4 (ECHO !subst_text!) ELSE ECHO %%a
)
)
del variables.txt
rename newFile.txt variables.txt

Resources