Computing.Net > Forums > Programming > formatting a text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

formatting a text file

Reply to Message Icon

Name: CKotlowski
Date: January 15, 2009 at 18:01:49 Pacific
OS: Windows Vista
CPU/Ram: dual core
Product: Toshiba / Pa3489u
Subcategory: General
Comment:

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

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

:loop
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!?



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: January 16, 2009 at 01:25:00 Pacific
Reply:

[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.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set max=0
:loop
>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
:len
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
)


0

Response Number 2
Name: reno
Date: January 16, 2009 at 02:29:53 Pacific
Reply:

woow, jugado, you are fast, here is alternative coding:

::TextFormat.bat
@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
)


0

Response Number 3
Name: Judago
Date: January 16, 2009 at 03:23:01 Pacific
Reply:

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.


0

Response Number 4
Name: reno
Date: January 16, 2009 at 03:55:38 Pacific
Reply:

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%


0

Response Number 5
Name: Judago
Date: January 16, 2009 at 04:07:39 Pacific
Reply:

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!-


0

Related Posts

See More



Response Number 6
Name: reno
Date: January 16, 2009 at 04:25:46 Pacific
Reply:

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.


0

Response Number 7
Name: CKotlowski
Date: January 17, 2009 at 19:51:42 Pacific
Reply:

Thank-you very much... it works very well!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: formatting a text file

Help browsing a text file www.computing.net/answers/programming/help-browsing-a-text-file/9929.html

splitting a text file www.computing.net/answers/programming/splitting-a-text-file/14804.html

Opening a text file in a textbox www.computing.net/answers/programming/opening-a-text-file-in-a-textbox/285.html