Jump to content

Recommended Posts

Posted

Need help~

 

I have a directory with multiple csv. e.g.

 

aaa1.csv

aaa2.csv

aaa3.csv

aaa4.csv

aaa5.csv

 

after using powershell, I can merge those csv into merge.csv

 

how do I list the *.csv and only take the string before 1.csv and rename from merge.csv into aaa-merge.csv ?

 

anyone can advice?

Posted
Are you looking to rename the existing CSV files and also merge them to produce a single csv file, or merge them to a single CSV file that gets its filename from the files merged to make it? :confused:
Posted
Are you looking to rename the existing CSV files and also merge them to produce a single csv file, or merge them to a single CSV file that gets its filename from the files merged to make it? :confused:
Merge them into a single csv and the single csv name will be the filenames exclude the 1 2 or 3.

 

E.g.

 

aaa1.csv

aaa2.csv

aaa3.csv

 

So Merge filename should be aaa-merge.csv

 

Any ideas?

Posted

Will the file name always be letters and never numbers? so for instance you'll never have:

 

a1aa1.csv

a1aa2.csv

a1aa3.csv

 

and expect to have a1aa-merge.csv as the output?

Posted (edited)

excluded so they aren't needed in the merged csv filename? using my previous example:

a1aa1.csv

a1aa2.csv

a1aa3.csv

would merge to aaa-merge.csv?

If so this will do what you need, must be saved and run from the same directory as the csv files.

 

If this is not the case and you'd still want to retain any numbers that aren't at the end of the filename this will not do what you need and will require more tweaking to the way $FilenameM is populated to get the filename of the merged csv how you require.

 

$Allcsv = Get-ChildItem -Filter *.csv 
$FilenameM = $Allcsv[0].BaseName -replace '[^a-zA-Z-]',''

foreach ($csv in $Allcsv) {
    Import-Csv -LiteralPath $csv.FullName | Export-Csv .\$filenameM-merge.csv -NoTypeInformation -Append
}

Edited by ThomL
  • Thanks 1
Posted
excluded so they aren't needed in the merged csv filename? using my previous example:

a1aa1.csv

a1aa2.csv

a1aa3.csv

would merge to aaa-merge.csv?

If so this will do what you need, must be saved and run from the same directory as the csv files.

 

If this is not the case and you'd still want to retain any numbers that aren't at the end of the filename this will not do what you need and will require more tweaking to the way $FilenameM is populated to get the filename of the merged csv how you require.

 

$Allcsv = Get-ChildItem -Filter *.csv 
$FilenameM = $Allcsv[0].BaseName -replace '[^a-zA-Z-]',''

foreach ($csv in $Allcsv) {
    Import-Csv -LiteralPath $csv.FullName | Export-Csv .\$filenameM-merge.csv -NoTypeInformation -Append
}

Thanks!! Will try them out

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...