【问题标题】:Copy file names from one folder to another while keeping the original extensions将文件名从一个文件夹复制到另一个文件夹,同时保留原始扩展名
【发布时间】:2018-09-28 02:20:51
【问题描述】:

我需要帮助我将文件名称(不是文件本身)从 C 盘复制到 D 盘。我能够在网上找到以下 powershell 代码:

$names = @()
$getPath = "C:\MyFiles"
$setPath = "D:\MyFiles"
Get-ChildItem $getPath |
    Foreach-object{
    $names += $_
}
$i = 0
Get-ChildItem $setPath |
Foreach-object{
    Rename-Item -Path $_.FullName -NewName $names[$i]
    $i++
}

此代码成功地将所有文件名从C:\MyFiles 重命名/复制为D:\MyFiles,严格按照相应的位置(枚举中的索引)。

不过,它也在更新扩展,例如:

C:\MyFiles\myfile.txtD:\MyFiles\thisfile.docx 重命名为 D:\MyFiles\myfile.txt

有没有办法编辑 Powershell 代码以仅重命名文件名的 base(例如,myFile)同时保留目标文件的 扩展名(例如, .docx)?

这样C:\MyFiles\myfile.txtD:\MyFiles\thisfile.docx 重命名为D:\MyFiles\myfile.docx,使用

【问题讨论】:

    标签: windows file powershell renaming


    【解决方案1】:

    听起来您想重命名目标目录中的文件位置,基于源目录中的相应文件 - 同时保留目标目录文件的扩展名:

    $getPath = "C:\MyFiles"
    $setPath = "D:\MyFiles"
    $sourceFiles = Get-ChildItem -File $getPath
    
    $iRef = [ref] 0
    Get-ChildItem -File $setPath | 
      Rename-Item -NewName { $sourceFiles[$iRef.Value++].BaseName + $_.Extension }
    
    • 预览生成的文件名,请将-WhatIf 附加到Rename-Item 调用。

    • Get-ChildItem 输出的[System.IO.FileInfo] 对象的.BaseName 属性返回不带扩展名的文件名部分。

    • $_.Extension 提取输入文件(即目标文件)的现有扩展名,包括前导 .

    • 1234563 em>(每次都会使用原始值创建此类变量的新副本);因此,将创建一个 [ref] 实例以间接保存该数字,然后子作用域可以通过 .Value 属性对其进行修改。

    这是一个完整示例

    注意:虽然此示例使用相似的文件名和统一的扩展名,但代码一般工作,具有任何名和扩展名。

    # Determine the temporary paths.
    $getPath = Join-Path ([System.IO.Path]::GetTempPath()) ('Get' + $PID)
    $setPath = Join-Path ([System.IO.Path]::GetTempPath())  ('Set' + $PID)
    
    # Create the temp. directories.
    $null = New-Item -ItemType Directory -Force $getPath, $setPath
    
    # Fill the directories with files.
    
    # Source files: "s-file{n}.source-ext"
    "--- Source files:"
    1..3 | % { New-Item -ItemType File (Join-Path $getPath ('s-file{0}.source-ext' -f $_)) } | 
      Select -Expand Name
    
    # Target files: "t-file{n}.target-ext"
    "
    ---- Target files:"
    1..3 | % { New-Item -ItemType File (Join-Path $setPath ('t-file{0}.target-ext' -f $_)) } | 
      Select -Expand Name
    
    # Get all source names.
    $sourceFiles = Get-ChildItem -File $getPath
    
    # Perform the renaming, using the source file names, but keeping the
    # target files' extensions.
    $i = 0; $iVar = Get-Variable -Name i
    Get-ChildItem -File $setPath | 
      Rename-Item -NewName { $sourceFiles[$iVar.Value++].BaseName + $_.Extension }
    
    "
    ---- Target files AFTER RENAMING:"
    
    Get-ChildItem -Name $setPath
    
    # Clean up.
    Remove-Item -Recurse $getPath, $setPath
    

    以上产出:

    --- Source files:
    s-file1.source-ext
    s-file2.source-ext
    s-file3.source-ext
    
    ---- Target files:
    t-file1.target-ext
    t-file2.target-ext
    t-file3.target-ext
    
    ---- Target files AFTER RENAMING:
    s-file1.target-ext
    s-file2.target-ext
    s-file3.target-ext
    

    注意目标文件现在如何具有源文件的基本文件名 (s-file*),但目标文件的原始扩展名 (.target-ext)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2011-08-22
      相关资源
      最近更新 更多