将文件从共享文件移动到.txt,然后将其删除

时间:2019-03-08 00:49:31

标签: excel windows powershell

我正在获取一份excel报告,将其从共享驱动器中移出,并将其部分文件名更改为txt文件,然后重命名,而不是从citrix中删除该文件。问题是文件没有更改为txt或重命名,也没有将其从citrix中删除

我对Powershell刚起步,有人可以提供见解吗?

Move-Item -Path 'S:\test\test*.xls'  -destination c:\test

$files = Get-ChildItem c:\test\test*.xls 

$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $false
$Excel.DisplayAlerts = $false

ForEach ($file in $files) {
 Write "Loading File '$($file.Name)'..."
 $WorkBook = $Excel.Workbooks.Open($file.Fullname)
 $NewFilePath = [System.IO.Path]::ChangeExtension($file.Fullname,".txt")
 $Workbook.SaveAs($NewFilepath, 42)   # xlUnicodeText
}

# cleanup
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkBook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

Rename-Item -Path "C:\test\test*.txt" -NewName "test1.txt"
Remove-Item -Path C:\test\test*.txt `

0 个答案:

没有答案