1.

Solve : Insert text to a new last line?

Answer»

Hi,

I am using the following code to insert the filename to the last line.
FOR %G in (*.txt) do echo.>>file.txt %~NG>>%G

However sometime it gets appended to the EXISTING last line.
I want just the filename to be in the last line. How do we insert a CRLF in DOS and add a text?

Thankyou
Atturhari

Quote from: atturhari on July 17, 2014, 06:17:05 AM

Hi,

I am using the following code to insert the filename to the last line.
FOR %G in (*.txt) do echo.>>file.txt %~nG>>%G

However sometime it gets appended to the existing last line.
I want just the filename to be in the last line. How do we insert a CRLF in DOS and add a text?

Thankyou
Atturhari

I have almost no idea what you mean, but I think you want the text ">>file.txt %~nG" At the end of the file %G.  In that case, you need to escape the first two '>'.  To do this simply prefix them with a carrot '^>'.Your problem seems to be that the last line doesn't always have a trailing CF/LF

This is a solution to that - test it on some sample .txt files:
remove - This is the last line as it's just to help see where it echos

Code: [Select]echo off
for /f "delims=" %%a in ('dir *.txt /a:-d /b') do (
   findstr /v ".*$" "%%a" >nul && (>>"%%a" echo()
   >>"%%a" echo(%%a - This is the last line
)As pointed out, the problem is the last line doesn't always have a trailing CF/LF causing the text (or filename) to be appended to the same line.

I tried foxidrive's code, unfortunately it does not work.
Also my files are on a network, so want to know if it is POSSIBLE to tweak the below line of code to add a CF/LF

FOR %G in (*.fex) do echo FILENAME:%~nG>>%G

Thankyou Quote from: atturhari on July 17, 2014, 10:47:08 PM
I tried foxidrive's code, unfortunately it does not work.

It works fine here.  How does it fail for you? Quote from: foxidrive on July 18, 2014, 01:27:21 AM
It works fine here.  How does it fail for you?

Was executing your program in CMD and just realized that i should use a single % instead of %%.

Your program does the job perfectly. Thank you very very much  Quote from: atturhari on July 18, 2014, 04:06:50 AM
Was executing your program in CMD and just realized that i should use a single % instead of %%.

You have to be kidding me... 

Who taught you to paste batch files into a cmd prompt? Quote from: foxidrive on July 17, 2014, 08:19:47 AM
Your problem seems to be that the last line doesn't always have a trailing CF/LF

This is a solution to that - test it on some sample .txt files:
remove - This is the last line as it's just to help see where it echos

Code: [Select]echo off
for /f "delims=" %%a in ('dir *.txt /a:-d /b') do (
   findstr /v ".*$" "%%a" >nul && (>>"%%a" echo()
   >>"%%a" echo(%%a - This is the last line
)
Quote from: foxidrive on July 18, 2014, 04:22:58 AM
You have to be kidding me... 

Who taught you to paste batch files into a cmd prompt?

Hmm.. my honest answer,
In cmd prompt, the code can be edited quickly and best for trail and error.
Running as .bat file is TIME consuming involving multiple steps  Quote from: atturhari on July 22, 2014, 05:50:33 AM
Hmm.. my honest answer,
In cmd prompt, the code can be edited quickly and best for trail and error.
Running as .bat file is time consuming involving multiple steps 
You should TRY using Notepad++.  You can edit and run your batch files within Notepad++. Quote from: atturhari on July 22, 2014, 05:50:33 AM
Hmm.. my honest answer,
In cmd prompt, the code can be edited quickly and best for trail and error.
Running as .bat file is time consuming involving multiple steps 
Or you could always just not maximize everything.


Discussion

No Comment Found