【问题标题】:Rename files in Powershell with a reference file使用参考文件重命名 Powershell 中的文件
【发布时间】:2018-03-24 21:17:29
【问题描述】:

抱歉之前的困惑...

我今天花了几个小时试图编写一个 powershell 脚本,该脚本将从系统 #1 中提取客户端 ID(例如,Smith,John_H123_20171012.pdf,其中客户端 ID 是 H### # 值),然后在包含系统 1 和系统 2 中的客户端 ID 的 Excel 电子表格中查找,然后将文件重命名为系统 2 所需的格式(xxx_0000000123_yyy.pdf)。

一个问题是客户端编号在系统 2 中是 2-4 位数字,并且总是以 0 开头。

使用 Powershell 和正则表达式。

这是我尝试用于初始重命名的第一部分:

    Get-ChildItem -Filter *.pdf | Foreach-Object{
        $pattern = "_H(.*?)_2"
        $OrionID = [regex]::Match($file, $pattern).Groups[1].value
        Rename-Item -NewName $OrionID
    }

它不接受“NewName”,因为它声明它是一个空字符串。我跑了:

    Get-Variable | select name,value,Description

新名称显示为名称但没有任何价值。如何将正则表达式的输出传递到重命名中?

【问题讨论】:

  • 如果您可以将 excel 保存为 csv,那么在 powershell 中处理 csv 会容易得多
  • 不幸的是,stackoverflow 不是免费的代码生成服务。您将需要首先努力解决问题,而不是仅仅要求其他人为您编写代码。
  • $files = Get-ChildItem -Path C:\PowerShell\Test foreach ($file in $files) { $pattern = "_H(.*?)_2" $OrionID = [regex]: :Match($file, $pattern).Groups[1].value #$_ -replace "(.*)(\.pdf)",$OrionID Write-host $OrionID Rename-Item -Path $_.FullName -新名称 $OrionID }
  • 感谢@Jimbo,这是一个有用的建议。
  • 我最终使用了一个名为“批量重命名实用程序”和 Excel 的实用程序。我可以通过 BRU 运行各种重命名正则表达式,并在一些 Excel 格式化后添加参考 .txt 文件。

标签: regex excel powershell pdf rename


【解决方案1】:

在调试器中逐行运行这段代码,你就会明白它是如何工作的。

#Starts an Excel process, you can see Excel.exe as background process
$processExcel = New-Object -com Excel.Application
#If you set it to $False you wont see whats going on on Excel App
$processExcel.visible = $True 

$filePath="C:\somePath\file.xls"
#Open $filePath file
$Workbook=$processExcel.Workbooks.Open($filePath)

#Select sheet 1
$sheet = $Workbook.Worksheets.Item(1) 
#Select sheet with name "Name of some sheet"
$sheetTwo = $Workbook.Worksheets.Item("Name of some sheet")

#This will store C1 text on the variable
$cellString = $sheet.cells.item(3,1).text
#This will set A4 with variable value
$sheet.cells.item(1,4) = $cellString

#Iterate through all the sheet
$lastUsedRow = $sheet.UsedRange.Rows.count
$LastUsedColumn = $sheet.UsedRange.Columns.count
for ($i = 1;$i -le $lastUsedRow; $i++){
    for ($j = 1;$j -le $LastUsedColumn; $j++){
        $otherString = $sheet.cells.item($i,$j).text
    }
}

#Create new Workbook and add sheet to it
$newWorkBook = $processExcel.Workbooks.Add()
$newWorkBook.worksheets.add()
$newSheet = $newWorkBook.worksheets.item(1)
$newSheet.name="SomeName"

#Close the workbook, if you set $False it wont save any changes, same as close without save
$Workbook.close($True)
#$Workbook.SaveAs("C:\newPath\newFile.xls",56) #You can save as the sheet, 56 is format code, check it o internet
$newWorkBook.close($False)

#Closes Excel app
$processExcel.Quit()

#This code is to remove the Excel process from the OS, this does not always work.
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($processExcel)
Remove-Variable processExcel

【讨论】:

    【解决方案2】:

    我最终使用了一个名为“批量重命名实用程序”的实用程序和 Excel。我可以通过 BRU 运行各种重命名正则表达式,并在一些 Excel 格式化后添加参考 .txt 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 2011-07-31
      • 2021-08-27
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多