Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 6000I wanted it to be this way:
Finance-- 10jan2009 5000
HR-------- 10jan2009 5000
Receiving 10jan2009 6000I 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 %3Where !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!?

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

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 )

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.

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%

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=%%aMy turn to simplify(provided delayed expansion is enabled)
for /l %%a in (1,1,%1) do set pad=!pad!-

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.

![]() |
![]() |
![]() |

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