Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum record length in a text file using Windows command line

Tags:

windows

cmd

How to find the maximum record length in a text file using Windows command. i.e, it should output the maximum length of the line found in that file.

like image 865
shruthi Avatar asked Sep 07 '25 20:09

shruthi


2 Answers

Using native Powershell utility only:

Get-Content C:\textfile.txt | Measure-Object -Property length -Maximum

To get the actual content of the longest line:

Get-Content C:\textfile.txt | Sort-Object -Property length | Select-Object -last 1

like image 179
meatspace Avatar answered Sep 10 '25 07:09

meatspace


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a maxlength=0
FOR /f "delims=" %%a IN (q27898945.txt) DO (
 SET "line=%%a"
 CALL :calc
)

ECHO maxlength IN file is %maxlength%

GOTO :EOF

:calc
SET "line2=!line:~%maxlength%!"
IF DEFINED line2 set/a maxlength+=1&GOTO calc
GOTO :eof

I used a file named q27898945.txt containing some random text for testing.

Will show some sensitivity to content - lines containing %text% for instance will be miscalculated.

like image 27
Magoo Avatar answered Sep 10 '25 06:09

Magoo