How to batch rename files in a directory from a list of names in a text file

廉价感情. 提交于 2019-12-11 23:25:07

问题


I'd like to use Power Shell or a batch file to rename several files in a folder based on a list in a text file. Essentially I want to append the file names with the author's last names (which I have stored in a separate text file).

E.g. Currently I have:

C:\myfiles

9-ART-2013.pdf
4-EGO-2013.pdf
2-ART-2013.pdf

My text file (in same order as files):

C:\myfiles

_Smith
_Jenkins
_McMaster

I want the files to be renamed as follows:

9-ART-2013_Smith.pdf
4-EGO-2013_Jenkins.pdf
2-ART-2013_McMaster.pdf

I've seen similar problems where people want to recursively rename files but they are always using a generic common appending element like adding an underscore or pre-pending with folder name, etc.

e.g. https://serverfault.com/questions/6268/easy-way-to-rename-all-files-in-a-directory-in-windows


回答1:


In PowerShell it would be:

$names = Get-Content c\myfiles
Get-ChildItem C:\somedir\*.pdf | Sort -desc | 
    Foreach {$i=0} {Rename-Item $_ ($_.basename + $names[$i++] + $_.extension) -WhatIf}

If it looks like it will copy correctly, remove the -WhatIf.




回答2:


Another way to achieve the same thing:

@echo off
setlocal EnableDelayedExpansion

rem Load the list of authors:
set i=0
for /F %%a in (myfiles.txt) do (
   set /A i+=1
   set "author[!i!]=%%a"
)

rem Do the rename:
set i=0
for /F %%a in ('dir /b *.pdf') do (
   set /A i+=1
   for %%i in (!i!) do ren "%%a" "%%~Na!author[%%i]!%%~Xa"
)

EDIT: New method added

If the list of names file have the "OLD-Name NEW-Name" structure, then the code is much simpler:

for /F "tokens=1,2" %%a in (myfiles.txt) do ren "%%a" "%%b"

Note that the names must be separated by a space.




回答3:


@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<Text.txt (
for /f "tokens=1*delims=_" %%a in ('dir /b /a-d /o-d *-*-*.pdf') do if "%%b"=="" (
    set "xand="
    set /p "xand="
    echo ren "%%~a" "%%~na!xand!%%~xa"
))



回答4:


Taking Endoro's and simplifying:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<texts.txt (
for /f "tokens=* delims=* " %%a in ('dir /b /a-d /o-d *-*-*.pdf') do (
set "xand="
set /p "xand="
ren "%%~a" "%%~na!xand!%%~xa"
))



回答5:


Any of the above are likely workable solutions with just a little tweaking but the simplest thing to do was to simply create one text file with each row containing "the current filename"... a TAB... then "the filename I wanted".

9-ART-2013.pdf     9-ART-2013_Smith.pdf
4-EGO-2013.pdf     4-EGO-2013_Jenkins.pdf
2-ART-2013.pdf     2-ART-2013_McMaster.pdf

Then save the file as rename_list.txt and create a batch file with the following code.

for /F "tokens=1,2" %%a in (rename_list.txt) do ren "%%a" "%%b"
pause

You can delete the pause line once you get it tweaked and running correctly. Just copy the rename_list.txt and batch files to the folder with the files you want to rename and run the batch file. If you have a large folder with many files names you can get them in a text file by running a batch file with the following line.

dir /D > filename_scrapper_output.txt

It will create a text file filename_scrapper_output.txt that you can use to start your rename_list.txt file for above.



来源:https://stackoverflow.com/questions/20060429/how-to-batch-rename-files-in-a-directory-from-a-list-of-names-in-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!