Ascii2 Posted November 17, 2009 Posted November 17, 2009 How can text be inserted or removed from a text file by using a batch file?Should it make a difference if a text file being edited is Unicode or ANSI?Examples of some applications for this are:Adding a semicolon to a line containing a string of text in TXTSETUP.SIF and DOSNET.INF.Removing a line containing a string of text from TXTSETUP.SIF and DOSNET.INF.
jaclaz Posted November 17, 2009 Posted November 17, 2009 As long as we are talking of adding/removing "entire" lines or "prepending/postpending" some characters to "entire lines" it is easily doable in 2K and later batches.This kind of approach is exemplified here:http://www.msfn.org/board/do-we-get-our-ha...ini-t66101.html Of course processing in batch a largish file like txtsetup.sif may take a bit of time, expecialy if you need to parse the entries.Since a .SIF or .INF is almost identical to a .INI file, ths may be a good base to play with:http://www.robvanderwoude.com/batexamples_r.phpBut using an external app like gsar or fedit or the like would speed up times considerably.Batch does not "understand" correctly UNICODE, AFAIK, but what are the UNICODE files? jaclaz
Ascii2 Posted November 17, 2009 Author Posted November 17, 2009 (edited) As long as we are talking of adding/removing "entire" lines or "prepending/postpending" some characters to "entire lines" it is easily doable in 2K and later batches.This kind of approach is exemplified here:http://www.msfn.org/board/do-we-get-our-ha...ini-t66101.html Of course processing in batch a largish file like txtsetup.sif may take a bit of time, expecialy if you need to parse the entries.Since a .SIF or .INF is almost identical to a .INI file, ths may be a good base to play with:http://www.robvanderwoude.com/batexamples_r.phpBut using an external app like gsar or fedit or the like would speed up times considerably.Batch does not "understand" correctly UNICODE, AFAIK, but what are the UNICODE files? I shall examine the page you linked to.Some files, like regedit scripts (ending .reg), use Unicode encodings.I noticed that some commands fail or yield undesirable output when working with some encodings or multiple encodings. An example of such a command is the COPY command (tested on Windows 2000 with Service Pack 4). COPY does not seem to correctly handle Unicode. When concatenating only Unicode files, COPY misrepresents a character (often invisible); when concatenating files with ANSI and Unicode encodings, the result is a file with at least one section random-like characters. Edited November 17, 2009 by Ascii2
Ascii2 Posted November 23, 2009 Author Posted November 23, 2009 (edited) I am now trying to get my batch file code to remove lines of text from TXTSETUP.SIF and DOSNET.INF that contain a specific text string. Original DOSNET.INF and TXTSETUP.SIF filenames (including case) from the installation source are to be retained.The code does not seem to work. I do not seem to be getting the syntax correct.The following is an example of the code that is not working:FINDSTR /V /I "string" TXTSETUP.SIF > TXTSETUP.TMPCOPY /V /Y /B TXTSETUP.TMP (DIR /B TXTSETUP.SIF)DEL TXTSETUP.TMPFINDSTR /V /I "string" DOSNET.INF > DOSNET.TMPCOPY /V /Y /B DOSNET.TMP (DIR /B DOSNET.INF)DEL DOSNET.TMPThe DIR commands in parenthesis are used in an attempt to acquire a filename in correct case.How can the code be fixed? Edited November 25, 2009 by Ascii2
Yzöwl Posted November 25, 2009 Posted November 25, 2009 Example.cmd@find /i /v "string"<txtsetup.sif>txtsetup.tmp && (( copy txtsetup.tmp txtsetup.sif>nul) && (del txtsetup.tmp))
Ascii2 Posted November 26, 2009 Author Posted November 26, 2009 I am now trying to get my batch file code to remove lines of text from TXTSETUP.SIF and DOSNET.INF that contain a specific text string. Original DOSNET.INF and TXTSETUP.SIF filenames (including case) from the installation source are to be retained.The code does not seem to work. I do not seem to be getting the syntax correct.The following is an example of the code that is not working:FINDSTR /V /I "string" TXTSETUP.SIF > TXTSETUP.TMPCOPY /V /Y /B TXTSETUP.TMP (DIR /B TXTSETUP.SIF)DEL TXTSETUP.TMPFINDSTR /V /I "string" DOSNET.INF > DOSNET.TMPCOPY /V /Y /B DOSNET.TMP (DIR /B DOSNET.INF)DEL DOSNET.TMPThe DIR commands in parenthesis are used in an attempt to acquire a filename in correct case.How can the code be fixed?I was not sure how to get the case of the original filenames of TXTSETUP.SIF and DOSNET.INF to be retained after the copy command. I had thought that the copy command would simply replace an existing file with the copied file (which should not preserve the text case of the original filenames). This does not seem to be what happens. It seems that in Windows 2000 family operating system the COPY command seems to preserve the original filenames of the existing files, and not that of the destination file parameter of the COPY command. This also seems to mean that if ever a filename case IS wanted to be applied, the copied files would have to be renamed. It would have been good if the COPY command had a way of selecting filename case preservation behavior (like an argument).To fix my script, I could replace the parentheses and contents between the parentheses with the destination filename; the case of the original filenames from existing files should retain.
Ascii2 Posted November 26, 2009 Author Posted November 26, 2009 Example.cmd@find /i /v "string"<txtsetup.sif>txtsetup.tmp && (( copy txtsetup.tmp txtsetup.sif>nul) && (del txtsetup.tmp))This may be more efficient.I shall test it.
Ascii2 Posted November 27, 2009 Author Posted November 27, 2009 Example.cmd@find /i /v "string"<txtsetup.sif>txtsetup.tmp && (( copy txtsetup.tmp txtsetup.sif>nul) && (del txtsetup.tmp))This may be more efficient.I shall test it.The example almost works as expected.On Windows 2000 family operating systems (maybe others), the FIND command with the /V argument has a defect such that it fails to correctly handle the end of a file. If the last line of a text file input to the FIND command does not end as a new line (such as with TXTSETUP.SIF), an extra end of line is appended to the output. The FINDSTR command does not appear to have the defect (but is unavailable in Windows 98) and yields the correct output.So FINDSTR should be used instead of FIND to find and remove text.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now