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

Author Topic: Batch File to Convert Txt files  (Read 11108 times)

0 Members and 1 Guest are viewing this topic.

codemonkey

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Batch File to Convert Txt files
    « on: September 27, 2012, 04:17:28 PM »
    Hi,
    I am trying to convert data in a text file to a delimiter format to open in excel.
     
    Below is a sample code of what I want to do:
     
    Text file comes like below (Always a first word with ":" and then information regarding it next
    Code: [Select]

    Apple:  1234
    Orange: Jon Thomas
    Pear:  He was walking down the street

    What I want the new txt or csv file to look like in excel (The pipes are just used for reference as columns)
    Code: [Select]

     Apple    |   Orange    |                Pear                          |
    1234      | Jon Thomas|   He was walking down the street|

    I was trying to use a For  F/ delims command that I used to merge CSV files however I could not figure it out.
     
    Thanks,

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch File to Convert Txt files
    « Reply #1 on: September 27, 2012, 11:27:51 PM »
    This will give you a .CSV for to open in excel.  Line length becomes important if the files are large.

    Code: [Select]
    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    for /f "tokens=1,* delims=: " %%a in (file.txt) do (
    set "var=!var!,%%a"
    set "var2=!var2!,%%b"
    )
    >file.csv echo %var:~1%
    >>file.csv echo %var2:~1%

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch File to Convert Txt files
    « Reply #2 on: September 27, 2012, 11:33:21 PM »
    Here's another way to do it which handles larger files - though there is a leading comma on each line.

    Code: [Select]
    @echo off
    for /f "tokens=1,* delims=: " %%a in (file.txt) do (
    set /p "=,%%a">>filea.tmp <nul
    set /p "=,%%b">>fileb.tmp <nul
    )
    echo.>>filea.tmp

    copy /b filea.tmp+fileb.tmp file.csv
    del filea.tmp
    del fileb.tmp

    codemonkey

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Batch File to Convert Txt files
      « Reply #3 on: September 28, 2012, 08:11:42 AM »
      Fox,
      Thank you for the help. This is actually exactly what I was looking for.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Batch File to Convert Txt files
      « Reply #4 on: September 28, 2012, 09:58:52 AM »
      Small change to fix the leading comma.
      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion
      set comma=
      for /f "tokens=1,* delims=: " %%a in (file.txt) do (
      set /p "=!comma!%%a">>filea.tmp <nul
      set /p "=!comma!%%b">>fileb.tmp <nul
      set comma=,
      )
      echo.>>filea.tmp

      copy /b filea.tmp+fileb.tmp file.csv
      del filea.tmp
      del fileb.tmp

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Batch File to Convert Txt files
      « Reply #5 on: September 29, 2012, 12:31:17 AM »
      Small change to fix the leading comma.

      Good thinking.