none
batch script to modify the file content format RRS feed

  • Question

  • Hello, 

    I've a text file like this:

    I need to create a .dat file using the above .txt file as shown below:

    This is the code I tried so far:

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

    This code removed the unwanted lines and changed the format of the file to .dat. 

    Now, how do I insert/manage the spaces between the columns?

    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.

    Wednesday, December 24, 2014 9:03 PM

Answers

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

    Until you get around to learning PowerShell, here is an easy-to-understand batch solution.

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

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

    • Marked as answer by SQL_SSIS_Dev Friday, December 26, 2014 4:51 PM
    Wednesday, December 24, 2014 11:18 PM

All replies

  • What do you mean by a "dat" file.  There is no such file type.  Anything can have a DAT extension.

    You have to specify the format you need.  This can be done in one line with PowerShell so forget about using batch.  Just post the column specs.


    ¯\_(ツ)_/¯

    Wednesday, December 24, 2014 10:15 PM
  • Here is is spread over three lines so it is easier to understand.

    $lines=Get-Content C:\test\Bonus_File_Export.txt
    $lines[1..$lines.Count]|
        ForEach-Object{
             '{0,11}{1,18}{2,4}' -f $_.Split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
        } # | Out-File Newfile.dat

    It is not complete because you need to supply the line specs.

    You can also use string cutting in batch to do the exact same thing.  Tiy should notbelearningbatch.  If you spend any time learning anything it should be PowerShell.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, December 24, 2014 10:30 PM
    Wednesday, December 24, 2014 10:22 PM
  • 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.

    Until you get around to learning PowerShell, here is an easy-to-understand batch solution.

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

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

    • Marked as answer by SQL_SSIS_Dev Friday, December 26, 2014 4:51 PM
    Wednesday, December 24, 2014 11:18 PM
  • Thank you very much Sir!
    Friday, December 26, 2014 4:51 PM