Link to home
Start Free TrialLog in
Avatar of leop1212
leop1212Flag for United States of America

asked on

how to merge to CSV files as a scheduled task

We have two CSV files exported from two different apps   on a weekly basis. We have to merge them and want to run it  as schedule task.
if i do it in SQL it will look like
Select a.key1, a.first, a. last, a.key2, b.number into c where a.key2=b.key2 and  a source1, b source2, c results

How can it be done with excel so we run it a a scheduled task? I all looking for a working syntax.
results.csv
source1.csv
source2.csv
ASKER CERTIFIED SOLUTION
Avatar of Eric Woodford
Eric Woodford
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abbas abdulla
Hi,

you can use this code
1. Change the paths to match your paths.
2.  Copy the code into new Notepad file
3.  Save the notepad file with  .bat extension.
4. schedule a task to open this file at the time or event you want the CSV files to be merged.


:: Merge CSV
@echo off
setlocal
set Source=C:\Users\Myuser\Downloads\Undo
set Dest=C:\Users\Myuser\Downloads\Undo\allbat.csv
::for /f "delims=" %%a in ('dir "%Source%\*.csv" /b /s') do (
::copy "%%a" "%Dest%"
copy "%Source%\*.csv" /b %Dest%

::)
pause

Open in new window

Avatar of leop1212

ASKER

when you merge two files i don' t see how you align lines by a.key2=b.key2 ?
If you are referring to my script, I use cmd line parameters..
 [parameter(Mandatory=$true,HelpMessage="Path and filename for source CSV 1")][ValidateScript({Test-Path $_ })][string]$csv1Name,
    [parameter(Mandatory=$true,HelpMessage="Propery from CSV1 to join files on.")][string]$csv1Property,
    [parameter(Mandatory=$true,HelpMessage="Path and filename for source CSV 2")][ValidateScript({Test-Path $_ })][string]$Csv2Name,
    [parameter(Mandatory=$true,HelpMessage="Propery from CSV2 to join files on.")][string]$csv2Property,
    [parameter(Mandatory=$true,HelpMessage="Path and Name for combined CSV file")][string]$JoinedCSV

Open in new window


$CSV1Property and $CSV2Property are the names of the keys $a.key and $b.key

In your sample CSV files, you have duplicate fields with different results. When I designed the script, I didn't have duplicate and different results (i.e. first name = first name in both CSV)

 .\CSVJoin.ps1 -csv1Name .\source1.csv -Csv2Name .\source2.csv -csv1Property key2 -csv2Property key2 -JoinedCSV .\myResults.csv

Open in new window

User generated image
Do the source files have same name, and location every week?
What shall happend with old source files after merge to result?
Shall it be any role for the name of the latest result file?
Is all fields always in the same order as in your sample?