简体   繁体   中英

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

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.

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

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.

Example: outputfile.txt file contains the names below:

renametoA
renametoB
renametoC
renametoD
renametoE
renametoF

There are 6 text files inside the "extractedFiles1" folder which you can see in the image. 在此处输入图片说明

The script actually renames the files in the "extractedFiles1" folder according to the names from the output.txt file. 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:

在此处输入图片说明

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.

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

  1. Open cmd with administrator privileges.

  2. Create a list of your current (old) file names:

    cd H:\dev\extractedFiles1

    dir > input.txt

  3. Open the input.txt (eg in Notepad). It should look like this:

Volume in drive H is Windows

Volume Serial Number is C123-4567

Directory of H:\dev\extractedFiles1

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,952 Filename2.txt

...

  1. Copy the lines with filenames and timestamps into an Excel sheet
  2. Use Data > Text to columns to split the filenames from the time stamps
  3. Copy or import your target/new filenames from output.txt next to the old filenames
  4. Make a new column with the formula = "ren"&" "&A1&" "&B1 resulting in something like

ren Filename1.txt FilenameA.txt

  1. Copy all formulas and paste them in cmd. Make sure you are in the right directory.

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

Instead of point 6. above do the following:

  1. Make a new column with the formula = "MOVE"&" "&A1&" "&"H:\dev\extractedFiles1\Renamed"&B1

  2. Copy the created command into cmd

It will move and rename the files according to the names in B1.

This example make a copy and rename file to a list csv

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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