locked
how to format the spaces in a text file using bat file scripting RRS feed

  • Question

  • Currently the records in my text file are like this:

    100239 0000015.00  QRP

    I want them to be like this:

      100266        0000015.00 QRP
    

    There should be 6 spaces before the first column and 8 spaces after that. There should be 1 space between the second and third columns.

    Generally this file contains approx 1000 records.

    Any help is highly appreciated.

    Thank you.



    • Edited by SQL_SSIS_Dev Wednesday, November 5, 2014 7:31 PM
    • Moved by Bill_Stewart Friday, December 5, 2014 3:41 PM This is not "scripts on demand"
    Wednesday, November 5, 2014 7:11 PM

Answers

  • I tried this and it didn't work.


    I fully tested the code and it works perfectly on my machine, using the input file I posted. Your feedback ("It did not work") tells me exactly nothing. You need to try a fair bit harder in trying to find out what is going on. Remember - this forum does not deliver scripts on demand. You will have to put a certain amount of your own effort into this project.
    • Marked as answer by Bill_Stewart Friday, December 5, 2014 3:41 PM
    Wednesday, November 5, 2014 11:00 PM

All replies

  • What does your script look like so far?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, November 5, 2014 7:20 PM
  • You came to the right place.  Here are instructions on how to write a script: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    You will not ;likely be able to do it with a batch file but it may be possible.  We will not do it for you.


    ¯\_(ツ)_/¯

    Wednesday, November 5, 2014 7:21 PM
  • So far it's like this:

    for /f "skip=2 delims=*" %%a in (C:\Bonus_File_Export.txt) do (
    echo %%a >>C:\newfile.txt    
    )
    xcopy C:\newfile.txt C:\Bonus_File_Export.txt /y
    del C:\newfile.txt /f /q

    I've to format a text file generated by the tool Microstrategy and it feed it to another system in a specific format. Ihave removed the headers with the above script but was unable to remove the spaces between those columns.

    Please help.

    Wednesday, November 5, 2014 7:27 PM
  • What did the original file look like before you broke it?


    ¯\_(ツ)_/¯

    Wednesday, November 5, 2014 7:34 PM
  • So far it's like this:

    for /f "skip=2 delims=*" %%a in (C:\Bonus_File_Export.txt) do (
    echo %%a >>C:\newfile.txt    
    )
    xcopy C:\newfile.txt C:\Bonus_File_Export.txt /y
    del C:\newfile.txt /f /q

    I've to format a text file generated by the tool Microstrategy and it feed it to another system in a specific format. Ihave removed the headers with the above script but was unable to remove the spaces between those columns.

    You can use this code as a basis for your script:

    @echo off
    set Line=
    if exist d:\temp\newfile.txt del d:\temp\newfile.txt
    for /F "skip=2 tokens=1-2" %%a in (d:\temp\test.txt) do call :Sub %%a %%b
    goto :eof

    :Sub
    if not "%2"=="" (
      set Line=      %1        %2
    ) else (
      echo %Line% %1>>d:\temp\newfile.txt
    )

    It works provided that your input file uses this format:

    Header line
    Header line
    100239 0000015.00  

    QRP
    100240 0000016.00  
    QRR
    100241 0000017.00  
    QRS
    100242 0000018.00  
    QRT
    100243 0000019.00  
    QRU
    100244 0000020.00  
    QRV

    • Proposed as answer by Frederik Long Friday, November 7, 2014 7:06 AM
    Wednesday, November 5, 2014 8:32 PM
  • Thank you for the update. For some reason, I was unable to copy the same format as my original file is. This website doesn't allow me to keep the spaces between the second and third column. Hence attaching the screenshot:



    • Edited by SQL_SSIS_Dev Wednesday, November 5, 2014 9:12 PM
    Wednesday, November 5, 2014 8:59 PM
  • Thank you for the update. For some reason, I was unable to copy the same format as my original file is. This website doesn't allow me to keep the spaces between the second and third column. Hence attaching the screenshot:

    Are you asking a question?
    Wednesday, November 5, 2014 9:30 PM
  • I am just trying to show the actual file screenshot. Meanwhile I am trying out the solution provided by you. I will update you.
    Wednesday, November 5, 2014 10:06 PM
  • The HTML appears to be purposely obfuscated to prevent screen scraping.  You will find that you will get randomly changing formats with each copy.

    Copy or save the html and post it as a file.  Sometimes it has an bvious format that can be parsed.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, November 5, 2014 10:50 PM
    Wednesday, November 5, 2014 10:40 PM
  • I tried this and it didn't work.

    @echo off
    set Line=
    if exist c:\test\newfile.txt del c:\test\newfile.txt
    for /F "skip=2 tokens=1-2" %%a in (C:\test\Bonus_File_Export.txt) do call :Sub %%a %%b
    goto :eof

    :Sub
    if not "%2"=="" (
      set Line=      %1        %2
    ) else (
      echo %Line% %1>>c:\test\newfile.txt
    )

    Wednesday, November 5, 2014 10:47 PM
  • Assume a file with no headers and with this structure:

    100239 0000015.00
    QRP
    100239 0000015.00
    QRP
    100239 0000015.00
    QRP
    100239 0000015.00
    QRP
    100239 0000015.00
    QRP
    100239 0000015.00
    QRP

    This works every time:

    Get-Content file.txt -ReadCount 2 |%{'{0} {1}' -f $_[0],$_[1]} | Out-File newfile.txt

    We can also easily skip headers.


    ¯\_(ツ)_/¯

    Wednesday, November 5, 2014 10:56 PM
  • I tried this and it didn't work.


    I fully tested the code and it works perfectly on my machine, using the input file I posted. Your feedback ("It did not work") tells me exactly nothing. You need to try a fair bit harder in trying to find out what is going on. Remember - this forum does not deliver scripts on demand. You will have to put a certain amount of your own effort into this project.
    • Marked as answer by Bill_Stewart Friday, December 5, 2014 3:41 PM
    Wednesday, November 5, 2014 11:00 PM