excel - How to open up each CSV file in current directory and add column of data based on CSV file name with an automated script -
i have group of csv files specific name includes date (e.g., reserve013112-sheet1.csv
, reserve013112-sheet2.csv
).
all of files have date in file name following "reserve" word. want extract date (in date format) , add column existing csv (potentially insert first column in .csv
file) follows:
old file:
number,status,reserve,open,code 110035,open,250000,yes,no,12 110056,auto,30000,no,yes,0 ...
new file:
date,number,status,reserve,open,code 01-31-2012,110035,open,250000,yes,no,12 01-31-2012,110056,auto,30000,no,yes,0 ...
i want automatically (potentially batch file).
i have script converts existing excel files csv
files, want run in batch file right before script adds date each of files.
as many text processing tasks, job can made simple , efficient regular expression utility. excellent option jrepl.bat, pure script (hybrid jscript/batch) utility runs natively on windows machine xp onward.
the solution below first uses list *reserve*.csv
files.
next uses /f coupled jrepl.bat parse out month, day, , year file name. if values not found, file skipped.
finally uses jrepl.bat call insert either "date," @ beginning of first line, or "mm-dd-20yy," @ beginning of other lines. last command uses output /o -
option overwrite original file result.
@echo off /f "eol=: delims=" %%f in ( 'findstr /vin "^date," *reserve*.csv' ) /f "tokens=1-3" %%a in ( 'echo %%f^|jrepl "^.*reserve(\d\d)(\d\d)(\d\d).*" "$1 $2 $3"' ) call jrepl "^" "(ln==1)?'date,':'%%a-%%b-20%%c,'" /j /f "%%f" /o -
there 1 potential problem above - modify file, if has date @ beginning of each line. bit more user supplied jscript can used prevent modification of files have date first column.
@echo off /f "eol=: delims=" %%f in ( 'findstr /vin "^date," *reserve*.csv' ) /f "tokens=1-3" %%a in ( 'echo %%f^|jrepl "^.*reserve(\d\d)(\d\d)(\d\d).*" "$1 $2 $3"' ) call jrepl "^" "(ln==1)?'date,':'%%a-%%b-20%%c,'" /j /f "%%f" /o - ^ /jbegln "if (ln==1 && $txt.search(/^date,/i)>=0) skip=true"
Comments
Post a Comment