Computer Hope

Microsoft => Microsoft DOS => Topic started by: braglin on June 12, 2012, 04:57:47 AM

Title: How to delete the last line of a txt file
Post by: braglin on June 12, 2012, 04:57:47 AM
Hi,

When I using
Code: [Select]
echo blablabla>test.txt
the output file (test.txt) has 2 lines


I need to delete the empty line!
Or find another way to create this file!

The first line is random, so I think I can't use the findstr command, I don't have sure!


Could anyone help me?


Tks!
Title: Re: How to delete the last line of a txt file
Post by: Sidewinder on June 12, 2012, 05:57:55 AM
How are you checking the line count? I suspect there is only one line in the file. You can use this little snippet to verify that. When prompted enter the name of the file (full path if necessary)

Code: [Select]
@echo off
setlocal
::
:: Count lines in a file (blanks included)
::
set /p filename=Enter File Name:

for /f "tokens=1* delims=:" %%i in ('findstr /n /r ".*" "%filename%"') do (
  set tLines=%%i > nul
)

echo %tLines% line(s) in file: %filename%

Happy counting  8)

Title: Re: How to delete the last line of a txt file
Post by: Salmon Trout on June 12, 2012, 12:13:00 PM
Something not right here.

echo blablabla>test.txt creates a file with exactly one (1) line.

Proof: use findstr with the /N switch (show line count) and the /R (regular expression) option (regex is "." which means "any string" that is, show all lines.)

Code: [Select]
C:\>echo blablabla>test.txt

C:\>findstr /N /R "." test.txt
1:blablabla

Editor set to show line endings

