Append string to text file at end of line starting with "blah"
-
Tuesday, July 21, 2009 7:17 PMLooking to use a batch file to search a specified txt file for the line starting with "whatever" and append a certain string "appendthis" to the end of the line. How can this be done? Many thanks.
All Replies
-
Tuesday, July 21, 2009 8:29 PMModeratorHi GrandPixel,
When you say "batch file", will a WSH script be OK? (Trying to do this with a cmd script will be pretty painful)
Bill -
Tuesday, July 21, 2009 8:49 PMI suppose. I just had to look up what WSH is. I am new to scripting (apparently) and have only done batch scripting. Is Windows PowerShell different from WSH? I would like to learn PowerShell as I understand it to be the latest greatest scripting platform.
Anyway, not sure what it takes to implement WSH but I'm sure that would be fine. I need to learn something more flexible than batch scripting anyway. Seems like that's now the old way of doing things. -
Wednesday, July 22, 2009 7:34 AM
@echo off
Set src_file=c:\src.txt
Set dst_file=c:\dst.txt
Set tmp_file=c:\tmp.txt
set s_find=blah
set s_append="appendthis
findstr %s_find% > %tmp_file%
for /F %i in (%tmp_file%) do echo %i %s_append% > %dst_file%- Marked As Answer by IamMredMicrosoft Employee, Owner Monday, January 11, 2010 6:44 AM
-
Wednesday, July 22, 2009 8:21 PMModerator
With PowerShell, you can do something like this:
$original = "whatever.txt" $tempfile = [IO.Path]::GetTempFileName() get-content $original | foreach-object { if ($_ -match "^whatever") { $_ + "appendthis" >> $tempfile } else { $_ >> $tempfile } } copy-item $tempfile $original remove-item $tempfile
Bill- Marked As Answer by IamMredMicrosoft Employee, Owner Monday, January 11, 2010 6:44 AM

