- 帖子
- 2
- 积分
- 14
- 技术
- 0
- 捐助
- 0
- 注册时间
- 2018-3-26
|
小弟托人写了一个powershell脚本,用途是
1.文件夹里面的文件是PDF,那么就将这些PDF的文件名字重命名,重命名格式是原来的文件名称_文件夹名称
例如文件名字是a,文件夹名字是1,那么重命名后的PDF名字是
a_1.pdf. 如果有多个PDF,例如b,c...那么就变成
a_1.pdf
b_1.pdf
c_1.pdf 如此类推
2.如果文件夹里面的文件是非PDF,那么就将这个文件首先转换为PDF,然后再进行1的操作进行重命名.
以上的功能在powershell环境下运行时是没有问题的,但是当脚本在计划任务下的时候运行,就出现问题,问题就是以上第二点不工作,也就是非PDF不会转换为PDF,直接把非PDF命名,而且命名也错了,命名为1_a/1_b/1_c.
所以需要请教一下群里大侠需要怎么设置计划任务,或者修改一下代码?
在计划任务下是这样设置命令的
powershell -command ".'G:\Section A\PS\run_new.ps1'"
以下为脚本代码
############################################################
# Get all documents waiting for handle
############################################################
function Get-WaitingHandleDocument
{
param([string]$WorkDir)
Get-ChildItem $WorkDir -File |
where {
$SafeFileExtension -contains $_.Extension
}
}
############################################################
# Convert Word document to PDF
############################################################
function Convert-WordToPdf{
param(
[io.fileinfo]$DocPath,
[string]$PdfPath
)
$wordApp = New-Object -ComObject Word.Application
#$wordApp.Visible = $true
$document = $wordApp.Documents.Open($DocPath.FullName)
$document.SaveAs([ref] $PdfPath, [ref] 17)
$document.Close()
$wordApp.Quit()
}
############################################################
# Convert Excel document to PDF
############################################################
function Convert-ExcelToPdf
{
param(
[io.fileinfo]$DocPath,
[string]$PdfPath
)
$xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type]
$objExcel = New-Object -ComObject excel.application
$workbook = $objExcel.workbooks.open($DocPath.fullname, 3)
$workbook.ActiveSheet.PageSetup.Orientation = 2
$objExcel.PrintCommunication = $false
$workbook.ActiveSheet.PageSetup.FitToPagesTall = $false
$workbook.ActiveSheet.PageSetup.FitToPagesWide = 1
$objExcel.PrintCommunication = $true
$workbook.Saved = $true
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $PdfPath)
$objExcel.Workbooks.close()
$objExcel.Quit()
}
############################################################
# Convert images file to PDF
############################################################
function Convert-ImageToPdf
{
param(
[io.fileinfo]$DocPath,
[string]$PdfPath
)
# Create a word document
[ref]$SaveFormaR9t = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
#$word.Visible = $true
$doc = $word.documents.add()
# Insert the target image file into document
$word.Selection.InlineShapes.AddPicture($DocPath) | Out-Null
# Save the document as temporary word document
$tempWordFile = [io.Path]::GetTempFileName();
$doc.SaveAs([ref] $tempWordFile, [ref]$saveFormat::wdFormatDocument)
$doc.Close()
$word.Quit()
# Convert word document to pdf
Convert-WordToPdf -DocPath $tempWordFile -PdfPath $PdfPath
# Remove the temporary file
Remove-Item $tempWordFile
}
############################################################
# Generate a new pdf file name
############################################################
function New-PdfFileName
{
param(
[io.directoryinfo]$ParentDirName,
[string]$OrderName,
[int]$FileIndex
)
$maxFileNumes = 3
$fileIndexMask = $FileIndex.ToString().PadLeft($maxFileNumes,'0')
$newPdfName = "{0}_{1}_{2}.pdf" -f $OrderName,$ParentDirName.Name, $fileIndexMask
$newDir = Join-Path $ParentDirName $OrderName
# create pdf folder
if(-not (Test-Path $newDir)){
mkdir $newDir
}
Join-Path $newDir $newPdfName
}
############################################################
# 将文档转换成PDF
############################################################
function Convert-DocToPDF
{
param(
[io.fileinfo]$DocFile
)
#文件不存在,直接退出
if(-not $DocFile.Exists){
return
}
$pdfFileName = Join-Path $DocFile.Directory.FullName ($DocFile.BaseName+".pdf")
#如果文件目标已存在,重命名为file.001.pdf
$index = 1
while(Test-Path $pdfFileName) {
$pdfFileName = "{0}_{1}.pdf" -f $DocFile.BaseName,$index.ToString().PadLeft('2','0')
$pdfFileName = Join-Path $DocFile.Directory.FullName $pdfFileName
$index++
}
#开始转换
Write-Host "正在转换$pdfFileName ..." -NoNewline
Switch($DocFile.Extension)
{
".xls" {
Convert-ExcelToPdf -DocPath $DocFile.FullName -PdfPath $pdfFileName
}
".xlsx" {
Convert-ExcelToPdf -DocPath $DocFile.FullName -PdfPath $pdfFileName
}
".doc" {
Convert-WordToPdf -DocPath $DocFile.FullName -PdfPath $pdfFileName
}
".docx" {
Convert-WordToPdf -DocPath $DocFile.FullName -PdfPath $pdfFileName
}
".rtf" {
Convert-WordToPdf -DocPath $DocFile.FullName -PdfPath $pdfFileName
}
Default{Convert-ImageToPdf -DocPath $DocFile.FullName -PdfPath $pdfFileName}
}
Write-Host "转换结束" -ForegroundColor Green
}
#############################################################
#处理单个单号文件夹
#############################################################
function Resolve-SingleOrderDir
{
param(
[IO.DirectoryInfo]$WorkingDir
)
#步骤1,先把非PDF转换成PDF
Push-Location
Get-WaitingHandleDocument -WorkDir $WorkingDir | foreach{
Convert-DocToPDF -DocFile $_.FullName
}
#步骤2,重命名PDF追加父目录名称,并移动
cd $WorkingDir
dir *.pdf | foreach {
#2.1重命名
$newName = "{0}_{1}.pdf" -f $_.BaseName,$WorkingDir.Name
Rename-Item $_ -NewName $newName
#2.2移动到部门目录的上一层
$newPath = Join-Path $WorkingDir.Parent.FullName $newName
Move-Item $newName -Destination $newPath
}
Pop-Location
}
#############################################################
#处理多个单号文件夹
#############################################################
function Resolve-MutitpleOrderDir
{
param(
[IO.DirectoryInfo]$WorkingDir
)
#步骤1,先把非PDF转换成PDF
Push-Location
Get-WaitingHandleDocument -WorkDir $WorkingDir | foreach{
Convert-DocToPDF -DocFile $_.FullName
}
#步骤2,重命名PDF追加父目录名称,并移动
cd $WorkingDir
dir *.pdf | foreach {
#2.1重命名
$newName = "{0}_{1}_{2}.pdf" -f $WorkingDir.Name,$WorkingDir.Parent.Name,$_.BaseName
Rename-Item $_ -NewName $newName
#2.2移动到部门目录的上一层
$newPath = Join-Path $WorkingDir.Parent.Parent.FullName $newName
Move-Item $newName -Destination $newPath
}
Pop-Location
}
function RenameFolder
{
param(
[IO.DirectoryInfo]$WorkingDir
)
#步骤2,重命名PDF追加父目录名称,并移动
cd $WorkingDir
dir *.xls*,*.doc*,*.jpg,*.png,*.gif,*.bmp | foreach {
#2.1重命名
$newName = "{0}_{1}_{2}" -f $WorkingDir.Name,$WorkingDir.Parent.Name,$_.Name
Rename-Item $_ -NewName $newName
#2.2移动到部门目录的上一层
#$newPath = Join-Path $WorkingDir.Parent.Parent.FullName $newName
Move-Item $newName -Destination "E:\Done"
}
Pop-Location
}
function RenameFile
{
param(
[IO.DirectoryInfo]$WorkingDir
)
#步骤2,重命名PDF追加父目录名称,并移动
cd $WorkingDir
dir *.xls*,*.doc*,*.jpg,*.png,*.gif,*.bmp | foreach {
#2.1重命名
$newName = "{0}_{1}" -f $WorkingDir.Name,$_.Name
Rename-Item $_ -NewName $newName
#2.2移动到部门目录的上一层
#$newPath = Join-Path $WorkingDir.Parent.FullName $newName
Move-Item $newName -Destination "E:\Done"
}
Pop-Location
}
#可以安全转换为PDF的文档
#重要:每一种新增的文件类型都应当经过测试,不能轻易扩展
[string[]]$SafeFileExtension=".doc",".docx",".xls",".xlsx",".jpg",".png",".gif","bmp"#,".rtf"
#遍历所有部门文件夹
#部门列表文件夹路径
$mainWorkingDir = (Get-Item "$PSScriptRoot\..\").FullName #根据相对目录找到【部门列表文件夹】
cd $mainWorkingDir
$departmentDir = Get-ChildItem $mainWorkingDir -Directory -Exclude "Complete","Error","PS","finish","temp_271","temp_306","temp_190","temp_380","temp_383","Packinglist 271"
foreach($department in $departmentDir){
#判断当前文件夹是【单个单号】还是【多个单号】
$orderDir = dir $department -Directory
if($orderDir.Count -eq 0){
#单个单号
Resolve-SingleOrderDir -WorkingDir $department
RenameFile -WorkingDir $department
}
else{
#多个单号
$orderDir | foreach {
Resolve-MutitpleOrderDir -WorkingDir $_.FullName
RenameFolder -WorkingDir $_.FullName
}
}
}
#Get-ChildItem -Path 'D:\1' -Recurse -ErrorAction SilentlyContinue -Filter *.xls* |Rename-Item -NewName ($_.Parent.Name + " - " + $_.BaseName) | Move-Item -Destination "E:\Done" -Force
#Get-ChildItem -Path 'D:\1' -Recurse -ErrorAction SilentlyContinue -Filter *.doc* | Move-Item -Destination "E:\Done"
#Get-ChildItem -Path 'D:\1' -Recurse -ErrorAction SilentlyContinue -Filter *.jpg | Move-Item -Destination "E:\Done"
#Get-ChildItem -Path 'D:\1' -Recurse -ErrorAction SilentlyContinue -Filter *.png | Move-Item -Destination "E:\Done"
#Get-ChildItem -Path 'D:\1' -Recurse -ErrorAction SilentlyContinue -Filter *.gif | Move-Item -Destination "E:\Done"
#Get-ChildItem -Path 'D:\1' -Recurse -ErrorAction SilentlyContinue -Filter *.bmp | Move-Item -Destination "E:\Done"
先谢谢! |
|