0

Complete novice here so all help gratefully recieved. I am hoping to edit a text file with a batch file.

I have a text file that is all one line. I need the first 240 characters deleted I need the last 82 characters deleted Finally, anything in between I need each 100 characters to be separated with a line break Thanks Mark

3 Answers3

4

try this (pure batch, no VBS):

@ECHO OFF &SETLOCAL
SET "longstring=012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X0123456789012345678901234567890123456789012345678901234567890123456789012345678901"
ECHO %longstring%
ECHO(
REM I need the first 240 characters deleted
SET "longstring=%longstring:~240%"
ECHO %longstring%
ECHO(
REM I need the last 82 characters deleted
SET "longstring=%longstring:~0,-82%"
ECHO %longstring%
ECHO(
REM I need each 100 characters to be separated with a line break
SET LB=^


SET "right=%longstring%"
SET "longstring="
SETLOCAL ENABLEDELAYEDEXPANSION
:loop
SET "left=%right:~0,100%"
SET "right=%right:~101%"
SET "longstring=!longstring!!left!!LB!"
IF DEFINED right GOTO :loop
ECHO(!longstring!

..output is:

012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X0123456789012345678901234567890123456789012345678901234567890123456789012345678901

X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X0123456789012345678901234567890123456789012345678901234567890123456789012345678901

X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X

X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Sure, but what do you think happens when the original single line text file is 8200 bytes long? Nothing good. – dbenham Aug 05 '13 at 22:42
  • @dbenham in my first comment I recommended _sed_, and provided the link. This would be my first choice :) – Endoro Aug 06 '13 at 00:26
3

Your problem is easily solved with nearly any programming language... except Windows batch :(

It is nearly impossible with Windows batch if your file is longer than 8192 bytes because batch variables are limited to 8192 characters. There is a complicated method that uses FC /B to effectively read one byte at a time, expressed as hex. It would be relatively easy to do the counting. But then each hex code would need to be converted back into the character value. You do not want to go through all that pain, except for maybe academic interest.

You could use PowerShell, VBScript, or JScript to easily do what you want.

But if you want to stick with batch, then you will need a non-standard utility to help you.

There are free utilities you could download that would make the solution trivial. For example, sed for Windows could make short work of this problem.

I have written a hybrid JScript/batch utility called REPL.BAT that performs regular expression search and replace on stdin, and writes the result to stdout. The utility is pure script that works on any Windows machine from XP onward - no exe download required. REPL.BAT is available here. Complete documentation is embedded within the script.

Assuming REPL.BAT is either in your current directory, or better yet, somewhere within your PATH, then the following simple batch script should do the trick. The script takes the name of the file to modify as the first and only argument. The file spec can include path information. Be sure to enclose the file in quotes if it contains spaces or other special characters.

@echo off
type %1|repl "^.{240}(.*).{82}$" "$1"|repl ".{100}" "$&\r\n" x >"%~1.mod"
move /y "%~1.mod" %1 >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

I'd strongly recommend switching to PowerShell or at least VBScript if at all possible. It'd be a lot easier to do what you want with those languages.

PowerShell:

$filename = 'C:\path\to\your.txt'
(Get-Content $filename | Out-String) `
    -replace '^[\s\S]{240}', '' `
    -replace '[\s\S]{82}$','' `
    -replace '([\s\S]{100})',"`$1`r`n" `
  | Set-Content $filename

VBScript:

filename = "C:\path\to\your.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set re  = New RegExp
re.Global = True

txt = fso.OpenTextFile(filename).ReadAll

re.Pattern = "^[\s\S]{240}"
txt = re.Replace(txt, "")
re.Pattern = "[\s\S]{82}$"
txt = re.Replace(txt, "")
re.Pattern = "([\s\S]{100})"
txt = re.Replace(txt, "$1" & vbNewLine)

fso.OpenTextFile(filename, 2).Write txt
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks VB script looks like a good solution. However, when I run your code I get a subscript out of range error on the last line. Could it be my input file? – user2653670 Aug 06 '13 at 08:10
  • No. I had first used `WScript.Arguments(0)` for the filename, then changed my mind and used a variable `filename` instead, but forgot to apply this change to the last line. Fixed. – Ansgar Wiechers Aug 06 '13 at 08:28