| 1. |
Solve : MS DOS Batch - String Manipulation? |
|
Answer» Does anyone here have experience with modifying text files via batch? I would like to create a .BAT that can be executed via Shell Wait in MS Access 2003. The text file needs to be modified/overwritten to remove lines of text that presently represent headers/footers causing problems with MS Access import specifications. The file originates as a UNIX print spooler file that I have retrieved via ftp and saved to the Windows box as an ASCII .txt file. SUPER - I just tried it out and it worked like a CHARM! Do you know how I can use the same to incorporate special characters such as CR, LF, etc also can I include wildcards? I am not sure what you mean by this; if you filtered out lines with a CR or an LF, you'd filter out everything. If you want to replace one character (or a set of characters) with another within word or line of text that is possible e.g. set text=Mary had a little lamb; its fleece was white as snow. set text=%text:Mary=Joe% result: Joe had a little lamb; its fleece was white as snow. set text=%text:lamb=cat% result: Joe had a little cat; its fleece was white as snow. set text=%text:white=black% set text=%text:snow=coal% result: Joe had a little cat; its fleece was black as coal. as for "wildcards", that makes me think you would do well to study "regular expressions", which you can use with find's grown up brother, findstr. For heavy duty string manipulation, Visual Basic Script is probably easier to use and more efficient. you can use batch to do string manipulation but its capabilities in TERMS of string/text manipulation is primitive and full of inefficiency. for better text manipulation capabilities, use tools, such as Perl (or Python) or gawk (see my sig). you can also make use GNU tools like head /tail to remove headers/footers , Code: [Select]C:\test>more file header text1 text2 footer C:\test>tail -n +2 file | head -n -1 text1 text2 show an example of your text file, and describe the output you want.[duplicate] Quote from: Salmon Trout on February 23, 2010, 03:48:45 PM I am not sure what you mean by this; if you filtered out lines with a CR or an LF, you'd filter out everything. Good observation, I wasn't thinking CLEARLY when I mentioned the CR/LF as it relates to this FUNCTION. My thoughts are that if the I know that line of text on which the real data begins, I could somehow remove or replace the first n lines each time which would me the first n CR/LF lines would be removed and then the script would be more universally suited to apply to a variety of text files where each header/footer may contain different text. As for a Visual Basic approach I would be interested in your thoughts. I am ultimately doing this to accomodate an fix width delmitation into MS Access tables, that said I couldn't find an equivalent to InputString function VB function in VBA. Quote from: danyelle on February 24, 2010, 07:37:28 AM As for a Visual Basic approach I would be interested in your thoughts. I am ultimately doing this to accomodate an fix width delmitation into MS Access tables, that said I couldn't find an equivalent to InputString function VB function in VBA.Use the InputBox function in VBA, for example Code: [Select]DocumentName = InputBox("Document Name ?")um.. VB.NET has an InputString Function: Code: [Select]InputString(_ ByVal FileNumber As Integer, _ ByVal CharCount As Integer _ ) As String looks like it reads data from a file- InputBox does not. VBA is nearly identical to VB6 in what features it supports; so: Paste In a new module: Code: [Select]Public Function TrimFile(ByVal sFileName As String, ByVal sOutputFile As String, Optional ByVal SkipHeaderLines As Integer = 0, Optional ByVal SkipFooterLines As Integer = 0) Dim fnum As Integer Dim ftemp As Integer, temppath As String Dim textin As String Dim FoundHeadercrlf As Long, FoundFooterCrlf As Long fnum = FreeFile Open sFileName For Input As fnum ftemp = FreeFile Open sOutputFile For Output As ftemp textin = Input$(LOF(fnum), fnum) Close fnum Dim currcount As Long If SkipHeaderLines Then FoundHeadercrlf = 1 Do Until currcount = SkipHeaderLines currcount = currcount + 1 FoundHeadercrlf = InStr(FoundHeadercrlf + 1, textin, vbCrLf) Loop End If If SkipFooterLines Then FoundFooterCrlf = Len(textin) currcount = 0 Do Until currcount = SkipFooterLines currcount = currcount + 1 FoundFooterCrlf = InStrRev(textin, vbCrLf, FoundFooterCrlf - 1) Loop End If textin = Mid$(textin, FoundHeadercrlf, FoundFooterCrlf - FoundHeadercrlf) 'remove leading and trailing crlfs... Do Until Left$(textin, 2) <> vbCrLf textin = Mid$(textin, 3) Loop Do Until Right$(textin, 2) <> vbCrLf textin = Mid$(textin, 1, Len(textin) - 2) Loop Print #ftemp, textin Close #ftemp End Function In my tests this seems to do what you require, if I understand what you require, of course. basically, just call it like this: TrimFile(ByVal sFileName As String, ByVal sOutputFile As String, Optional ByVal SkipHeaderLines As Integer = 0, Optional ByVal SkipFooterLines As Integer = 0) Code: [Select]TrimFile("D:\Filename.txt","D:\output.txt", <number of header lines to skip>, <number of footer lines to skip>) remember to replace each parameter with the filenames and the number of header and footer lines you REALLY want to skip. This code should work in Access 97 and higher, so you should be fine. |
|