简体   繁体   English

使用 powershell 从文本列表重命名文件

[英]Renaming files from a text list using powershell

I have seen many different postings here on how to approach this task.我在这里看到了许多关于如何处理此任务的不同帖子。 I have tried them all with minor variations of them to no avail.我已经尝试了所有这些方法,但都做了一些细微的改动,但都无济于事。 Every time I run the powershell code, I still get the same error message: "cannot rename because the file does not exist..."每次运行 powershell 代码,我仍然得到相同的错误消息:“无法重命名,因为文件不存在...”

I just simply want to rename a bunch of files in one folder from the list of filenames I have in a text file.我只是想从文本文件中的文件名列表中重命名一个文件夹中的一堆文件。

Here is one version of code:这是一个版本的代码:

$inputFile1=@()
$filesTochange=@()
$inputFile1 = get-content H:\dev\outputfile.txt
$filesToChange = Get-ChildItem H:\dev\extractedFiles1  | Foreach -Begin 
{$i=0}
-process {Rename-Item $filesToChange[$i] -newName ($inputFile1[$i] -f $i++) 
} 

Here is another version:这是另一个版本:

$inputFile1=@()    
$filesTochange=@()
$inputFile1 = get-content H:\dev\outputfile.txt             
$filesToChange = Get-ChildItem H:\dev\extractedFiles1 
$i=0
foreach ($fil in $filesToChange) {Rename-Item $fil -NewName 
$inputFile1[$i++]}  

Not entirely sure what's your setup or desired output is but give this a whirl bud.不完全确定您的设置或所需的输出是什么,但请试一试。 Not the most elegant looking solutions but hopefully this is what you are looking for?不是最优雅的解决方案,但希望这就是您正在寻找的? Do be mindful with the sorting of the filenames in your outputfile.txt and how the folders are listed when you get the childitem.请注意 outputfile.txt 中文件名的排序以及获取 childitem 时文件夹的列出方式。

$BasePath = 'C:\Test'
$FilesToChangeFolderName = 'extractedFiles1'
$filestochange = Get-ChildItem -Path "$BasePath\$FilesToChangeFolderName"

$FileNames = Get-Content "$BasePath\outputfile.txt"

if($filestochange.FullName.Count -eq $FileNames.Count)
{
    for($i=0; $i -lt $FileNames.Count; $i++)
    {
        write-host "Renaming file $($filestochange.Name[$i]) to $($FileNames[$i]+".txt")"
        Rename-Item -Path $filestochange.FullName[$i] -NewName ($FileNames[$i]+".txt")
    }
}

Setup - outputfile.txt contains:设置 - outputfile.txt 包含:

renametoA
renametoB
renametoC
renametoD
renametoE
renametoF

Folder structure:文件夹结构:

在此处输入图片说明

Results:结果:

Renaming file renameto1.txt to renametoA.txt
Renaming file renameto2.txt to renametoB.txt
Renaming file renameto3.txt to renametoC.txt
Renaming file renameto4.txt to renametoD.txt
Renaming file renameto5.txt to renametoE.txt
Renaming file renameto6.txt to renametoF.txt

Explanation [TLDR]: The script below takes a text file input which contains the name that should be used to rename each text file you have.说明 [TLDR]:下面的脚本采用文本文件输入,其中包含用于重命名您拥有的每个文本文件的名称。

Example: outputfile.txt file contains the names below:示例: outputfile.txt文件包含以下名称:

renametoA
renametoB
renametoC
renametoD
renametoE
renametoF

There are 6 text files inside the "extractedFiles1" folder which you can see in the image.您可以在图像中看到“extractedFiles1”文件夹中有 6 个文本文件。 在此处输入图片说明

The script actually renames the files in the "extractedFiles1" folder according to the names from the output.txt file.该脚本实际上根据 output.txt 文件中的名称重命名“extractedFiles1”文件夹中的文件。 Thus it follows this logic:因此它遵循这个逻辑:

Renaming file renameto1.txt to renametoA.txt
Renaming file renameto2.txt to renametoB.txt
Renaming file renameto3.txt to renametoC.txt
Renaming file renameto4.txt to renametoD.txt
Renaming file renameto5.txt to renametoE.txt
Renaming file renameto6.txt to renametoF.txt

So after all the script runs your "extractedFiles1" folder's gonna look something like this:因此,在所有脚本运行后,您的“extractedFiles1”文件夹将如下所示:

