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

Posted on 2013-01-29
Last Modified: 2013-01-29
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
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

Question by:sonawanekiran
LVL 83

Assisted Solution

oBdA earned 100 total points
ID: 38830669
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++ ( 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:

Open in new window

LVL 43

Expert Comment

by:Steve Knight
ID: 38830704
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      "      "
9      "      "
9      "      "
9      "      "
100      d
LVL 53

Accepted Solution

Bill Prew earned 400 total points
ID: 38830705
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


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)
  WScript.Echo "No filename specified."
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
  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
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)

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

' Cleanup and end
Set oFSO = Nothing

Open in new window

LVL 43

Expert Comment

by:Steve Knight
ID: 38830724
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]

LVL 17

Author Closing Comment

ID: 38831186
Thanks everyone...

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sequence of icacls to permit specific user but deny all users access to a file 8 36
Install both Office 2010 and 2013 4 38
facebook change 4 39
Reinstall Windows 10 3 21
Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question