excel vba - Converting Dos Text Files to Unix in VBA fails -
i've followed suggestion in prior question , grabbed code don't useable file out of it. i've looked @ , don't see problem.
public enum converttype dos2unix = 0 unix2dos = 1 end enum public function convertfile(originalfile string, newfile string, econverttype converttype, _ optional deleteoriginal boolean = false) dim openfilenum, savefilenum integer dim newfilebuffer string ' function open file , convert ' txt file format usable under *nix or dos on error goto error_found openfilenum = freefile ' grab first free file open originalfile input #openfilenum ' open unix file savefilenum = freefile ' free file write open newfile binary #savefilenum ' open/create save file while not eof(openfilenum) line input #openfilenum, newfilebuffer ' retrive text (if unix file, entire text on 1 line) if econverttype = dos2unix ' check type of conversion newfilebuffer = newfilebuffer & chr(10) else newfilebuffer = replace(newfilebuffer, chr(10), vbcrlf) end if put #savefilenum, , newfilebuffer ' write out file loop close #savefilenum close #openfilenum if deleteoriginal = true kill originalfile exit_sub: exit function
try this:
option explicit sub testconversion() convertfile "c:\test.txt" end sub public sub convertfile(byval filename string) const dos2unix = 1 dim fs object, txt string set fs = createobject("scripting.filesystemobject") txt = fs.opentextfile(filename, 1).readall 'forreading = 1 txt = iif(dos2unix = 1, replace(txt, vbcrlf, vblf), replace(txt, vblf, vbcrlf)) fs.opentextfile(filename, 2).write txt 'forwriting = 2 end sub
notes:
- in notepad (windows) lines appear on 1 continuous line
- all carriagereturn–linefeed combinations replaced linefeed characters
Comments
Post a Comment