csv - How can I use absolute path in batch file or force vbs not to need the path? -
i writing batch file , part of converts .csv .xlsx. i've been able incorporate solution post:
convert .csv .xlsx using command line
dim file, wb createobject("excel.application") on error resume next each file in wscript.arguments set wb = .workbooks.open(file) wb.saveas replace(wb.fullname, ".csv", ".xlsx"), 51 wb.close false next .quit end it works great, issue must pass absolute path batch file work, so:
csv2xlsx.vbs c:\users\data\ktq\abc.csv as explained in original thread, doesn't work doing following:
csv2xlsx.vbs abc.csv this bit of pain if move folder need update path. there way can force vbs take in above command correctly finding file, or can absolute path file in question , pass in somehow? great!
the following script provide path of running .vbs script. can use such script determine path of files.
if, instance, script stored in c:\users\data\ktq\yourscript.vbs", script bellow outputstrmainpathasc:\users\data\ktq` (without trailing backslash):
dim objshell : set objshell = createobject("wscript.shell") dim strpath : strpath = wscript.scriptfullname dim objfso : set objfso = createobject("scripting.filesystemobject") dim objfile : set objfile = objfso.getfile(strpath) dim strmainpath : strmainpath = objfso.getparentfoldername(objfile) 'cleaning no longer needed objects set objfile = nothing set objfso = nothing set objshell = nothing print strmainpath so, supposing script in same folder .csv files, able find them all. if have level of files, in c:\users\data\ktq\myfiles\ go if change print part of code above this:
print strmainpath & "\myfiles\" adapting code, should this:
dim objshell : set objshell = createobject("wscript.shell") dim strpath : strpath = wscript.scriptfullname dim objfso : set objfso = createobject("scripting.filesystemobject") dim objfile : set objfile = objfso.getfile(strpath) dim strmainpath : strmainpath = objfso.getparentfoldername(objfile) 'cleaning no longer needed objects set objfile = nothing set objfso = nothing set objshell = nothing dim file, wb createobject("excel.application") on error resume next each file in wscript.arguments set wb = .workbooks.open(strmainpath & "\" & file) wb.saveas replace(wb.fullname, ".csv", ".xlsx"), 51 wb.close false next .quit end
Comments
Post a Comment