将文件名从一个文件夹复制到另一个文件夹,同时保留原始扩展名 [英] Copy file names from one folder to another while keeping the original extensions

查看:243
本文介绍了将文件名从一个文件夹复制到另一个文件夹,同时保留原始扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关将文件名称(不是文件本身)从C驱动器复制到D驱动器的帮助.我能够在线找到以下powershell代码:

I need help with copying file names (not the files themselves) from C drive to D drive. I was able to find the following powershell code online:

$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.

This code successfully renames/copies all file names from C:\MyFiles to D:\MyFiles, strictly by corresponding positions (indices in the enumeration).

但是,它也在更新扩展名,例如:

However, it is also updating the extensions, so for instance:

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

是否有一种方法可以编辑Powershell代码,使其仅重命名文件名的 base (例如myFile),同时保留目标文件的扩展名(例如,.docx)?

Is there a way to edit the Powershell code to only rename the base of the filename (e.g., myFile) while keeping the target files' extensions (e.g., .docx)?

C:\MyFiles\myfile.txt使用

推荐答案

听起来您想根据目标目录中的相应文件重定位目标位置 中的文件源目录-保留目标目录文件的扩展名:

It sounds like you want to rename files in a target directory positionally, based on corresponding files in a source directory - while retaining the target directory files' extensions:

$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调用.

    • To preview the resulting filenames, append -WhatIf to to the Rename-Item call.

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

      The .BaseName property of the [System.IO.FileInfo] objects output by Get-ChildItem returns the file-name portion without the extension.

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

      $_.Extension extracts the input file's (i.e., the target file's) existing extension, including the leading .

      请注意,传递给Rename-Item的脚本块({ ... })创建一个 child 变量作用域,因此您不能在调用方作用域中直接增加变量(它将每次使用原始值创建一个此类变量的新副本);因此,将创建一个[ref]实例以间接 保留该数字,然后可以通过.Value属性通过该子作用域对其进行修改.

      Note that the script block ({ ... }) passed to Rename-Item creates a child variable scope, so you cannot increment a variable in the caller's scope directly (it would create a new copy of such a variable with the original value every time); therefore, a [ref] instance is created to indirectly hold the number, which the child scope can then modify via the .Value property.

      这是一个完整示例:

      注意:虽然本示例使用相似的文件名和统一扩展名,但代码通常以任何名称和扩展名运行 .

      Note: While this example uses similar file names and uniform extensions, the code works generically, with any names and extensions.

      # 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).

      Note how the target files now have the source files' base file names (s-file*), but the target files' original extensions (.target-ext).

      这篇关于将文件名从一个文件夹复制到另一个文件夹,同时保留原始扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