PowerShell ForEach libreoffice转换
我正在尝试将所有rtf文件转换为特定文件夹和所有子文件夹下的docx文件。 转换的文件需要与原始文件位于同一文件夹中。转换文件的名称不会更改 此代码转换文件,但不处理子文件夹。仅处理顶部文件夹PowerShell ForEach libreoffice转换,powershell,libreoffice,Powershell,Libreoffice,我正在尝试将所有rtf文件转换为特定文件夹和所有子文件夹下的docx文件。 转换的文件需要与原始文件位于同一文件夹中。转换文件的名称不会更改 此代码转换文件,但不处理子文件夹。仅处理顶部文件夹 foreach ($file in Get-ChildItem -Recurse -Force "C:test" -Filter *.rtf ) { &("C:\Program Files\LibreOffice\program\soffice.exe") -headless
foreach ($file in Get-ChildItem -Recurse -Force "C:test" -Filter *.rtf )
{
&("C:\Program Files\LibreOffice\program\soffice.exe") -headless -convert-to docx $file | Out-Null
}
下面的代码处理子文件夹,但由于某些原因跳过某些文件
Get-ChildItem "C:\test" -Filter *.rtf -Recurse -Force | foreach-Object {
set-location $_.directoryname
&("C:\Program Files\LibreOffice\program\soffice.exe") --convert-to docx $_.name
}
最后,libreoffice有一个输出目录选项,但我无法让它工作
foreach ($file in Get-ChildItem "C:\test" -Filter *.rtf)
{
&("C:\Program Files\LibreOffice\program\soffice.exe") -headless -convert-to docx -outdir $FolderName $file | Out-Null
}
上面的代码处理并转换所有文件。
谢谢大家。您可以尝试使用包含路径的文件名,而不是
$file
$file.FullName
@Olaf更正,Get ChildItem
返回的对象是文件对象,而不仅仅是文件名或-path。LibreOffice确实需要文件路径。
Get-ChildItem "D:\OneDrive - University of Houston\Anaconda3\JupyterFiles\audio\EarningsCalls\2020" -Filter *.rtf -Recurse -File| foreach-Object {
set-location $_.directoryname
write-host ($_)
write-host ($_.FullName)
write-host ($_.DirectoryName)
&("C:\Program Files\LibreOffice\program\soffice.exe") --convert-to docx -outdir $_.DirectoryName $_ | Out-Null
}