-2

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"
Mofi
  • 46,139
  • 17
  • 80
  • 143
JoeStack
  • 1
  • 2
  • 1
    I just can't understand what do you mean by a DOS and not a cmd batch file (is it really a command batch file what you want?), and I believe this is a mission for awk. – jarnosz Jun 16 '16 at 04:17
  • What operating system are you using? – Ross Ridge Jun 16 '16 at 04:18
  • @erreka - JoeStack is not trying to manipulate either text file. He is trying to use the content of the two files to rename files that exist is some directory. – dbenham Jun 16 '16 at 11:55
  • I admire your tenacity on this question, but the resulting stream-of-consciousness may be rather too localised to be of help to anyone else. It appears that the body of this question has been used for extended conversation as well, which does not lend itself well to the Q&A format we like here. It is much better to ask a specific question, with a clear problem statement, and then let someone answer it in the answer space below. Clarification and discussion can happen in the comments. – halfer Jun 26 '16 at 20:30
  • (I didn't have the patience to remove the break tags - I find these are _never_ needed in questions, and one of their major disadvantages is that they show up as text if a block of data is changed to preformatted, as has happened here). – halfer Jun 26 '16 at 20:31

1 Answers1

3

Renaming a file is trivial, and reading from a single file is simple with FOR /F. The only tricky bit is reading the corresponding line from a second file, which is the same issue when merging two text files. The most efficient solution is to redirect stdin to the 2nd file for an entire block of code. You can then use SET /P to read successive lines

The following script will generally work, but note that no attempt is made to detect or handle name collisions that could result from a rename operation.

@echo off
setloacal disableDelayedExpansion
pushd "d:\Librarian"
<d:\Test\MyList2.txt (
  for /f "usebackq eol=: delims=" %%F in ("d:\Test\MyList1.txt") do (
    set "from=%%F"
    setlocal enableDelayedExpansion
    set "to="       %= Clear any existing value =%
    set /p "to="    %= Read the next line =%
    if defined to ren "!from!" "!to!"
    endlocal
  )
)
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • See what I add in my question. Your batch work except for 2 files because they use the character "-" (without ""). Thanks a lot for your great work! – JoeStack Jun 16 '16 at 14:52
  • @JoeStack - There are multiple forms of dashes. The dash in the file must match the dash in the actual file name. Also, Windows cmd.exe (batch files) has limited support for Unicode characters. In order for the batch file to work, your code page (selected ANSI character set) must support all the characters in the file names. – dbenham Jun 16 '16 at 15:34
  • Ok all work perfectly now. What I did is rename all files in examples (MyList1, MyList2, d:\Librarian) and do that with DOS (under Windows 10). This time all WORK! By the way you are a Genius ! Thanks a lot! – JoeStack Jun 16 '16 at 17:31