1.

Solve : Merge CSV files then paste into a column in the master sheet?

Answer»

Hi,
I use a simple batch file to copy all csv's in a folder and merge them into one master csv file. We then open this in excel to check productivity. I know this would be much easier to write in VB on excel, but i was curious if a batch file could do this. Basicaly, I want to know if I could have a portion of the file name placed into the last column of the csv file. For example:

Copy "directory\path\*.csv" "directory\path\All_Csv"
then possibly
like a where statement (since it is a UNC on windows 7)
net use V: \\server\path
where "\\server\path:*.csv*" > results.txt or pipe to the clipboard i suppose
then somehow copy the results of the txt file
and place them in the last or first column on csv file

However, these files contain 50 to 100s of lines each, so i would want the file name to replicated on the file it goes with

I am really interested in being able to copy the results of a where statement or results of any command without physically using the mouse.

Thanks,I am not fully understanding what your input is and what your intended output is.
Could you give us an example of what two of the input files LOOKS like and then an example of what the outfile needs to look like.

file1
Code: [Select]apple,8123
orange,9123file2
Code: [Select]beans,2324
peas,234243Output
Code: [Select]apple,8123,?????
orange,9123,?????
beans,2324,?????
peas,234243,?????Squash,
Ill explain the process. These csv files are extracts out of xls sheets that my team works. These extracts get picked up in a nightly process and inserted in SQL tables that load into databases. All csv files go from Column A to Column DD in excel. We were wanting to combine the files in order to create a quick pivot table to check productivity for quick results on certain occasions. All files have the same column headers given in below in File1 and the details of course correspondence in the same sequence. They are named in the following way Work_Client_UserNameAndCompany_Date-1741.csv. Basically what I want to do is ADD the whole file name and/or just the UserName in column DF of the report, or the last column the notepad code given below. So there would be a header added to the end of this file called File_Name and then under it following the logic it would provide the file name.

File1:
Code: [Select][PRACTICE,DATA-BASE,CHARGE STATUS,CURR PST,CURR PIWC,AGING WEEKS,AGING BUCKET,ACCT,PatAltID,CHARGE,DOS,CHG POST DATE,POS,DEPT,CHG REF PROV CODE,CHG PROV CODE,CHG ORD PROV CODE,CURR INSR CODE,CUR INS POLICY NUMBER,CUR INS GROUP NUMBER,CUR INS REL CODE,PMT CODE,MAPPED PMT MSG 1,MAPPED PMT MSG 2,MAPPED PMT MSG 3,MAPPED PMT MSG 4,PMT INS PAID CODE,PROC CODE,MOD 1,MOD 2,CHG UNITS,DX Code 1,DX Code 2,DX Code 3,DX Code 4,OWED AMOUNT,DOS TOTAL,ACCT,CHARGE,CTRL GROUP NUMBER ,PAY / ADJ CODE,NEXT PAYR INS CODE,NEXT PAYR PIWC,PRNT CLM,MSG 1,MSG 2,MSG 3,MSG 4,CHECK NUMBER,BANK ABA NUMBER,ACTION / NOTES,STATUS,DATE WORKED,AGING DATE,PMT POST DATE,PMT DATE,PMT AMOUNT,PMT CODE DUP,PMT MSG 1,PMT MSG 2,PMT MSG 3,PMT MSG 4,Pmt ABA,PMT CTRL GRP,PAT FIRST NAME,PAT LAST NAME,PAT DOB,ACCOUNT INSURANCE TOTAL,CHARGE BILLED AMT,CHARGE PAID AMT,CHARGE ADJ AMT,REF PLUS CHG REF AUTH NO,CUR INS PRI PREAUTH,CHG INS SET,CHG INS HIST,DFLT INS SET,DFLT INS HIST,SET ERROR,LAST CLAIM DATE,PRI CLAIMS,SEC CLAIMS,TER CLAIMS,CLAIM DELAY,CLAIM GROUP,AGE DAYS,CHG CUR INS CODE,CHG PRI INS CODE,CHG SEC INS CODE,CHG TER INS CODE,CUR INS NAME,CUR PIWC,CUR INS TYPE,CUR INS CLASS,PRI INS NAME,PRI PIWC,PRI INS TYPE,PRI INS CLASS,SEC INS NAME,SEC PIWC,SEC INS TYPE,SEC INS CLASS,TER INS NAME,TER PIWC,TER INS TYPE,TER INS CLASS,ANALYST,KEY,REPT DATE]Squashman might have a better way to do it but here's mine: it creates newfile.csv

Code: [Select]@echo off
for %%a in (*.csv) do set /p var=<"%%a">nul & goto :next
:next
>newfile.tmp echo %var%,File_Name
for %%a in (*.csv) do (
for /f "skip=1 delims=" %%b in ('type "%%a"') do (
>> newfile.tmp echo %%b,%%a
)
)
move /y newfile.tmp newfile.csv >nulAll I really wanted was a small example like I described in my first post. I am a visual person. I need to see the input and see what you want the output to look like.
I think Foxidrive has you covered.Sorry sqush I am new to this form, ill consider shortening my example in future questions. The code fox gave me works perfectly. It does delete the headers in the file, but not a biggie i can always grab the headers from a different file and paste them into the first row.

I really appreciate you both HELPING me with this.

Thanks,Quote from: codemonkey on August 11, 2012, 11:26:52 PM

The code fox gave me works perfectly. It does delete the headers in the file, but not a biggie i can always grab the headers from a different file and paste them into the first row.

It is supposed to create the headers for the new csv file, but remove all the headers of the individual files.

If you want to keep the headers for all the individual files then remove skip=1

I also note now that the headers you have are too long for the set /p command.

Try this:

Code: [Select]@echo off
for /f "delims=" %%a in ('dir /b *.csv') do (
for /f "delims=" %%b in ('type "%%a"') do (
>newfile.tmp echo %%b,File_Name
goto :next
)
)
:next
for %%a in (*.csv) do (
for /f "skip=1 delims=" %%b in ('type "%%a"') do (
>> newfile.tmp echo %%b,%%a
)
)
move /y newfile.tmp newfile.csv >nul


Discussion

No Comment Found