<http://www.netikka.net/tsneti/info/tscmd044.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Sun 5-Mar-2017 10:04:53

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



44} How do I get the first 68 columns from a text file?

Actually, needed this one for my own tasks.
  @echo off & setlocal enableextensions disabledelayedexpansion
  set filename_=myfile.txt
  if not exist "%filename_%" (
    echo File "%filename_%" not found
    goto :EOF)
  if exist "outfile.txt" del "outfile.txt"
  for /f "delims=" %%r in ('type "%filename_%"') do call :WriteLine68 %%r
  dir "outfile.txt"
  endlocal & goto :EOF

  :: ============================
  :WriteLine68
  setlocal enableextensions
  set line_=%*
  echo %line_:~0,68%>>"outfile.txt"
  endlocal & goto :EOF

Be aware of the limitations inherent to CMD.EXE. The empty lines will be deleted, special symbols such as > and single %:s dropped, %% reduced to %, and so on. Thus, the usefulness depends on the circumstances of the application. Consequently, a Visual Basic Script (VBScript) aided command line script solution can be written which does not have these limitations.

  @echo off & setlocal enableextensions disabledelayedexpansion
  ::
  :: Where from and where to

  set infile_=C:\_M\myfile.txt
  set outfile_=C:\_M\outfile.txt
  if not exist "%infile_%" (
    echo File "%infile_%" not found
    goto :EOF)
  if exist "%outfile_%" del "%outfile_%"
  ::
  :: Build a Visual Basic Script

  set temp_=%temp%
  if defined mytemp if exist "%mytemp%\" set temp_=%mytemp%
  set skip=
  findstr "'%skip%VBS" "%~f0" > "%temp_%\tmp$$$.vbs"
  ::
  :: Run the VBS script with Microsoft Windows Script Host Version 5.6

  cscript //nologo "%temp_%\tmp$$$.vbs" "%infile_%" "%outfile_%"
  ::
  :: Clean up

  for %%f in ("%temp_%\tmp$$$.vbs") do if exist "%%~f" del "%%~f"
  ::
  :: Display

  type "%outfile_%"
  endlocal & goto :EOF
  '
  '................................................................
  'The Visual Basic Script
  '

  Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
  Dim fso, arg, f1, f2, line 'VBS
  Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
  Set arg = WScript.Arguments 'VBS
  Set f1 = fso.OpenTextFile(arg(0), ForReading, True) 'VBS
  Set f2 = fso.OpenTextFile(arg(1), ForWriting, True) 'VBS
  Do While Not f1.AtEndOfStream 'VBS
    line = f1.ReadLine 'VBS
    f2.WriteLine Mid(line, 1, 68) 'VBS
  Loop 'VBS
  f2.Close 'VBS

However, by far the easiest solution to write is
  @echo off & setlocal enableextensions
  set filename_=C:\_M\myfile.txt
  if not exist "%filename_%" (
    echo File "%filename_%" not found
    goto :EOF)
  rem Usage: substr(STRING, START, LENGTH)
  gawk '{printf"%%s\n",strftime(substr($0,1,68))}' "%filename_%"
  endlocal & goto :EOF
In the "vernacular" of the GnuWin32 gawk port (let's call it unxgawk) one has to adjust the syntax to
  @echo off & setlocal enableextensions
  set filename_=C:\_M\myfile.txt
  if not exist "%filename_%" (
    echo File "%filename_%" not found
    goto :EOF)
  unxgawk "{printf\"%%s\n\",strftime(substr($0,1,68))}" "%filename_%"
  endlocal & goto :EOF

References/Comments:
  VBScript Microsoft
  GNU awk String-Manipulation Functions
  String functions in gawk info at feebsd.org