Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2013-01-29
5
Medium Priority
?
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 400 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++ (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
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      "      "
0      
9      "      "
9      "      "
9      "      "
100      d
0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 1600 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

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
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]

Steve
0
 
LVL 17

Author Closing Comment

by:sonawanekiran
ID: 38831186
Thanks everyone...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

647 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