1.

Solve : Replacing characters in a large amount of folders?

Answer»

Hi,

I'm having troubles renaming folders

a colleague has a large amount of folders that contain # and she wants to replace these characters to _

123#12#12raw --> 123_12_12raw

What I have done today as a QUICK fix was list all the folders in a txt
dir /b *#* > print.txt

then I opened this txt in excel and generated LINES for each folder and pasted these in a *.bat

move 123#12#12raw 123_12_12raw
move 123#12#13raw 123_12_13raw
.....

This worked perfectly but it is not very user friendly

Is there a way to use the move command with wildcards?

I tried
move *#*#* *_*_*
but this (probably obvious) doesn't work.

If I try to find the files like that it does work
dir /b *#*#*

I spent the last 2 hours googeling but didn't find a decent solution yet

I also tried to do something with
for %1 in (*#*) do move %1 xxxxxx
but I can't figure out how to alter the variable so that # gets replaced with _ in %1

Anyone with suggestions?

Thanks,

JupkeGot an answer onan other (car)forum 

Wanted to share it here as well

Code: [Select]C:\Users\Roel\test>FOR /F "tokens=1,2,3 delims=#" %A IN ('dir /b *#*') DO move %
A#%B#%C %A_%B_%C

C:\Users\Roel\test>move 2123#12#14 2123_12_14
Er zijn         1 map(pen) verplaatst.

C:\Users\Roel\test>move 22123#12#13 22123_12_13
Er zijn         1 map(pen) verplaatst.

C:\Users\Roel\test>what's all that weird euro language stuff?
Quote from: Salmon Trout on September 15, 2010, 01:03:22 PM

what's all that weird euro language stuff?


I 'm sorry, I forgot to paste only the relevant part, edited the code now

"Er zijn 1 map(pen) verplaatst." means 1 folder has been moved.An alternative, maybe: if you want to take folder names containing # and replace that character with _ why bother using move? Just get the folder name into a string variable, use the replace function of the SET command, and rename the folder with the new string. E.g

Code: [Select]echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /ad *#*') do (
set oldname=%%A
set newname=!oldname:#=_!
echo renaming "!oldname!" to "!newname!"
REN "!oldname!" "!newname!"
)

To do all subdirectories change dir /b /ad to dir /s /b /ad

Before...

Code: [Select]C:\test>dir
 Volume in drive S is USBHD
 Volume Serial Number is 2C51-AA7F

 Directory of C:\Test
15/09/2010  08:45 PM    <DIR>          .
15/09/2010  08:45 PM    <DIR>          ..
15/09/2010  08:44 PM    <DIR>          folder#1
15/09/2010  08:44 PM    <DIR>          folder#2
15/09/2010  08:44 PM    <DIR>          folder#3
15/09/2010  08:44 PM    <DIR>          folder#4
15/09/2010  08:45 PM    <DIR>          folder#4#5#6#####7
15/09/2010  08:44 PM               215 test1.bat
               1 File(s)            217 bytes
               7 Dir(s)  188,506,218,496 bytes free

Code: [Select]c:\Test\>test1.bat
renaming "folder#1" to "folder_1"
renaming "folder#2" to "folder_2"
renaming "folder#3" to "folder_3"
renaming "folder#4" to "folder_4"
renaming "folder#4#5#6#####7" to "folder_4_5_6_____7"

After...

Code: [Select]C:\Test>dir
 Volume in drive S is USBHD
 Volume Serial Number is 2C51-AA7F

 Directory of C:\Test

15/09/2010  08:48 PM    <DIR>          .
15/09/2010  08:48 PM    <DIR>          ..
15/09/2010  08:44 PM    <DIR>          folder_1
15/09/2010  08:44 PM    <DIR>          folder_2
15/09/2010  08:44 PM    <DIR>          folder_3
15/09/2010  08:44 PM    <DIR>          folder_4
15/09/2010  08:45 PM    <DIR>          folder_4_5_6_____7
15/09/2010  08:48 PM               217 test1.bat
               1 File(s)            217 bytes
               7 Dir(s)  188,506,218,496 bytes free
Indeed, this also works, but the effect is in this case exactly the sale with move as rename here?

I'm not sure in win7 but I thought rename didn't work for folder in XP? Quote from: Jupke on September 15, 2010, 02:18:59 PM
Indeed, this also works, but the effect is in this case exactly the sale with move as rename here?

Yes, however move has a potential ambiguity that ren does not, viz:

move folder1 folder2 (if folder2 does not exist!) is equivalent to ren folder1 folder2; BUT if folder2 does exist, folder1 can be moved to be a subfolder of folder2, unless its attributes forbid this . However, ren only renames, never MOVES.

Quote
I'm not sure in win7 but I thought rename didn't work for folder in XP?

1. Where did you get that idea?
2. Could you not have done an experiment?
3. It works for all Windows, as far as I know, as long as you have the right permissions and the folder attributes allow it.

Code: [Select]G:\test>ver

Microsoft Windows XP [Version 5.1.2600]

G:\test>md testdir

G:\test>dir
 Volume in drive G is DATA1
 Volume Serial Number is 9476-662D

 Directory of G:\test

15/09/2010  22:42    <DIR>          ..
15/09/2010  22:42    <DIR>          testdir
15/09/2010  22:42    <DIR>          .
               0 File(s)              0 bytes
               3 Dir(s)  16,695,369,728 bytes free

G:\test>ren testdir cakes-and-coffee

G:\test>dir
 Volume in drive G is DATA1
 Volume Serial Number is 9476-662D

 Directory of G:\test

15/09/2010  22:42    <DIR>          ..
15/09/2010  22:42    <DIR>          cakes-and-coffee
15/09/2010  22:42    <DIR>          .
               0 File(s)              0 bytes
               3 Dir(s)  16,695,369,728 bytes free

G:\test>


Discussion

No Comment Found