• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

batch file - target file has 3 records and destination file has 4 records

If I execute below batch file then CAAGIndividualsFile.TXT file has 3 records and
temp_CAAGIndividualsFile.TXT file has 4 records, Basically if there is space in record it goes to another line. I want to loop for [CR][LF] not for space. Please find attached file as a input file

@echo off
setlocal enabledelayedexpansion

call :StripLastCRLF CAAGIndividualsFile.TXT

goto :eof
:StripLastCRLF
for %%a in ("%~1") do set OutF=%%~dpa\temp_(%%~na).txt
for /f %%a in ('type "%~1" ^| find /v /c ""') do (set TotalLines=%%a)
if exist "%OutF%" del "%OutF%"
set /a LineCount=0
for /f "tokens=1* delims=[]" %%a in ('type "%~1" ^| find /v /n ""') do (
	set /a LineCount += 1
	echo !TotalLines!
	if !LineCount!==!TotalLines! (
		>>"%OutF%"<NUL (set /p Dummy=%%b)
	) else (
		>>"%OutF%" echo.%%b
	)
)
goto :eof
                                            

Open in new window

CAAGIndividualsFile.TXT
0
sonawanekiran
Asked:
sonawanekiran
2 Solutions
 
oBdACommented:
The issue with that file is that there's a NUL (ASCII 0) in the first line of that file (check it in a hex editor or in something like Notepad++ (http://notepad-plus-plus.org/) with "Show all characters" enabled).
This character should not appear in a text file at all, so you need to check your database how it slipped into a text field in the first place and then made its way into an export.
You can expect all kinds of strange behavior when trying to modify a "text" file that contains non-printable characters other than TAB/CR/LF. In other words: your "text" file is actually a binary file.
The first line looks like this; the [NUL] in the second field is creating the issues:
kiran[tab][NUL][tab][tab][tab]d[tab]3[tab]3[tab][tab]s[tab]s[tab]s[tab]2[tab]d[tab]d[tab]d[tab]d[tab]d

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
The first few bytes of your text file include a null character, and it is the "find" command which is choking on this and sending a new line.  Any reason this has to be in done in batch?

107      k
105      i
114      r
97      a
110      n
9      "      "
0      
9      "      "
9      "      "
9      "      "
100      d
0
 
Bill PrewCommented:
Just as an alternative since this question has had a lot of activity here lately, here's a simple VBS approach to getting rid of any trailing <CR><LF> pairs.  In addition I added a replace of any NUL characters (0x00) with a space character.

You can run it as either syntax below, which of course can be called from a BAT file:

cscript cscript EE28012352.vbs in.txt out.txt

or

cscript EE28012352.vbs in.txt


In the first case it will read in.txt, make the changes and save them to out.txt, great for testing or if you want to preserve the input file.  In the second case it will read from in.txt, make the changes, and overwrite in.txt with the updated file.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Replace NUL with SPACE
sData = Replace(sData, Chr(0), " ")

' Remove any trailing <CR><LF>
Do While Right(sData, 2) = vbCrLf
  sData = Left(sData, Len(sData)-2)
Loop

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
Wscript.Quit

Open in new window

~bp
0
 
Steve KnightIT ConsultancyCommented:
Cross posted there clearly!

If you do have to process this data for some reason then you will have to read the file as a binary / random file which you could do using VBA, VBScript etc.

You will notice just doing

more filename.txt
find /v "" filename.txt
etc. all show the extra line, though
type filename.txt doesn't show it, it does then pass it onto the find command still.

[Edit .... glad I didn't write out a VBScript, Bill got in first anyway]

Steve
0
 
sonawanekiranAuthor Commented:
Thanks everyone...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now