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.
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
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With