use strict; use warnings; use Cwd; use Win32::OLE; my $ext = ".xls"; my $newext = ".csv"; my $Excel=Win32::OLE->new('Excel.Application'); $Excel->{Visible}=1; $Excel->{DisplayAlerts}=1; #Set to 0 when the code is working, but keep at 1 while debugging. my $dir = getcwd; $dir =~ s/\//\\\\/g; #Sort out the slashes and backslashes opendir(DIR, $dir) or die "can't opendir $dir: $!"; #Cookbook recipe 9.5 while (defined(my $file = readdir(DIR))) { if ((!-d $file) and (substr($file, -length($ext)) eq $ext)) { #Someone WILL call a directory something.xls. I have the scars. my $Book = $Excel->Workbooks->Open("$dir\\\\$file") or die "Can't open file $dir\\\\$file"; my $sheet = $Book -> Worksheets(1); $sheet -> Range ('A:A') -> {NumberFormat} = "000"; #The next line is pretty explicit to try to make it clear what's going on. $Book->SaveAs({Filename => $dir . "\\\\" . substr($file, 0, length($file) - length($ext)) . $newext, FileFormat => 6, #xlCSV, CreateBackup => 0}); $Book->Close; } } closedir(DIR); $Excel->Quit;