locked
Batch script to sort the records and align the column in a text file RRS feed

  • Question

  • Hello, 

    I've a text file which has records that needs to be sorted and aligned. Below is my requirement:

    I used the below code to get to this point:

    @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

    After processing the source text file with the above code, my text file records looks like the ones in the "Existing file" section of the screenshot. 

    There are 6 spaces before the first column, 8 after first and 1 after second column.

    Please help me with this. Any help is highly appreciated.

    Thank you.

    Tuesday, January 6, 2015 9:09 PM

Answers

  • Sir,

    Thanks for the update. Actually, this batch scripting is not my skill set but I have to do it just to format this file (one time task).

    I am not sure how to write code to extract the last 7 characters. Can you please help me in this regard. Also, is it possible to sort the records as seen in the screenshot.


    A few weeks ago I wrote the first part of the script for you. It seems that you are now asking for yet another bit which amounts to you asking respondents in this forum to write the whole script for you without any effort from your side. Please make at least a token effort to write part of the script, e.g. by following the suggestion I made in my first response in this thread. If you feel that this is too difficult then you need to engage a consultant to do the job for you, preferably using Powershell.
    • Marked as answer by Bill_Stewart Sunday, January 25, 2015 5:11 PM
    Tuesday, January 6, 2015 11:03 PM

All replies

  • You need to do this to pad your numbers:

    1. Add a few spaces to the left of %1:
      set Number=    %1
    2. Extract the last 7 characters from %Number%.

    Type set /? | more at the Command Prompt to find out how to obtain a substring from an existing string. And by the way - why not familiarise yourself with snippingtool.exe? I would remove the need for respondents to use a magnifying glass when trying to read your tiny screen shots.

    Tuesday, January 6, 2015 9:42 PM
  • Sir,

    Thanks for the update. Actually, this batch scripting is not my skill set but I have to do it just to format this file (one time task).

    I am not sure how to write code to extract the last 7 characters. Can you please help me in this regard. Also, is it possible to sort the records as seen in the screenshot.

    Will try the snippingtool tool next time. For now, you can right click the image and select "Open image in new tab". It will show in the actual size.

    Thank you.

    Tuesday, January 6, 2015 10:16 PM
  • 1. It is better not to post screen shots but rather please copy and paste the sample data you want.

    2. Where is this data coming from? Wouldn't it be easier to output this data in the correct format in the first place?

    3. Shell script (batch file) is a poor choice for data manipulation. PowerShell is far superior in every conceivable way.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 6, 2015 10:43 PM
  • Sir,

    Thanks for the update. Actually, this batch scripting is not my skill set but I have to do it just to format this file (one time task).

    I am not sure how to write code to extract the last 7 characters. Can you please help me in this regard. Also, is it possible to sort the records as seen in the screenshot.


    A few weeks ago I wrote the first part of the script for you. It seems that you are now asking for yet another bit which amounts to you asking respondents in this forum to write the whole script for you without any effort from your side. Please make at least a token effort to write part of the script, e.g. by following the suggestion I made in my first response in this thread. If you feel that this is too difficult then you need to engage a consultant to do the job for you, preferably using Powershell.
    • Marked as answer by Bill_Stewart Sunday, January 25, 2015 5:11 PM
    Tuesday, January 6, 2015 11:03 PM
  • Below are my answers.

    1) When I copy/pasted for the first time, it was irregular due to some reason. So, tried the screesnhot.

    2) This text file is exported by the microstrategy tool. After the formatting is done, we have to feed this to peoplesoft application.

    3) Will try searching code in powershell.


    Tuesday, January 6, 2015 11:13 PM
  • Yes, you are right. This script is yours. In earlier post, I have shared my code initially. As I found your code more effective, I replaced my code with yours. Thank you.
    Tuesday, January 6, 2015 11:16 PM
  • As Frederik Long pointed out, this is not a custom script design and consulting forum. This forum is really designed for answering scripting questions. If your organization needs custom programming and design, you may need to hire a consultant if the custom programming job is beyond your area of expertise.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 6, 2015 11:18 PM