1.

Solve : batch script to align the columns in a text file.?

Answer»

Hello, Below is my problem:

The data in my file is like this:

      98869        0000390.00 QRP
      178411        0000015.00 QRP


I need it like the below:

        96782        0000015.00 QRP
      100266        0000015.00 QRP

Below is the code currently I am using:

echo off
if exist c:\Newfile.dat del c:\Newfile.dat
for /F "skip=1 delims=" %%a in (C:\test\Bonus_File_Export.txt) do call :Sub %%a
goto :eof

:Sub
echo        %1        %2 %3>>c:\test\Newfile.dat

how to enhance this code to make SURE that, when there is a 5 digit value shows up, it will be like the one I need.

Do let me know if any information is needed from my side. Any help is highly appreciated. Thank you.
We have a FORMAT function over on DosTips.com.
Let me know if you don't understand how to use it.
http://www.dostips.com/DtCodeCmdLib.php#Function.FormatYes Sir, I've gone through this link a while ago. A tried a BIT but my code failed with syntax errors as I was not using them properly. Could you please guide me with this? Quote from: ssis_sql_dev on JANUARY 07, 2015, 03:26:36 PM

Yes Sir, I've gone through this link a while ago. A tried a bit but my code failed with syntax errors as I was not using them properly. Could you please guide me with this?
So you tried using this function already?  Why didn't you ask for help on the Dostips.com forum if you couldn't get it too work? Code: [Select]echo off &setlocal enabledelayedexpansion
if exist Newfile.txt del Newfile.txt
for /F "tokens=1-3" %%G in (file.txt) do (
call :Format "[-6][-18][-4]" %%G %%H %%I
>>Newfile.txt echo !line!
)
goto :eof

:Format fmt str1 str2 ... -- outputs columns of strings right or left aligned
::                        -- fmt [in] - format string specifying column width and alignment, i.e. "[-10][10][10]"
:$created 20060101 :$changed 20091130 :$categories Echo
:$source http://www.dostips.com
SETLOCAL
set "fmt=%~1"
set "line="
set "spac=                                                     "
set "i=1"
for /f "tokens=1,2 delims=[" %%a in ('"echo..%fmt:]=&echo..%"') do (
    set /a i+=1
    call call set "subst=%%%%~%%i%%%spac%%%%%~%%i%%"
    if %%b0 GEQ 0 (call set "subst=%%subst:~0,%%b%%"
    ) ELSE        (call set "subst=%%subst:~%%b%%")
    call set "const=%%a"
    call set "line=%%line%%%%const:~1%%%%subst%%"
)
endlocal&set "line=%line%"

GOTO :EOF
output
Code: [Select]C:\BatchFiles\Format>type file.txt
96782        0000015.00 QRP
100266        0000015.00 QRP

C:\BatchFiles\Format>format_SSIS.bat

C:\BatchFiles\Format>type newfile.txt
 96782        0000015.00 QRP
100266        0000015.00 QRP

C:\BatchFiles\Format> Quote from: ssis_sql_dev on January 07, 2015, 02:15:14 PM
Hello, Below is my problem:

The data in my file is like this:

      98869        0000390.00 QRP
      178411        0000015.00 QRP


I need it like the below:

        96782        0000015.00 QRP
      100266        0000015.00 QRP


The numbers are different - is that part of the task?

Quote from: ssis_sql_dev on January 07, 2015, 02:15:14 PM
how to enhance this code to make sure that, when there is a 5 digit value shows up, it will be like the one I need.

What do you mean by this?  Will there be 4 digit numbers for example? 
The task is not clear from what you have written.


Discussion

No Comment Found