Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file needed to wrap entire text column in double quotes  (Read 47574 times)

0 Members and 1 Guest are viewing this topic.

BatchNoob21

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    Batch file needed to wrap entire text column in double quotes
    « on: November 08, 2021, 12:07:59 AM »

    Just joined here to ask this ! I have a text file that has multiple lines without any quotes. I need a batch file that will append a single double quote at the beginning of the first line (not on a separate line by itself , it must be the first character on the first line) I need the same for the very last line position also. So to end up looking like

    "One
    Two
    Three
    Four
    Five
    Six
    Seven"

    ...Therefore wrapping the entire column. I hope you guys can solve this for me as its been driving me nuts ! :)

    Thanks in advance !

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch file needed to wrap entire text column in double quotes
    « Reply #1 on: November 09, 2021, 02:20:49 PM »
    It is customary to post whatever code you have created so we can better see where you're going with this. In any case, my batch codes days are pretty much behind me, but I did come up with this:

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set count=0
    set ifile=g:\wfc\testlib\hope.txt
    set ofile=c:\temp\hopeless.txt

    for /f %%b in ('type !ifile! ^| find "" /v /c') do set ubound=%%b

    for /f %%i in (!ifile!) do (
      set /a count=!count!+1
      if !count! EQU 1 ( echo ^"%%i >> !ofile!
      ) else if !count! EQU !ubound! ( echo %%i^" >> !ofile!
      ) else ( echo %%i >> !ofile! )
    )

    Change the ifile and ofile values in the set statements to match your environment.

    Happy Coding  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein