Unix Tail command in DOS Batch

I was struggling the last days for converting my applications from UNIX servers to Windows servers. The main difficulty I faced was the conversion of the shell scripts to DOS Batch scripts. Since I was not that expert in UNIX Shell commands, it was a challenge for me. I will keep on posting the different conversions I did. This is the first one.

The tail command in UNIX will take the last specified number of lines from the input given. The below line of command will take last 20 lines from del_temp.log and put into value.log.

tail -20 del_temp.log >> value.log

This single line tail command can be converted to its equivalent DOS Batch by the below block of code.

SETlocal EnableExtensions
SET tailCount=20
SET fileName=del_temp.log
SET output=value.log
FOR /F “usebackq tokens=3,3 delims= ” %%l IN (`find /c /v “” %fileName%`) DO (call SET find_lc=%%l)
SET /a linenumber = %find_lc% – %tailCount%
IF %linenumber% LSS 1 (
more +0 %fileName% >> %output%
) ELSE (
more +%linenumber% %fileName% >> %output%

Enjoy and Happy Coding Guys šŸ™‚


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: