Renaming a list of files (in one folder) contained in a text file like MyList1.txt
by another list of files contained in another text file like MyList2.txt
.
I would like to use a Windows command batch file (not PowerShell, script, etc.) which renames a list of files in one folder, contained in a text file, by another list of files contained in another text file.
Suppose I have a list of files inside a text file. These files are in one folder, for example D:\Librarian
File D:\Test\MyList1.txt contains:
Directory.zip
Subject.zip
Penny.zip
Car.zip
Auto.zip
One_Upon_A_time.zip
All is relative.zip
Zen of graphics programming – Corilolis.zip
PC Magazine – July 1999 No.22.zip
Bytes is over with quantum.zip
And I want to replace them by the file names listed in D:\Test\MyList2.txt containing:
Patatoes.zip
Chips.zip
Hot Dogs Sweet.zip
Ice Cream_Very – Good.zip
We must eat to live.zip
Ketchup on potatoes is a muxt.zip
The Magazine are owned by few guys.zip
Desert are not necessary_in life.zip
Sugar_is_a_legal_drug.zip
People-who_don’t-like_pizzas_don’t like bread.zip
So
Directory.zip
will become Patatoes.zip
Subject.zip
will become Chips.zip
Penny.zip
will become Hot Dogs Sweet.zip
etc.
Both MyList1.txt
and MyList2.txt
have same number of lines (same number of file names).
OS is: Windows 10.
Must use batch (cmd)
One folder - D:\Librarian
have files to be renamed.
Another folder have the two files MyList1.txt
and MyList2.txt
MyList1.txt
contains the list of files from D:\Librarian
MyList2.txt
contains the new name of files for D:\Librarian
------Begin 1: dbenham----------------------------------------------------------
I have tried what you wrote and it's working except for 2 file names.
Probably Windows command interpreter doesn't accept some punctuation in these file names.
Here what appear on command prompt (DOS under Windows 10):
(I remove @echo off
of course to be able to see that.)
D:\>setlocal disableDelayedExpansion
D:\>pushd "d:\Librarian"
d:\Librarian>(for /F "usebackq eol=: delims=" %F in ("d:\Test\MyList1.txt") do (
set "from=%F"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
) ) 0 (<) d:\Test\MyList2.txt
It's not (<) but only < but for x reason 0< is not visible.
d:\Librarian>(
set "from=Directory.zip "
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
d:\Librarian>(
set "from=Subject.zip"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
...
d:\Librarian>(
set "from=Zen of graphics programming Corilolis.zip"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
The system cannot find the file specified.
d:\Librarian>(
set "from=PC Magazine July 1999 No.22.zip""
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
The system cannot find the file specified.
d:\Librarian>(
set "from=Bytes is over with quantum.zip"
setlocal enableDelayedExpansion
set "to="
set /p "to="
if defined to ren "!from!" "!to!"
endlocal
)
Windows command interpreter doesn't like -
character probably in the following file names:
PC Magazine - July 1999 No.22.zip
Zen of graphics programming - Corilolis.zip
But since a lot of my files have the hyphen character, I would like an advice to get the batch file working also for such file names.
Later... After to use CMD to rename all files (inside MyList1.txt
and MyList2.txt
and in the folder D:\Librarian
) all work good.
I think I copy and paste files from this page and so not same characters for all even if -
can appear to be -
like you say they can be different on console or on web page.
I can't say I understand all what you did (so bright)!
For example why do:
pushd "d:\Librarian"<br>
(<)d:\Test\MyList2.txt
(No () around the <)
I find Windows commands are just extraordinary. Well more to see what people, like you, can do so with so few commands. Just unbelievable!
------End 1: dbenham----------------------------------------------------------
Hope to not bore you ...
Can I do the same if my files are in folder and subfolders?
Suppose my files are distributed in the following folders:
D:\Librarian\
D:\Librarian\S1\
D:\Librarian\Plane\
MyList1.txt
D:\Librarian\Directory.zip
D:\Librarian\Subject.zip
D:\Librarian\Penny.zip
D:\Librarian\S1\Car.zip
D:\Librarian\S1\Auto.zip
D:\Librarian\S1\One_Upon_A_time.zip
D:\Librarian\S1\All is relative.zip
D:\Librarian\Plane\Zen of graphics programming - Corilolis.zip
D:\Librarian\Plane\PC Magazine - July 1999 No.22.zip
D:\Librarian\Plane\Bytes is over with quantum.zip
MyList2.txt
D:\Librarian\Patatoes.zip
D:\Librarian\Chips.zip
D:\Librarian\Hot Dogs Sweet.zip
D:\Librarian\S1\Ice Cream_Very - Good.zip
D:\Librarian\S1\We must eat to live.zip
D:\Librarian\S1\Ketchup on potatoes is a must.zip
D:\Librarian\S1\The Magazine are owned by few guys.zip
D:\Librarian\Plane\Desert are not necessary_in life.zip
D:\Librarian\Plane\Sugar_is_a_legal_drug.zip
D:\Librarian\Plane\People-who_don't-like_pizzas_don't like bread.zip
What I would like is for example:
ren "D:\Librarian\Directory.zip" "D:\Librarian\Patatoes.zip"
ren "D:\Librarian\Plane\Zen of graphics programming - Corilolis.zip" "D:\Librarian\Plane\People-who_don't-like_pizzas_don't like bread.zip"