【问题标题】:Add spaces, hyphens and move text in filename using PowerShell使用 PowerShell 在文件名中添加空格、连字符和移动文本
【发布时间】:2023-01-27 20:41:46
【问题描述】:

我正在尝试使用 Powershell 重命名文件。我真的不确定哪种方法最适合完成此任务。这些文件由 POS 系统以如下所示的格式生成和命名。我想让它们在文件夹中易于阅读和分类,以便数据录入员稍后处理。这是我到目前为止所拥有的。我认为最好的方法是正则表达式,但现在我不确定。任何帮助将不胜感激。

示例:(日期时间报告名称)

StoreClose20230122220648 ---> 2023-01-22 220648 Store Close
TillSummeryClose20230122220648 ----> 2023-01-22 220648 Till Summery Close

尝试使用正则表达式但没有成功。

for ($i = 0; $i -lt $files.Count; $i++) {
    $BaseName = $files[$i].BaseName
    $FileExt = $files[$i].Extension
    $NewName = ($BaseName -csplit '([0-9]+)' -ne '' -join '-').Trim("")
    Write-Output (-join($NewName, $FileExt))
}
$SRC = "$env:USERPROFILE\Desktop\From Here"
$DST = "$env:USERPROFILE\Desktop\To Here"
$EXT = "*.pdf", "*.log", "*.txt"

$SRC_EXST = Test-Path -Path $SRC -ErrorAction SilentlyContinue
$DST_EXST = Test-Path -Path $DST -ErrorAction SilentlyContinue
$CWD = $PSScriptRoot

if (!$SRC_EXST) {
    Write-Output "$(Get-Date) Invalid source(SRC) path [$SRC]." | Out-File -FilePath "$CWD\log.txt" -Encoding utf8 -Append
    exit
}

if (!$DST_EXST) {
    Write-Output "$(Get-Date) Invalid destination(DST) path [$DST]." | Out-File -FilePath "$CWD\log.txt" -Encoding utf8 -Append
    exit
}

$files = Get-ChildItem -Path "$SRC\*" -Include $EXT

for ($i = 0; $i -lt $files.Count; $i++) {
    $BaseName = $files[$i].BaseName
    $FileExt = $files[$i].Extension
    $NewName = ($BaseName -csplit '([0-9]+)' -ne '' -join '-').Trim("")
    Write-Output (-join($NewName, $FileExt))
}

【问题讨论】:

    标签: regex powershell


    【解决方案1】:

    要将这些名称拆分为包含大写单词之间的空格的名称部分和日期部分,您需要做更多的工作。

    Helas,在您的问题中,您不清楚您是否只想重命名这些文件或将它们复制到新名称下的新位置。 下面的代码显示了如何做到这一点:

    $SRC = "$env:USERPROFILEDesktopFrom Here"
    $DST = "$env:USERPROFILEDesktopTo Here"
    $EXT = "*.pdf", "*.log", "*.txt"
    $LOG = Join-Path -Path $PSScriptRoot -ChildPath 'log.txt'
    
    if (-not (Test-Path -Path $SRC -PathType Container)) {
        "$(Get-Date) Invalid source(SRC) path [$SRC]." | Add-Content -Path $LOG -Encoding utf8
        exit
    }
    if (-not (Test-Path -Path $DST -PathType Container)) {
        "$(Get-Date) Invalid destination(DST) path [$DST]." | Add-Content -Path $LOG -Encoding utf8
        exit
    }
    
    # loop thhrough the files
    Get-ChildItem -Path $SRC -File -Recurse -Include $EXT | 
    Where-Object { $_.BaseName -match 'd{14}$' } |     # should end in 14 digits
    ForEach-Object {
        $name = $_.BaseName -replace 'd{14}$' 
        $time = $_.BaseName -replace "^$name"
        $date = [datetime]::ParseExact($time, 'yyyyMMddHHmmss', $null)
        # insert spaces in front of each Capital letter in the name (make sure only one single space)
        $name = ($name -creplace '([A-Z])', ' $1').Trim() -replace 's+', ' '
        # recombine these parts to create the new file name
        $newName = '{0:yyyy-MM-dd HHmmss} {1}{2}' -f $date, $name, $_.Extension
        # if you want to RENAME the file in the source directory:
        $_ | Rename-Item -NewName $newName -WhatIf
    
        # if you want to COPY the file with a new name to the destination path leaving the source file as-is:
        # $_ | Copy-Item -Destination (Join-Path -Path $DST -ChildPath $newName) -WhatIf
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多