在此处输入图片说明

Despite this being an older thread, I wanted to offer another "brute force" option.尽管这是一个较旧的线程,但我想提供另一个“蛮力”选项。 CodeNagi's reply works well in PowerShell, although it took me a bit to get working. CodeNagi 的回复在 PowerShell 中运行良好,尽管我花了一些时间才开始工作。

If you already have a list with file names (output.txt) consider using excel (or OpenOffice) and the command prompt cmd. This video is a step by step guide for some of this solution: https://youtu.be/znhqGrF4gVQ如果您已经有了包含文件名 (output.txt) 的列表,请考虑使用 excel(或 OpenOffice)和命令提示符 cmd。此视频是部分解决方案的分步指南: https://youtu.be/znhqGrF4gVQ

  1. Open cmd with administrator privileges.以管理员权限打开cmd。

  2. Create a list of your current (old) file names:创建当前(旧)文件名列表:

    cd H:\dev\extractedFiles1 cd H:\dev\extractedFiles1

    dir > input.txt目录 > 输入.txt

  3. Open the input.txt (eg in Notepad).打开 input.txt(例如在记事本中)。 It should look like this:它应该是这样的:

Volume in drive H is Windows驱动器 H 中的卷是 Windows

Volume Serial Number is C123-4567卷序列号是 C123-4567

Directory of H:\dev\extractedFiles1 H:\dev\extractedFiles1目录

05/12/2022 11.24. 2022 年 5 月 12 日 11 月 24 日。

05/12/2022 11.24.. 05/12/2022 11.24..

05/12/2022 09.34 5,255,248 Filename1.txt 05/12/2022 09.34 5,255,248 文件名 1.txt

05/12/2022 09.34 5,255,952 Filename2.txt 05/12/2022 09.34 5,255,952 文件名2.txt

... ...

  1. Copy the lines with filenames and timestamps into an Excel sheet将带有文件名和时间戳的行复制到 Excel 工作表中
  2. Use Data > Text to columns to split the filenames from the time stamps使用Data > Text to columns从时间戳中拆分文件名
  3. Copy or import your target/new filenames from output.txt next to the old filenames从旧文件名旁边的 output.txt 复制或导入您的目标/新文件名
  4. Make a new column with the formula = "ren"&" "&A1&" "&B1 resulting in something like使用公式 = "ren"&" "&A1&" "&B1 创建一个新列,结果类似于

ren Filename1.txt FilenameA.txt ren 文件名1.txt 文件名A.txt

  1. Copy all formulas and paste them in cmd. Make sure you are in the right directory.复制所有公式并将它们粘贴到 cmd 中。确保您位于正确的目录中。

Notes: If you have spaces in the file names, you will need to wrap each file name first in apostrophes ". Since the concatenation formula in excel doesn't accept """ (triple apostropes), make yourself a column with only " (here C) and then refer to it in the concatenation: = "ren "&C1&A1&C1&" "&C1&B1&C1&. Further, if you have duplicate files or want to make sure they are copied correclty, you can use the MOVE function instead of rename (ren).注意:如果文件名中有空格,则需要先将每个文件名用撇号 " 包裹起来。由于 excel 中的连接公式不接受 """(三个撇号),因此将自己设为仅包含 " (这里 C) 然后在连接中引用它:= "ren "&C1&A1&C1&" "&C1&B1&C1&。此外,如果您有重复文件或想确保它们被正确复制,您可以使用 MOVE function 而不是重命名 (ren) .

Instead of point 6. above do the following:代替上面的第 6 点,执行以下操作:

  1. Make a new column with the formula = "MOVE"&" "&A1&" "&"H:\dev\extractedFiles1\Renamed"&B1使用公式 = "MOVE"&" "&A1&" "&"H:\dev\extractedFiles1\Renamed"&B1 创建一个新列

  2. Copy the created command into cmd将创建的命令复制到 cmd

It will move and rename the files according to the names in B1.它将根据 B1 中的名称移动和重命名文件。

This example make a copy and rename file to a list csv此示例将文件复制并重命名为列表 csv

Import-CSV LISTA.csv -Header newFileName |导入-CSV LISTA.csv -Header newFileName | % { Copy-Item -Path archivo_convert.pdf -Destination "$($_.newfilename).pdf" } % { Copy-Item -Path archivo_convert.pdf -Destination "$($_.newfilename).pdf" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM