seek folder in FOR + actions on it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
svgirl
Posts: 1
Joined: 26 Nov 2014 02:36

seek folder in FOR + actions on it

#1 Post by svgirl » 26 Nov 2014 03:50

Hello !

I try to do something simple but I don't suceed.

I would like seek a folder with a particular name (example : TOTO) and try to display the folder and rename it.
My command line doesn't works :

Code: Select all

FOR /D /R %I IN (dir /s TOTO) DO echo %I & rename %i\TOTO %I\TATA



FOR /D /R : Seek for folder recursively
dir /s TOTO : Display all the folders whicj contain the string "TOTO".
echo %I & rename %i\TOTO %I\TATA : Display the name of the folder which contain the string "TOTO" et rename "TOTO" to "TATA"

That doesn't works..
Anyone can help me ?

Thanks a lot !
Svgirl
(my english is not very good sorry)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: seek folder in FOR + actions on it

#2 Post by foxidrive » 26 Nov 2014 13:00

svgirl wrote:My command line doesn't works :

Code: Select all

FOR /D /R %I IN (dir /s TOTO) DO echo %I & rename %i\TOTO %I\TATA



You have several flaws.

This is incorrect - dir /s TOTO. You just need TOTO* but it will also match TOTOM for example, without some extra logic.

You are using %i but the for metavariable name is case sensitive.

Your rename command is not correct. Put echo before rename and see what it prints to the console, once you fix the other errors. It also needs quotes to handle spaces etc.

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: seek folder in FOR + actions on it

#3 Post by aGerman » 26 Nov 2014 13:05

DIR /S already searches recursively. If you want to process the output of the DIR command you have to use a FOR /F loop.
untested:

Code: Select all

for /f "delims=" %I in ('dir /ad /b /s TOTO^|findstr /e "TOTO"') do (echo "%I"& rename "%I" "TATA")

If you want to use it in a Batch file you have to double the percent signs.

Regards
aGerman

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: seek folder in FOR + actions on it

#4 Post by Yury » 26 Nov 2014 14:50

foxidrive wrote:This is incorrect - dir /s TOTO. You just need TOTO* but it will also match TOTOM for example, without some extra logic.
+
aGerman wrote:

Code: Select all

for /f "delims=" %I in ('dir /ad /b /s TOTO^|findstr /e "TOTO"') do (echo "%I"& rename "%I" "TATA")
=

Code: Select all

cd /d "C:\Folder"& for /f "delims=" %I in ('dir /ad /b /s TOTO*^|findstr /e "TOTO"') do (echo "%I"& rename "%I" "TATA")


But not in the case of "TOTO\TOTO\TOTO". For this case:

Code: Select all

cd /d "C:\Folder"& for /f "delims=" %I in ('dir /ad/b/s "TOTO*"^| findstr /elc:"\TOTO"^| sort /r') do @echo %I& rename "%I" "TATA"
.

Post Reply