Mike
asked on
Need a Powershell Script to Covert .csv to .xlsx dcouments
Greeting Experts,
Does anybody have a script that can change the extension of excel file from “.csv” to “.xlsx “. I have a list of excel documents that I manually change daily (i.e. open, and save with .xlsx) but that requires me to at work every day… the script needs to look at creation date on documents properties, change only that document (for that day), and then delete .csv file . Does anybody have a script that can do this?
Does anybody have a script that can change the extension of excel file from “.csv” to “.xlsx “. I have a list of excel documents that I manually change daily (i.e. open, and save with .xlsx) but that requires me to at work every day… the script needs to look at creation date on documents properties, change only that document (for that day), and then delete .csv file . Does anybody have a script that can do this?
The only way is to do exactly what you do per script. That is, read the CSV into Excel, and store it as xlsx. This can be done in VBS and PS.
I do not understand your date restriction - do you mean the CSV file needs to be created today or yesterday or such?
I do not understand your date restriction - do you mean the CSV file needs to be created today or yesterday or such?
ASKER
I receive daily reports in "report.csv" format and need to covert them over to "report.xlsx" format. I don't need for the script to to do the entire list in Network Folder. Just the one that has been received for that day (i.e. Monday, Tuesday, Wed, etc)..... Just so the script does not have to long period of time. Then I want the script to delete the .csv file once the excel document has been covered over .xlsx
Still not getting it. If the file is always "reports.csv", it is overwritten the next day if not processed, or if processed does not exist any more (since it has been deleted).
set-StrictMode -Version latest
$file = 'C:\Daily Reports\reports'
$excel = New-Object -ComObject excel.application
$excel.visible=$true
$wb = $excel.Workbooks.Open($file + '.csv')
$wb.SaveAs($file + '.xlsx')
$excel.Quit()
remove-item $file + '.csv'
ASKER
Thanks Dan Craciun, it works perfect
Yes, it does rename the file. But can you actually open it?
Cause an xlsx file is an archive (rename it to zip and you'll see) and the csv is a text file.
I don't think Excel will like your newly renamed file...
Cause an xlsx file is an archive (rename it to zip and you'll see) and the csv is a text file.
I don't think Excel will like your newly renamed file...
ASKER
I was able to run the script to covert the file from .csv to .xlsx using the same name and then delete the .csv file.... Original File name did not change
That cannot work! You will get complaints by Excel about having the wrong format. As stated, xlsx is a completely different format from CSV.
ASKER
I will have to take back what I said... it did change the ext. of the file but it did not convert the file.... now they will not open up... error message " the file format or file extension is not valid. Verify the file has not been corrupted and the file extension matches the format of the file.
ASKER
I jumped the gun too soon.... sorry about that Qlemo..... Is there a way to modify this script to covert it over...?
You asked for
My script does that, but that was not what you needed.
I have not tested Qlemo's script, but it looks like it should give you what you need.
a script that can change the extension of excel file from “.csv” to “.xlsx “
My script does that, but that was not what you needed.
I have not tested Qlemo's script, but it looks like it should give you what you need.
ASKER
what I need is to covert a .csv formatted file over to .xlsx formated excel file that i am able to open .... sorry i was not more clear..... thanks for your help head time on this issue...
I'm confused, didn't the last PS1 script from Qlemo do just that? I was getting ready to fashion one in VBS when I saw that and thought we were all set.
~bp
~bp
Yes, Bill, but the PS1 script had some flaws. This is the improved one:
The VBS code would be almost the same.
set-StrictMode -Version latest
$file = 'C:\temp\ee\reports'
if (Test-Path "$file.csv")
{
Remove-Item "$file.xlsx"
$excel = New-Object -ComObject excel.application
$excel.visible=$true
$wb = $excel.Workbooks.Open("$file.csv")
$wb.SaveAs($file, [Microsoft.Office.Interop.Excel.xlFileFormat]::xlOpenXMLWorkbook)
$wb.Close()
$excel.Quit()
Remove-Variable wb, excel
Remove-Item "$file.csv"
}
It does not throw any error, runs only if there is a reports.csv file, and is able to overwrite the existing reports.xlsx file.The VBS code would be almost the same.
ASKER
I ran the script on a test folder with a few .csv files and did not see anything change. did not get any error messages as well...
set-StrictMode -Version latest
$file = 'C:\test'
if (Test-Path "$file.csv")
{
Remove-Item "$file.xlsx"
$excel = New-Object -ComObject excel.application
$excel.visible=$true
$wb = $excel.Workbooks.Open("$file.csv")
$wb.SaveAs($file, [Microsoft.Office.Interop.Excel.xlFileFormat]::xlOpenXMLWorkbook)
$wb.Close()
$excel.Quit()
Remove-Variable wb, excel
Remove-Item "$file.csv"
}
That's because the script looks for C:\test.csv
Make $file a parameter and run the script with the -file option.
HTH,
Dan
Make $file a parameter and run the script with the -file option.
HTH,
Dan
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I can say both of you came up with a solution that works (prof of concept) for my needs... It does cover the files from .csv to .xlsx file and then deletes the original .csv file once the first step is done...
Param (
[string]$file = '\\Network Share\folder1'
)
$filesToConvert = Get-Item -path ($file + "\*.csv") | Where-Object {$_.LastWriteTime -gt ((Get-Date).AddDays(-5))}
foreach ($fileToConvert in $filesToConvert)
{
$excel = New-Object -ComObject excel.application
$excel.visible=$false
$wb = $excel.Workbooks.Open("$fileToConvert")
$wb.SaveAs(($fileToConvert.FullName.Substring(0,$fileToConvert.FullName.Length-3) + 'xlsx'), [Microsoft.Office.Interop.Excel.xlFileFormat]::xlOpenXMLWorkbook)
$wb.Close()
$excel.Quit()
Remove-Variable wb, excel
Remove-Item $fileToConvert
}
ASKER
I've requested that this question be closed as follows:
Accepted answer: 250 points for Qlemo's comment #a39840696
Assisted answer: 0 points for amstoots's comment #a39840636
for the following reason:
thank you
Accepted answer: 250 points for Qlemo's comment #a39840696
Assisted answer: 0 points for amstoots's comment #a39840636
for the following reason:
thank you
ASKER
thank you
ASKER
yes... thanks...
ASKER
Thanks,....for the help on this problem.....guys... :)
Open in new window
This will rename all csv files from today to a xlsx extensions
HTH,
Dan