Solved Wrap Text after 94 Characters

September 3, 2015 at 11:40:29
Specs: Windows 7
Is it possible to create code in a batch file that would open a .txt file, wrap text after 94 characters on each line, and output that information with a new file name?

See More: Wrap Text after 94 Characters

Report •

September 4, 2015 at 02:28:48
✔ Best Answer
Found this after a quick google search:

It uses QBasic to run, but is written in batch. You can change the wrap position in the script.

Don't forget, google is your friend.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

Report •

September 22, 2015 at 11:53:14
Sorry for my delayed response and thank you for your prompt response. My security team is unwilling to install Qbasic on my computer at work. Currently, I have access to Batch files and Active Scripting through a Scheduler application.

Trying to get the .txt file to open, reformat (wrap after 94 characters), save file with new name, and close.

This is the closest I have found so far from

start oldname.txt
@echo off>newname.txt & setlocal enabledelayedexpansion
set x=
for /f "tokens=*" %%a in (%1) do set x=!x!%%a
>>newname.txt echo %x:~0,94%
set x=%x:~95%
if "%x%" neq "" goto :10
:: end batch
taskkill /f /im notepad.exe

This will open the file, modify the contents in the file, save file as newname.txt, and close. Here are the contents that export:

Any thoughts on what to do to wrap the contents in my 1st file and save it with a new name using active scripting or batch files only?

Thanks again for your help.

Report •

September 22, 2015 at 17:56:38
One: does the filename (%1) contain spaces? if so, be sure to enclose the name in quotes, but I will assume you tested with "oldname" and "newname". Just checking.

For debugging, insert this line here:
for /f "tokens=*" %%a in (%1) do set x=!x!%%a
:: --- inserted content
echo content of var x: %x%

If the content is nul/blank then there's a problem. %x% content should be the whole content of "oldname.txt" with no breaks. If oldname.txt is huge, there could also be a problem, but I'm not sure what the "huge-limit" is for a single environment variable in win-7. That could easily be worked around with a moving-window approach.

Report •

Related Solutions

September 24, 2015 at 07:51:33
Thanks Nbrane. This did work for me.

Report •

September 24, 2015 at 08:56:30
Honestly, I'm not sure I'd trust Batch to handle everything correctly, so I'd probably use a VBScript.
Set fso = CreateObject("Scripting.FileSystemObject")
For Each file In WScript.Arguments
  ext = fso.GetExtensionName(file)
  Set inFile = fso.OpenTextFile(file, 1)
  Set outFile = fso.OpenTextFile( _
   Mid(file, 1, Len(file) - Len(ext)) + "wrapped." + ext, 2, True)
  Do Until inFile.AtEndOfStream
    WrapLine inFile.ReadLine, outFile
Next 'file

Sub WrapLine(sLine, oFile)
  oFile.WriteLine Left(sLine, 94)
  remLine = Mid(sLine, 95)
  If Len(remLine) Then WrapLine remLine, oFile
End Sub

How To Ask Questions The Smart Way

Report •

Ask Question