

InterviewSolution
Saved Bookmarks
1. |
Solve : MS DOS Batch - String Manipulation? |
Answer» <html><body><p>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.<br/><br/>Please respond with "dummy" instructions. My DOS is very old rusty knowledge and will require some hand holding to implement.To remove lines with specific text you could try using the TYPE command piped to the FIND command and redirect the output to a new file. The /v switch for FIND means "filter lines that do not contain the text in quotes".<br/><br/>examples<br/><br/> Code: <a>[Select]</a>type "before.txt" | find /v "This text is only found in a HEADER" > "after.txt"<br/>type "before.txt" | find /v "This text is only found in a FOOTER" > "after.txt"<br/>type "before.txt" | find /v "This text is only found in a HEADER" | find /v "This text is only found in a FOOTER"> "after.txt"<br/><br/>Having done the processing you could then <a href="https://interviewquestions.tuteehub.com/tag/delete-karana-436574" style="font-weight:bold;" target="_blank" title="Click to know more about DELETE">DELETE</a> before.txt and rename after.txt to before.txt.<br/>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? Quote from: danyelle on February 23, 2010, 03:08:23 PM</p><blockquote>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?<br/></blockquote> <br/>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<br/><br/>e.g.<br/><br/>set text=Mary had a little lamb; its fleece was white as snow.<br/><br/>set text=%text:Mary=Joe%<br/>result: Joe had a little lamb; its fleece was white as snow.<br/><br/>set text=%text:lamb=cat%<br/>result: Joe had a little cat; its fleece was white as snow.<br/><br/>set text=%text:white=black%<br/>set text=%text:snow=coal%<br/>result: Joe had a little cat; its fleece was black as coal.<br/><br/>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.<br/><br/>For heavy duty string manipulation, Visual Basic Script is probably easier to use and more efficient.<br/><br/><br/>you can use batch to do string manipulation but its capabilities in <a href="https://interviewquestions.tuteehub.com/tag/terms-239101" style="font-weight:bold;" target="_blank" title="Click to know more about TERMS">TERMS</a> 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). <br/>you can also make use GNU tools like head /tail to remove headers/footers ,<br/> Code: <a>[Select]</a>C:\test>more file<br/>header<br/>text1<br/>text2<br/>footer<br/><br/>C:\test>tail -n +2 file | head -n -1<br/>text1<br/>text2<br/><br/><br/>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<blockquote>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. <br/><br/>For heavy duty string manipulation, Visual Basic Script is probably easier to use and more efficient.<br/></blockquote> <br/>Good observation, I wasn't thinking <a href="https://interviewquestions.tuteehub.com/tag/clearly-420736" style="font-weight:bold;" target="_blank" title="Click to know more about CLEARLY">CLEARLY</a> when I mentioned the CR/LF as it relates to this <a href="https://interviewquestions.tuteehub.com/tag/function-11303" style="font-weight:bold;" target="_blank" title="Click to know more about FUNCTION">FUNCTION</a>. 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 <em>n</em> lines each time which would me the first <em>n</em> 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.<br/><br/>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<blockquote>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.<br/></blockquote> Use the InputBox function in VBA, for example<br/> Code: <a>[Select]</a>DocumentName = InputBox("Document Name ?")um..<br/><br/>VB.NET has an InputString Function:<br/><br/> Code: <a>[Select]</a>InputString(_<br/> ByVal FileNumber As Integer, _<br/> ByVal CharCount As Integer _<br/>) As String<br/><br/>looks like it reads data from a file- InputBox does not.<br/><br/>VBA is nearly identical to VB6 in what features it supports; so:<br/><br/>Paste In a new module:<br/><br/> Code: <a>[Select]</a>Public Function TrimFile(ByVal sFileName As String, ByVal sOutputFile As String, Optional ByVal SkipHeaderLines As Integer = <a href="https://interviewquestions.tuteehub.com/tag/0-242464" style="font-weight:bold;" target="_blank" title="Click to know more about 0">0</a>, Optional ByVal SkipFooterLines As Integer = 0)<br/> Dim fnum As Integer<br/> Dim ftemp As Integer, temppath As String<br/> Dim textin As String<br/> Dim FoundHeadercrlf As Long, FoundFooterCrlf As Long<br/> fnum = FreeFile<br/> Open sFileName For Input As fnum<br/> <br/> ftemp = FreeFile<br/><br/> <br/> Open sOutputFile For Output As ftemp<br/><br/> textin = Input$(LOF(fnum), fnum)<br/> Close fnum<br/> <br/> <br/> <br/> Dim currcount As Long<br/> If SkipHeaderLines Then<br/> FoundHeadercrlf = 1<br/> Do Until currcount = SkipHeaderLines<br/> currcount = currcount + 1<br/> FoundHeadercrlf = InStr(FoundHeadercrlf + 1, textin, vbCrLf)<br/> Loop<br/> <br/> End If<br/> <br/> If SkipFooterLines Then<br/> FoundFooterCrlf = Len(textin)<br/> currcount = 0<br/> Do Until currcount = SkipFooterLines<br/> currcount = currcount + 1<br/> FoundFooterCrlf = InStrRev(textin, vbCrLf, FoundFooterCrlf - 1)<br/> Loop<br/> <br/> End If<br/><br/> textin = Mid$(textin, FoundHeadercrlf, FoundFooterCrlf - FoundHeadercrlf)<br/> <br/> 'remove leading and trailing crlfs...<br/> Do Until Left$(textin, 2) <> vbCrLf<br/> textin = Mid$(textin, 3)<br/> Loop<br/> <br/> Do Until Right$(textin, 2) <> vbCrLf<br/> textin = Mid$(textin, 1, Len(textin) - 2)<br/> Loop<br/> <br/> Print #ftemp, textin<br/> <br/> Close #ftemp<br/> <br/>End Function<br/><br/><br/><br/>In my tests this seems to do what you require, if I understand what you require, of course.<br/><br/>basically, just call it like this:<br/>TrimFile(ByVal sFileName As String, ByVal sOutputFile As String, Optional ByVal SkipHeaderLines As Integer = 0, Optional ByVal SkipFooterLines As Integer = 0)<br/> Code: <a>[Select]</a>TrimFile("D:\Filename.txt","D:\output.txt", <number of header lines to skip>, <number of footer lines to skip>)<br/>remember to replace each parameter with the filenames and the number of header and footer lines you REALLY want to skip.<br/><br/>This code should work in Access 97 and higher, so you should be fine.</body></html> | |