JSI Tip 8151. How can I remove the last N characters of the last line of a file?

In tip 8150, I scripted StripString.bat to remove the last N characters of a string. In tip 4574, I scripted Tail.bat to count the number of lines in a file.

Using those techniques, I have scripted StripFile.bat to remove the last N characters of the last line of a file.

The syntax for using StripFile.bat is:

StripFile FileName \[NumChar\]

Where FileName is the file you wish to shorten, and NumChar is an optional parameter that contains the number of characters (N) to remove. The default is 1.

StripFile.bat contains:

@echo off
if \{%1\}

\{\} @echo Syntax: StripFile FileName \[NumChar\]&goto :EOF if not exist %1 @echo StripFile File %1 not found.&goto :EOF setlocal ENABLEDELAYEDEXPANSION set wrk="%TEMP%\StripFile.tmp" set fle=%1 set /a strip=1 if not \{%2\}

\{\} set /a strip=%2 if exist %wrk% del /q %wrk% for /f %%n in ('find /V /C "" ^>%wrk% ) else ( set rec=%%L call set out=!rec:~0,^-%strip%! echo/!out!>>%wrk% ) ) call :cpy >nul 2>&1 del /q %wrk% endlocal goto :EOF :cpy copy /Y %wrk% %fle%

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.