

InterviewSolution
Saved Bookmarks
1. |
Solve : Replacing characters in a large amount of folders? |
Answer» <html><body><p>Hi,<br/><br/>I'm having troubles renaming folders<br/><br/>a colleague has a large amount of folders that contain # and she wants to replace these characters to _<br/><br/>123#12#12raw --> 123_12_12raw<br/><br/>What I have done today as a <a href="https://interviewquestions.tuteehub.com/tag/quick-1174961" style="font-weight:bold;" target="_blank" title="Click to know more about QUICK">QUICK</a> fix was list all the folders in a txt<br/>dir /b *#* > print.txt<br/><br/>then I opened this txt in excel and generated <a href="https://interviewquestions.tuteehub.com/tag/lines-240113" style="font-weight:bold;" target="_blank" title="Click to know more about LINES">LINES</a> for each folder and pasted these in a *.bat<br/><br/>move 123#12#12raw 123_12_12raw<br/>move 123#12#13raw 123_12_13raw<br/>.....<br/><br/>This worked perfectly but it is not very user friendly<br/><br/>Is there a way to use the move command with wildcards?<br/><br/>I tried <br/>move *#*#* *_*_*<br/>but this (probably obvious) doesn't work.<br/><br/>If I try to find the files like that it does work<br/>dir /b *#*#*<br/><br/>I spent the last 2 hours googeling but didn't find a decent solution yet<br/><br/>I also tried to do something with<br/>for %1 in (*#*) do move %1 xxxxxx<br/>but I can't figure out how to alter the variable so that # gets replaced with _ in %1 <br/><br/>Anyone with suggestions?<br/><br/>Thanks,<br/><br/>JupkeGot an answer onan other (car)forum <br/><br/>Wanted to share it here as well<br/><br/> Code: <a>[Select]</a>C:\Users\Roel\test>FOR /F "tokens=1,2,3 delims=#" %A IN ('dir /b *#*') DO move %<br/>A#%B#%C %A_%B_%C<br/><br/>C:\Users\Roel\test>move 2123#12#14 2123_12_14<br/>Er zijn 1 map(pen) verplaatst.<br/><br/>C:\Users\Roel\test>move 22123#12#13 22123_12_13<br/>Er zijn 1 map(pen) verplaatst.<br/><br/>C:\Users\Roel\test>what's all that weird euro language stuff?<br/> Quote from: Salmon Trout on September 15, 2010, 01:03:22 PM</p><blockquote>what's all that weird euro language stuff?<br/><br/></blockquote> <br/>I 'm sorry, I forgot to paste only the relevant part, edited the code now<br/><br/>"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<br/><br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/>for /f "delims=" %%A in ('dir /b /ad *#*') do (<br/> set oldname=%%A<br/> set newname=!oldname:#=_!<br/> echo renaming "!oldname!" to "!newname!"<br/> REN "!oldname!" "!newname!"<br/> )<br/> <br/>To do all subdirectories change dir /b /ad to dir /s /b /ad<br/><br/>Before...<br/><br/> Code: <a>[Select]</a>C:\test>dir<br/> Volume in drive S is USBHD<br/> Volume Serial Number is 2C51-AA7F<br/><br/> Directory of C:\Test<br/>15/09/2010 08:45 PM <DIR> .<br/>15/09/2010 08:45 PM <DIR> ..<br/>15/09/2010 08:44 PM <DIR> folder#1<br/>15/09/2010 08:44 PM <DIR> folder#2<br/>15/09/2010 08:44 PM <DIR> folder#3<br/>15/09/2010 08:44 PM <DIR> folder#<a href="https://interviewquestions.tuteehub.com/tag/4-238406" style="font-weight:bold;" target="_blank" title="Click to know more about 4">4</a><br/>15/09/2010 08:45 PM <DIR> folder#4#5#6#####7<br/>15/09/2010 08:44 PM 215 test1.bat<br/> 1 File(s) 217 bytes<br/> 7 Dir(s) 188,506,218,496 bytes free<br/><br/> Code: <a>[Select]</a>c:\Test\>test1.bat<br/>renaming "folder#1" to "folder_1"<br/>renaming "folder#2" to "folder_2"<br/>renaming "folder#3" to "folder_3"<br/>renaming "folder#4" to "folder_4"<br/>renaming "folder#4#5#6#####7" to "folder_4_5_6_____7"<br/><br/>After...<br/><br/> Code: <a>[Select]</a>C:\Test>dir<br/> Volume in drive S is USBHD<br/> Volume Serial Number is 2C51-AA7F<br/><br/> Directory of C:\Test<br/><br/>15/09/2010 08:48 PM <DIR> .<br/>15/09/2010 08:48 PM <DIR> ..<br/>15/09/2010 08:44 PM <DIR> folder_1<br/>15/09/2010 08:44 PM <DIR> folder_2<br/>15/09/2010 08:44 PM <DIR> folder_3<br/>15/09/2010 08:44 PM <DIR> folder_4<br/>15/09/2010 08:45 PM <DIR> folder_4_5_6_____7<br/>15/09/2010 08:48 PM 217 test1.bat<br/> 1 File(s) 217 bytes<br/> 7 Dir(s) 188,506,218,496 bytes free<br/>Indeed, this also works, but the effect is in this case exactly the sale with move as rename here?<br/><br/>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<blockquote>Indeed, this also works, but the effect is in this case exactly the sale with move as rename here?</blockquote> <br/>Yes, however move has a potential ambiguity that ren does not, viz: <br/><br/>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 <a href="https://interviewquestions.tuteehub.com/tag/moves-239392" style="font-weight:bold;" target="_blank" title="Click to know more about MOVES">MOVES</a>.<br/><br/> Quote<blockquote>I'm not sure in win7 but I thought rename didn't work for folder in XP?</blockquote> <br/>1. Where did you get that idea?<br/>2. Could you not have done an experiment?<br/>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.<br/><br/> Code: <a>[Select]</a>G:\test>ver<br/><br/>Microsoft Windows XP [Version 5.1.2600]<br/><br/>G:\test>md testdir<br/><br/>G:\test>dir<br/> Volume in drive G is DATA1<br/> Volume Serial Number is 9476-662D<br/><br/> Directory of G:\test<br/><br/>15/09/2010 22:42 <DIR> ..<br/>15/09/2010 22:42 <DIR> testdir<br/>15/09/2010 22:42 <DIR> .<br/> 0 File(s) 0 bytes<br/> 3 Dir(s) 16,695,369,728 bytes free<br/><br/>G:\test>ren testdir cakes-and-coffee<br/><br/>G:\test>dir<br/> Volume in drive G is DATA1<br/> Volume Serial Number is 9476-662D<br/><br/> Directory of G:\test<br/><br/>15/09/2010 22:42 <DIR> ..<br/>15/09/2010 22:42 <DIR> cakes-and-coffee<br/>15/09/2010 22:42 <DIR> .<br/> 0 File(s) 0 bytes<br/> 3 Dir(s) 16,695,369,728 bytes free<br/><br/>G:\test></body></html> | |