Solved

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

Posted on 2013-01-29
5
518 Views
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
: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
Comment
Question by:sonawanekiran
5 Comments
 
LVL 82

Assisted Solution

by:oBdA
oBdA earned 100 total points
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 51

Accepted Solution

by:
Bill Prew earned 400 total points
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 17

Author Closing Comment

by:sonawanekiran
Comment Utility
Thanks everyone...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now