(http://i124.photobucket.com/albums/p29/badoit/blabla1.jpg)

hex mode... see? one line followed a cr/lf pair...

(http://i124.photobucket.com/albums/p29/badoit/blabla3.jpg)

On the other hand if we deliberately create a second blank line...

Code: [Select]
C:\>echo blablabla>test.txt

C:\>echo. >> test.txt

C:\>findstr /N /R "." test.txt
1:blablabla
2:

(http://i124.photobucket.com/albums/p29/badoit/blabla2.jpg)

so,

Quote from: Sidewinder
How are you checking the line count? I suspect there is only one line in the file.

Title: Re: How to delete the last line of a txt file
Post by: braglin on June 12, 2012, 12:48:30 PM
ok!

User will input a model name in to a variable, so the echo will create a file with this model name, so, another software (in C++) will check in DB and will return with the work instruction for this model.

 I'm using Notepad++ for check it, and showed as your UltraEdit, but you can see the line number 2? It's is empty, but my C++ program didn't work with it, I made some test removing this line by manually, and works!

see my codes:

Code: [Select]
@echo off
cls

:: Network Configuration ::
IPCONFIG /RENEW
NET USE \\105.103.74.60 /user:fabio.b 1qazxdr5
XCOPY \\105.103.74.60\HDDSL\*.* C:\SOPLoader\SOP\ /S /E /Y
MOVE C:\SOPLoader\SOP\profiles.txt C:\SOPLoader

:: GUI CONFIGURATION ::
:START
set wabmp=ld.y
set waico=SL.ico
set wasig=SEDA C  -  Fabio.b
set wizapp=start /w WizApp.exe
set i0=1
set i1=1
set watitle=HDD SOP Loader
set model=

:: Model Scan ::
:PAGE1
set page=:page1
set wabat=%TEMP%\wabat.bat
set watext=~~~~~~~Código do modelo:
set waoutput=%model%
start /w wizapp noback EB
if errorlevel 2 goto :cancel
if errorlevel 1 goto :PAGE2
call %wabat%
set model=%waoutput%

:: DB access ::
:PAGE2
echo model>mdchk.txt
call MSL.exe
goto START

:cancel


C++ Code MSL
Code: [Select]
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream.h>

void print_profile( const char * code, const char * model, const char * sop )
{
    char command[50];
cout << "\n\nPROFILE:"
    << "\n\t\t Code: " << code
    << "\n\t\t Model: " << model
    << "\n\t\t SOP: " << sop << "\n \n";
sprintf(command,"%s",sop);
system(command);
exit (1);
}

void find_record( const char * mdchk, const char * database_filename )
{
    FILE * file;
    if( !(file = fopen(database_filename, "r")) )
{
cout << "\nDatabase filename: " << database_filename << " not found!";
return;
    }
    char buffer[ 1024 ] = { '\0' };
    while( fgets(buffer, 1023, file) )
{
if( strstr(buffer, mdchk) != 0 )
{
char code[ 16 ] = { '\0' };
char model[ 6 ] = { '\0' };
char sop[ 6 ] = { '\0' };
char * pCh = strtok(buffer, ",");
strcpy( code, pCh );
pCh = strtok( 0, "," );
strcpy( model, pCh);
pCh = strtok( 0, "," );
strcpy( sop, pCh );
print_profile( code, model, sop );
fclose( file );
return;
}
    }
    cout << "\n\n\nMODEL NOT FOUND => Database:" << database_filename;
    fclose( file );
}

int main()
{
    FILE * file;
if ( !(file = fopen("MDCHK.txt", "r")) )
{
cout << "\n\n\nMDCHK.txt not found!";
return(0);
}
char mdchk[16];
while( fgets(mdchk,15, file) )
{
mdchk[16]='\0';
puts (mdchk);
find_record( mdchk, "profiles.txt" );
return 0;
}
return (0);
}


and profiles.txt:

Code: [Select]
test,asdf,notepad test.txt,
test2,afds,notepad test2.txt,


Thank you!
Title: Re: How to delete the last line of a txt file
Post by: Salmon Trout on June 12, 2012, 01:26:04 PM
Quote
:PAGE2
echo model>mdchk.txt

Shouldn't that be

echo %model%>mdchk.txt

....?

Does wizapp append an extra CR/LF?

Title: Re: How to delete the last line of a txt file
Post by: braglin on June 12, 2012, 01:38:06 PM
oh sorry... my mistake, the real code is %model%! thks


wizapp will create a GUI for this batch script!
Title: Re: How to delete the last line of a txt file
Post by: braglin on June 13, 2012, 07:20:11 AM
someone?
Title: Re: How to delete the last line of a txt file
Post by: Squashman on June 13, 2012, 08:03:36 AM
You are going to have to figure out what Wizapp is doing to that variable.  I don't plan on installing it on my machine.
It looks like it creates a batch file to set the OUTPUT of the environmental variables.  I would look to see what is going on inside that batch file because there as Salmon Trout has demonstrated the basic batch code you are using cannot possible ECHO two lines of input to your output file.
Title: Re: How to delete the last line of a txt file
Post by: braglin on June 13, 2012, 08:19:34 AM
Hi Squashman!

You don't need to install the wizapp, don't worry!

If you open the cmd and type:
Code: [Select]
set model=blablabla
echo %model%>mdchk.txt

You can see the output as my batch script will create!

I checked in Notepad++ and has a "Enter" at the end of line! When I removed it my C++ program runs normally! This is the point! The "ENTER"!

But I don't know how to remove this!

See picture:

(http://img37.imageshack.us/img37/1417/blablablaec.jpg) (http://imageshack.us/photo/my-images/37/blablablaec.jpg/)

Uploaded with ImageShack.us (http://imageshack.us)
Title: Re: How to delete the last line of a txt file
Post by: Salmon Trout on June 13, 2012, 11:20:46 AM
Only makes 1 line for me:

WriteVar.bat

Code: [Select]
@echo off
set wabat=%TEMP%\wabat.bat
set watext=Some text
start /w wizapp noback EB
call %wabat%
set model=%waoutput%
echo %model%>test.txt
findstr /N /R "." test.txt

Output:

One (1) line counted.

Code: [Select]
C:>WriteVar.bat
1:hello world

Editor:

One (1) line shown in normal mode, one (1) line ending visible.

(http://i124.photobucket.com/albums/p29/badoit/wizapp1.jpg)

Hex edit mode:

(http://i124.photobucket.com/albums/p29/badoit/wizapp2.jpg)

One (1) 0D 0A cr/lf pair (only).

In hex mode, we see that the line text is followed by a hex 0D character (a carriage return or CR) and a hex 0A character (a line feed or LF). Together these are called a CR/LF pair. This is the standard MS-DOS/Windows text file line ending. There is only one line ending in the file, and the 0A (LF) character is the last byte in the file, which has a size of 13 bytes: 11 text characters: "hello world" plus the CR and the LF. There is one line in this file.

So what is going on here?


Title: Re: How to delete the last line of a txt file
Post by: gpl on June 13, 2012, 11:29:28 AM
(previous posted while I was typing)

I think I see what you are getting at

the point is, the cr/lf marks the END of the line

I suppose you just want to know how to output text without this cr/lf pair ?

try this
Code: [Select]
set model=blablabla
echo.|set/p x=%model%>mdchk.txt
it does put in a trailing space, hope that doesnt spoil it for you
Title: Re: How to delete the last line of a txt file
Post by: braglin on June 13, 2012, 11:41:30 AM
Tks gpl!

It's worked!


Thank you all for your help!
Title: Re: How to delete the last line of a txt file
Post by: Salmon Trout on June 13, 2012, 12:36:25 PM
avoids trailing space

Code: [Select]
set model=blablabla
0>nul set /p=%model%>mdchk.txt

Title: Re: How to delete the last line of a txt file
Post by: Squashman on June 13, 2012, 03:43:56 PM
So your C++ program thinks there is two lines if there is a CR/LF at the end of line 1? That makes no sense to me at all. I do data processing for a living and work with large text files everyday and I have never run into that occurence.

Why don't you just change your C++ program to accept command line input and pass that variable to your program as a command line argument instead of having it read the variable from a text file.
Title: Re: How to delete the last line of a txt file
Post by: braglin on June 13, 2012, 04:32:20 PM
Hi Squashman!

I tried to read the system variable, yesterday after create this post, I changed my C++ code, but I'm not a programmer, I'm an test engineer and I don't have knowledge to do a graphical interface in C++, and I'm using Turbo C++ 3.0 as compiler too!  :P

When wizapp set the system variable, I don't know why, but I can't read these variable in C++! Maybe a wizapp script limitation! I don't have sure!




Thank you for your help!
Title: Re: How to delete the last line of a txt file
Post by: Squashman on June 13, 2012, 04:49:15 PM
I am not saying you should read directly from the environmental variable in your program.  I am saying pass it as a command line argument. This has nothing to do with a graphical interface.  If your program was setup correctly to read a variable from input you could call your program from your batch file like so.

MSL.exe %model%

I haven't programmed in C++ since I took my class in 1996 so I really can't recall how to do it but just about every Compiled and Interpreted programming Language can accept command line input in some fasion.
Title: Re: How to delete the last line of a txt file
Post by: gpl on June 14, 2012, 01:47:15 AM
avoids trailing space

Code: [Select]
set model=blablabla
0>nul set /p=%model%>mdchk.txt
cool, thanks
Title: Re: How to delete the last line of a txt file
Post by: braglin on June 14, 2012, 04:16:23 AM
I am not saying you should read directly from the environmental variable in your program.  I am saying pass it as a command line argument. This has nothing to do with a graphical interface.  If your program was setup correctly to read a variable from input you could call your program from your batch file like so.

MSL.exe %model%

I haven't programmed in C++ since I took my class in 1996 so I really can't recall how to do it but just about every Compiled and Interpreted programming Language can accept command line input in some fasion.

hum... I see, next time i will try it!!!

Thank you for your help!
Title: Re: How to delete the last line of a txt file
Post by: Salmon Trout on June 14, 2012, 04:39:29 AM
look for argv, argc, etc