formatting a text file

Toshiba / Pa3489u
January 15, 2009 at 18:01:49
Specs: Windows Vista, dual core
Hello... I am trying to find a way to format a text file so that its' data is in columns. For example, given this data:

Finance 10jan2009 5000
HR 10jan2009 5000
Receiving 10jan2009 6000

I wanted it to be this way:

Finance-- 10jan2009 5000
HR-------- 10jan2009 5000
Receiving 10jan2009 6000

I wrote this batch, but it still won't format it this way.

@echo off
setlocal Enabledelayedexpansion

(@For /F "tokens=*" %%a in (myfile.txt) Do call :Length %%a %%b %%c)
goto :eof

echo %1>> "%temp%\st.txt"
for %%a in (%temp%\st.txt) do set /a len=%%~za & set /a len -=3 & del "%temp%\st.txt"

If !len! leq 9 set /a len=10
If !len! geq !max! echo %1 %2 %3>>newfile.txt
If !len! lss !max! set var=%1- & call :length %var% %2 %3

Where !max! comes from another batch, and is the number of characters of the longest string in the first column.

It almost works, but mishandles the first line and deletes the last (I believe)... any help will be greatly appreciated. Thanks.

P.S.... is there an easy way to get !Max!?

See More: formatting a text file

Report •

January 16, 2009 at 01:25:00
[edit: Small cleanup - removed unnessacery recursion]

I just knocked up something that seemed to work well for me.

A few notes:
1. !Max! doesn't need to be passed to it, it will only ignore the value.
2. Only the first column is padded all other columns are ignored and output as is.
3. No consideration has be take for special characters; !,<,>,|,& and ^ will all probably cause problems.
4. A space is an assumed delimiter, if it is infact a tab you will need to change ">>newtextfile.txt echo !col1! %%b" so a tab is placed between !col1! and %%b instead of a space.

set max=0
>newtextfile.txt type nul
for /f "usebackq tokens=1,* delims=	 " %%a in ("textfile.txt") do (
set col1=%%a
set col=
call :len in te rn
if "%~1%~2%~3"=="intern" (
if defined col1 (
set /a col+=1
set col1=!col1:~1!
goto len
) else (
goto :eof
if !col! gtr !max! (
set max=!col!
goto :loop
) else (
set /a pad=max-col
set col1=%%a
for /l %%d in (1,1,!pad!) do set col1=!col1!-
>>newtextfile.txt echo !col1! %%b

Report •

January 16, 2009 at 02:29:53
woow, jugado, you are fast, here is alternative coding:

@echo off

for /f "tokens=1-3" %%a in (sample.txt) do (
	echo.>"--------- %%b %%c"
	ren "--------- %%b %%c" "%%a*"
	for %%d in (dir/b del) do %%d "%%a*%%b %%c">>newfile.txt

Report •

January 16, 2009 at 03:23:01
Good trick with ren and the wildcard, didn't know that one. I like it when someone brings something so simple and effective, especially when my solution is overly complicated in comparison. It's the reason that everyone around here keeps getting better. The only issue with your script is that you need to know what the maximum length is to begin with.

You could have also used 1,* for tokens, that way %%a will be token 1 and %%b will be the rest of the line.

Report •

Related Solutions

January 16, 2009 at 03:55:38
here is the code to get the padding.

::Max.bat (len as %1)
@echo off
echo.	|more /T%1>tmp
set/p pad=<tmp
set pad=%pad: =-%
echo %pad%

you are right with the tokens 1*, i didnt think of that. nice one.

anyone know how to set screen output to variable without using tmp file??
i try this and not working.

dir /b test.txt|set/p var=
echo %var%

Report •

January 16, 2009 at 04:07:39
Output of the command can be set to a variable by for /f
for /f "delims=" %%a in ('dir /b test.txt') do set var=%%a

My turn to simplify(provided delayed expansion is enabled)

for /l %%a in (1,1,%1) do set pad=!pad!-

Report •

January 16, 2009 at 04:25:46
that's great. simple and to the point. and its only one line. =)

duh, what am i thinking, using "more" make the code complicated. hahhaha.

Report •

January 17, 2009 at 19:51:42
Thank-you very much... it works very well!

Report •

Ask Question