Tuesday 15 April 2014

input - .txt file data into 2 column csv file batch file -


Hope this is a simple answer. Actually I would like to be able to get the content of a multinay text file, which we will call on FileSample.txt. There may be some lines with some rows

  some text sample 1: extra 1 some text sample 2: extra 2 some text sample 3: extra 3 some text sample 4: extra 4   

and inevitably enter it in 2-column CSV, we will call Output.csv. The first column should be the file name and the second column should contain the content of the text file (including hard return). So essentially it should be in the CSV ...

  filename, content fileSample.txt, "some text sample 1: extra 1 some text sample 2: extra 2 some text sample 3: additional 3 Some Text Sample 4: Extra 4 "/ Code>  

I apologize for the confusion here, and completely noob scripting skills. I'm really trying to do this so that I made a solution with PowerShell.

  $ content = gate-content \ FileSample.txt $ FileName = Get-ChildItem \ FileSample.txt new-object PSObject -Property @ {FileName = $ FileName.Name Content = "$ Content"} | Export-CSV \ Output. Csv -NoTypeInformation   

more than I do not want to deviate from its initial request to obtain it through the batch file, I did manage to get it ExifTool Using a file saved from the resort to the outside without VBScript results it out.

  dim WshShell, oExec Set WshShell = WScript.CreateObject ( "WScript.Shell") set oExec = WshShell.Exec ( "% comspec% / c", "d: \ EXIFTOOL \ exiftool.exe "" test.jpg ") x 1 = oExec.StdOut.ReadAll x2 = Change (x1, chr (34), chr (34) & amp; chr (34)) set objFSO = CreateObject (" Scripting. FileSystemObject ") set objLogFile = objFSO.CreateTextFile ( 'D: \ EXIFTOOL \ Output.csv", 1, false) objLogFile.Write "filename" ObjLogFile.Write "ExifData" objLogFile.Writeline objLogFile.Write chr (34) & amp ; "Sample6.csv" & amp; CRR (34) & amp; "," ObjLogFile.Write chr (34) & amp; X 2 and amp; chr (34) objLogFile.Writeline objLogFile.Close    

post
  close @ECHO SETLOCAL ENABLEDELAYEDEXPANSION @ECHO file name, content set file = FileSamp Le.txt set == "% file% ^", ^ "for / f" token = * "%% l (% file%) DO @ ((set line =%% L) & amp; (@ECHO! First of all! Line: ^ "^ ^" ^ "!) & Amp; (SET FIRST =)) @ECHO ^"   

This resonates the file name, A comma, a double quote, then each row of the file (with double quotes "by transferring them to the place of double-double quotations" which is necessary if the data can contain double quotation in the file), a (closing) double quote after.

I see that one limitation is that if the file does not end with a new line, the closing double quote will effectively add one. A new line, for loop, will not process that blank line, and the output is correct. Overcoming that limit means you have to manually calculate lines (extra loop same e lines) and add an additional variable and check in for loop. To think about me, it is very messy, unless you really are, actually want that behavior (at some point, in some batch / CMD powershare, VBScript or anybody Based on other real programming languages, the simple worsens things to do.)

If you intend to process multiple files, then you have the last three lines "Sub-rootin" (for creating nested for loops My priority) and can call it in the loop, which takes the files in the questions.

  @ECHO off SETLOCAL ENABLEDELAYEDEXPANSION @ECHO file name, (FileSample * .txt) in material %% say f: PROCESS_FILE "%% ~ f" goto: EOF: PROCESS_FILE set " File =% ~ 1 "set first =" "% FILE% ^", ^ "FOR / F" token = * "%% l IN (% FILE%) DO @ ((SET LINE = %% l) & amp; (@ECHO! First of all! Line: ^ "^ ^" ^ "!) & Amp; (SET FIRST =)) Extra citation of arguments for AECO ^" XIT / B   

: PROCESS_FILE is a case in the file name when handling the case. If there is a place in it and you do not quote it, then the fragment of the file name will be treated like a different argument. I'd use *% in that position, but I like to quote things to make it clear That I want the data considered as a single entity.

No comments:

Post a Comment