Link to home
Start Free TrialLog in
Avatar of Kiran Sonawane
Kiran SonawaneFlag for India

asked on

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
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Kiran Sonawane

ASKER

Thanks everyone...