We help IT Professionals succeed at work.

DOS - Trim Text File

andyb7901
andyb7901 asked
on
3,718 Views
Last Modified: 2012-08-13
Is there anyway to trim a text file using dos? I am using Blat to send a result of a defrag to my email address. This runs weekly. However, the file is pretty big and contains rubbish. All i would really need is the last ten or so lines? Is there anyway to trim a text file using dos or smoething?
Comment
Watch Question

Top Expert 2007

Commented:
Try this:

@echo off

setlocal enabledelayedexpansion

set linesNeeded=10

set fileName=%~1

if "%~1"=="" set /p fileName=Enter input file name:
if "%fileName%"=="" goto :EOF
if not exist "%fileName%" echo %fileName% does not exist&goto :EOF

set outFile=%~2

if "%~2"=="" set outFile=output.txt

set /a lineCnt=0

for /f %%a in ('type "%fileName%"') do set /a lineCnt+=1

set /a skipCnt=%lineCnt% - %linesNeeded%

if /i %skipCnt% LSS 1 copy "%fileName%" "%outFile%"&goto EXIT

del "%outFile%" 2>NUL

set /a lineCnt=0

for /f "tokens=*" %%a in ('type "%fileName%"') do (
  set /a lineCnt+=1
 
  if /i !lineCnt! GTR %skipCnt% (echo %%a)>>"%outFile%"
)

:EXIT

echo Output in %outFile%

Author

Commented:
Where would this place the output file? And where would I name it?

Author

Commented:
I am running it with my input file to = C:\Hello.txt and when I run the Bat ti stops on the dos screen and needs me to press enter. It simply has in the Dos Screen; C:\Hello.txt
Top Expert 2007

Commented:
The processing has the following usage:

batchfilename [input file] [output file]

If you do not enter the input file name the program will prompt you. If not output file name is given the default of output.txt is used.

In your case you might say:

batchfilename c:\hello.txt c:\hello.out

If you were to utilize this in a batch file you'd say:

call batchfilename c:\hello.txt c:\hello.out

Author

Commented:
So what would be the correct syntax if I had a file called Hello in C:\ and an ouput called Goodbye.txt in C:\.
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.