goterrier Posted September 13, 2018 Posted September 13, 2018 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?
ThomL Posted September 13, 2018 Posted September 13, 2018 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?
goterrier Posted September 13, 2018 Author Posted September 13, 2018 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? 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?
ThomL Posted September 13, 2018 Posted September 13, 2018 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?
goterrier Posted September 13, 2018 Author Posted September 13, 2018 Definately will have 1 2 or 3 before the .csv Any numbers before the 1 2 or 3 shall be excluded.
ThomL Posted September 13, 2018 Posted September 13, 2018 (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 September 13, 2018 by ThomL 1
goterrier Posted September 13, 2018 Author Posted September 13, 2018 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now