2

I want to write a batch script to rename folders in a directory.

The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.

So if I have 20 folders, 20 names would exist in file to rename each folder.

What I have so far:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt" 
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
   ren Read the next name from the redirected input file
   SET /P newname=
   ren "%%f" "!newname!" 
))

The above script didn't give me the desired result.

Dejan Skledar
  • 11,280
  • 7
  • 44
  • 70
Ribo01
  • 59
  • 1
  • 7
  • the `set /p` method for reading files line by line does not work good with FOR loops – npocmaka May 04 '15 at 20:51
  • @npocmaka: The `set /P` command placed inside a FOR loop is the _simplest way_ to merge two files. See [this example](http://stackoverflow.com/questions/20099623/merge-2-csv-files-as-it-is/20103830#20103830), or [this one](http://stackoverflow.com/questions/14521799/combinining-multiple-text-files-into-one/14523100#14523100), or several more... You may even merge one file via FOR with _more than one input files_ via `set /p`'s with this method, like in [this example](http://stackoverflow.com/questions/24910000/how-to-merge-files-horizontally-in-windows-7-command-prompt/24987950#24987950), or.. – Aacini May 06 '15 at 00:28

2 Answers2

0

Not tested:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0 
(for /f "tokens=*" %%f in ('dir /b %old%') do (
   ren Read the next name from the redirected input file
   set /a counter=counter+1
   for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b" 
   ren "%%f" "!newname!" 
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

The problem is that the dir /b %old% command generate a list of files with .txt extension. If you want to rename folders, then include /AD switch and eliminate the wild-card:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt" 
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
   ren Read the next name from the redirected input file
   SET /P newname=
   ren "%%f" "!newname!" 
))
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • can this script be simplified. its seems not to be working. what can seem to be the problem? is there something i need to take out or add? – Ribo01 May 05 '15 at 10:27
  • The script is simple enough already. You may insert an `echo` command before the `ren` one (`echo ren "%%f" "!newname!"`) so you may see the executed commands. Also, be sure that `dir /b /AD` command and the contents of the file are correct. If this not help to identify the problem, remove the `@echo off` line, execute the script from the command-line window (_not_ from the explorer) so you may review exactly what is executed... – Aacini May 05 '15 at 16:28