straytoasters Posted June 9, 2008 Posted June 9, 2008 I can add text to a text file via batch with:echo this is a new line>>filename.txtWhat I want to do is put the new line at the beginning or somewhere in between. The "somewhere in between" is a static blank line so it would appear at the same place in every file I want to change. Any thoughts?Straytoasters
Mijzelf Posted June 10, 2008 Posted June 10, 2008 At the beginning:echo this is a new line>temp.txttype filename.txt>>temp.txtdelete filename.txtrename temp.txt filename.txt
Yzöwl Posted June 10, 2008 Posted June 10, 2008 For 'interpend', although I'd generally suggest SED or G(nu)AWK for the task it can also be done in 'Pure' NT Command Script.The method would greatly depend upon the content of both the 'text file' and 'text string'.If you provide details I may provide a reasonable solution.
Scr1ptW1zard Posted June 11, 2008 Posted June 11, 2008 Based on the information provided, the following may get you started:@echo offset offset=30set filename=test.txtset tempfile=temp.tmpset linecount=0::: Delete files if they existif exist %filename% del %filename%if exist %tempfile% del %tempfile%:: Populate file with dummy data for testingfor /l %%n in (1,1,35) do echo %%n>>%filename%:: Loop through file to find line offsetsetlocal enabledelayedexpansionfor /f %%# in (%filename%) do ( set /a linecount=!linecount!+1 if !linecount! EQU %offset% ( echo %%# Offset reached!>>%tempfile% ) else ( echo %%#>>%tempfile% ))endlocalThe above simply allows you to change a line given the line number specified by the offset variable. A new file is created with the changes made. Additional changes could be made to check for a specific string at the offset line. Also, there is currently no error checking. If a file does not contain a given line offset, what should be done?Hope that helps!
Yzöwl Posted June 11, 2008 Posted June 11, 2008 Although this is slightly off topic, in order for you to understand what problems can arise from a lack of full information for the content here is an example text file:existing.txtI am the first line of this document.I once stated, "I am the second line".I demand to stay put! I don't wish to move!Do you wish to stop & start notepad, just start it, or neither? My email, <something@somedomain.ext>, is not working!NoteLine 5 includes a trailing space after the question mark and line 6 contains a single space, both must be retained in the output file, NewFile.ext as must the blank line currently at line 4.I would be interested to see a reasonably concise Windows NT Command Script using no third party programs which will add the text Text string added! into the file at line 3I have started the script, anyone up for the challenge?AddItIn.cmd@Echo off&Setlocal:: Existing Filename [to add text to]Set "EF=existing.txt":: Line Number [to insert string]Set "LN=3":: Text String [to add]Set "TS=Text string added!":: New Filename [as updated document]Set "NF=NewFile.ext"Type Nul>%NF%
gunsmokingman Posted June 11, 2008 Posted June 11, 2008 (edited) Yzöwl here is my entry and it more of a cheat, sorry but Windows NT Command Script is not my best.Save as a StringManipulation.vbsOption Explicit Const ForReading = 1 Const ForWriting = 2 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Dim C1, TFile1, TFile2, Ts, Txt1, Txt2 TFile1 = "existing.txt" TFile2 = "NewFile.ext"'/-> Check For File Exists If Fso.FileExists(TFile1) Then ProcessFile() Else MsgBox "Can not find " & TFile1, 4128, "Missing File" End If '/-> Process The Text File If Exists Function ProcessFile() Set Ts = Fso.OpenTextFile(TFile1, ForReading) Do Until Ts.AtEndOfStream Txt1 = Ts.ReadLine C1 = C1 + 1 If C1 = 3 Then Txt2 = Txt2 & "Text string added!" & vbCrLf Txt2 = Txt2 & Txt1 & vbCrLf Loop Set Ts = Fso.OpenTextFile(TFile2,ForWriting,True) Ts.WriteLine Txt2 Ts.Close CreateObject("Wscript.Shell").Run ("Notepad.exe " & Chr(34) & TFile2 & Chr(34)),1,True'/-> UnComment If You Want To Delete The File ' Fso.DeleteFile(TFile2),True End Function Edited June 12, 2008 by Yzöwl
Scr1ptW1zard Posted June 13, 2008 Posted June 13, 2008 Ok, after a few days of scratching my head and trying many elaborate combinations, I have come up with the following:@echo offset linecount=0:: Existing Filename [to add text to]Set "EF=existing.txt":: Line Number [to insert string]Set "LN=3":: Text String [to add]Set "TS=Text string added!":: New Filename [as updated document]Set "NF=NewFile.extecho %TS%>insert.txtif exist top.txt del top.txtfor /f "tokens=*" %%l in (%EF%) do ( call :inc linecount call :print %%l )call :dec LNmore +%LN% %EF%>last.txtcopy /b top.txt+insert.txt+last.txt %NF%>nulgoto end:print if %linecount% LSS %LN% echo %*>>top.txt goto :eof:dec set /a %1=%1-1 goto :eof:inc set /a %1=%1+1 goto :eof:endThis is using no external utilities. Works under Windows XP.Thoughts?Tidied version:@Echo off&Setlocal:: Existing Filename [to add text to]Set "EF=existing.txt":: Line Number [to insert string]Set "LN=3":: Text String [to add]Set "TS=Text string added!":: New Filename [as updated document]Set "NF=NewFile.ext::Type Nul>%NF%Set LC=0Echo/%TS%>_#$_2For /f "delims=" %%# In (%EF%) Do (Set/a LC +=1&Call :PR_ %%#)Set/a LN -=1More +%LN% %EF%>_#$_3Copy/b _#$_1+_#$_2+_#$_3 %NF%>Nul&&Del _#$_*Goto :Eof:PR_If %LC% Lss %LN% Echo/%*>>_#$_1
Yzöwl Posted June 13, 2008 Posted June 13, 2008 Superb GSM, I'm happy to leave your vbscript solution here to help show how much quicker it'll work than a batch only solution. Because it edits a stream as SED would but is built-in it deserves its place in this threadI just thought I'd clarify for anyone interested that the expected output should be:@Echo off&Setlocal:: Existing Filename [to add text to]Set "EF=existing.txt":: Line Number [to insert string]Set "LN=3":: Text String [to add]Set "TS=Text string added!":: New Filename [as updated document]Set "NF=NewFile.ext"::Type Nul>%NF%Set/a PL=LN-1 Sed -n 1,%PL%p %EF%>%NF%Echo:%TS%>>%NF%Sed -n %LN%,$p %EF%>>%NF%GAWK@Echo off&Setlocal:: Existing Filename [to add text to]Set "EF=existing.txt":: Line Number [to insert string]Set "LN=3":: Text String [to add]Set "TS=Text string added!":: New Filename [as updated document]Set "NF=NewFile.ext::Type Nul>%NF%Set/a PL=LN-1 Gawk "{print;if(NR==%PL%)print"""%TS%"""}" %EF%>%NF%Congratulations Scr1ptW1zard, we have a working entry according to the original specification.I have nothing but admiration for you for giving this a shot, was intrigued by your approach and love your use of the copy switch! I may be completely wrong but I have no recollection of seeing a script attempt this 'simple' task anywhere on the net and didn't expect anyone to take up the 'pointless' challenge. I'm however a little bit disappointed with one particular aspect; It seems as if it produces the output quicker than mine, the difference isn't much but I'm sure that if I was adding a line to a large file mine would be a lot slower.Now for the kicker, to show how different content can really affect your work, change the insert line number to 13 and change the existing.txt to: Column 1 | Column 2A variable is enclosed in percent signs: %var1%10% of 42 = 5% of 21My email, <something@somedomain.ext>, is not working!Do you wish to stop & start notepad, just start it, or neither? I demand to stay put! I don't wish to move!I stated, "I am in the middle of something".I stated, "I am in the middle of something".I demand to stay put! I don't wish to move!Do you wish to stop & start notepad, just start it, or neither? My email, <something@somedomain.ext>, is not working!10% of 42 = 5% of 21A variable is enclosed in percent signs: %var1% Column 1 | Column 2LinesNotesThis file is mirrored, first and last lines say <TAB>Column 1<TAB>|<TAB>Column 2Lines 7 and 18 contain a single spaceLines 8 and 17 end with a trailing spaceNow start pulling your hair out! BTW, my solution adds just a single line to my 'starter' script, which I split, for beautifying reasons, into threeP.S I'm going to edit/append posts etc. in order not to completely ruin the original intent of the topic and try to keep only the better attempts here!
Scr1ptW1zard Posted June 14, 2008 Posted June 14, 2008 Congratulations Scr1ptW1zard, we have a working entry according to the original specification.I have nothing but admiration for you for giving this a shot, was intrigued by your approach and love your use of the copy switch! I may be completely wrong but I have no recollection of seeing a script attempt this 'simple' task anywhere on the net and didn't expect anyone to take up the 'pointless' challenge. I'm however a little bit disappointed with one particular aspect; It seems as if it produces the output quicker than mine, the difference isn't much but I'm sure that if I was adding a line to a large file mine would be a lot slower.Wow! Thank you for the compliment Yzöwl! This was one of those challenges that I just could not stop working on. So many times I was getting close but missing one special character. I ended up scraping everything and starting from scratch today with a completely different thought process. Methodically stepping through what needed to be done, made me see a straight-forward solution.Thank you very much for your comments. I am glad to contribute to this fine community.<Edit> : Updated solution for changed parametersI think this should handle all the requirements:@echo off:: Existing Filename [to add text to]Set "EF=existing.txt":: Line Number [to insert string]Set "LN=13":: Text String [to add]Set "TS=Text string added!":: New Filename [as updated document]Set "NF=NewFile.ext"if exist %NF% del %NF%call :_dec LNfor /L %%# in (1,1,%LN%) do ( call :_doit %%# )echo %TS%>>%NF%more +%LN% %EF%>>%NF%goto _end:_decset /a %1 -=1goto :eof:_doit findstr /N /R "." %EF%|findstr /B /C:"%1:">nul if errorlevel 1 ( echo.>>%NF% ) else ( for /f "tokens=1,* delims=: " %%a in ('findstr /N /R "." %EF%^|findstr /B /C:"%1:"') do ( if [%%b] EQU [] ( echo. >>%NF% ) else ( echo %%b>>%NF% ) ) ) goto :eof:_endA bit slower as the line number for the insertion is increased. I tested inserting the line at each line of the file as well as lines that do not exist. When entering a line greater than the number of lines in the file, additional lines are added to reach the desired line.</Edit>
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now