0

I have this script thanks to foxidrive Link to post

@echo on
setlocal enabledelayedexpansion
set num=0
cd /d "C:\testfolder\test\test"  

if /i "%a:~-7%"=="Part" goto:process else goto:next

:process
for /f "delims=" %%a in ('dir /b /ad /o-d ') do (
set /a num+=1
set name=000!num!
set name=!name:~-3!
ren "%%a" "%%a - Part !name!"
)
pause

:next
pause 

Before

Folder 1

After

Folder 1 - Part 001

If I execute the script again

Folder 1 - Part 001 - Part 001

Solution

if /i "%a:~-7%"=="Part" goto:process else goto:next

I would like the above code to look for the workd "Part". If it is found anywhere, it would GOTO:NEXT. I am not sure what I am missing here as I have tried it with not avail and I have looked around for some time now. Thanks in advanced.

Community
  • 1
  • 1
Arthor
  • 666
  • 2
  • 13
  • 40
  • In the script you've posted, you appear to be testing the value of `%a` before you've set it. Or did I miss something? – icabod Jul 17 '14 at 16:27
  • @icabod Yes you are right, but I am green to this to be honest. So it would be? `if /i ":~-7%"=="Part" goto:process else goto:next"` ? Thanks – Arthor Jul 17 '14 at 16:30
  • Don't have time to write a full answer (as I'm about to head home for the day), but no - you would need to check for `%a` containing `Part`, perhaps by searching it for a [substring](http://stackoverflow.com/q/7005951/218597). – icabod Jul 17 '14 at 16:32

2 Answers2

1

This small batch file demonstrates how to identify subdirectories with string Part anywhere within directory name.

@echo off
setlocal enabledelayedexpansion
cd /d "C:\testfolder\test\test"

for /f "delims=" %%a in ('dir /b /ad /o-d ') do (
   set "DirName=%%a"
   if "!DirName:Part=!" == "!DirName!" (
      echo Directory "%%a" does not contain string "Part".
   ) else (
      echo Directory "%%a" contains string "Part".
   )
)
endlocal
echo.
pause

!DirName:Part=! creates a string from value of environment variable DirName where the string Part is replaced by an empty string. This string is compared with unmodified value of environment variable DirName. If both strings are identical, the directory name does not contain Part.

Replace the 2 echo commands with whatever you need for your task.

This second example code searches in directory C:\testfolder\test\test for any subdirectory containing the string Part in directory name in order from newest to oldest directory. The loop is exit immediately on first subdirectory with Part in name found. The environment variable DirName contains in this case the name of this subdirectory.

@echo off
cls
setlocal enabledelayedexpansion
cd /d "C:\testfolder\test\test"

for /f "delims=" %%a in ('dir /b /ad /o-d ') do (
   set "DirName=%%a"
   if not "!DirName:Part=!" == "!DirName!" goto PartDirFound
)

DirName=
endlocal
echo No subdirectory contains "Part" in name.
echo.
pause
rem Exit this batch file as nothing do.
goto :EOF

:PartDirFound
rem Insert your code here instead of commands echo and pause.
echo "!DirName!" contains the string "Part" in name.
echo.
pause
DirName=
endlocal

But match faster would be following batch with same execution behavior:

@echo off
cd /d "C:\testfolder\test\test"

for /f "delims=" %%a in ('dir *part* /b /ad /o-d ') do (
   set "DirName=%%a"
   goto PartDirFound
)

cls
echo No subdirectory contains "Part" in name.
echo.
pause
rem Exit this batch file as nothing do.
goto :EOF

:PartDirFound
rem Insert your code here instead of commands echo and pause.
echo "%DirName%" contains the string "Part" in name.
echo.
pause
DirName=
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • OK but from what I understand this is a loop right. So even if the last folder was named with `PART` inside it. It would not go out of the loop until it go to the folder. `@echo off setlocal enabledelayedexpansion cd /d "C:\testfolder\test\test" for /f "delims=" %%a in ('dir /b /ad /o-d ') do ( set "DirName=%%a" if "!DirName:Part=!" == "!DirName!" ( GOTO:PROCESS ) else ( GOTO:NEXT ) ) pause :NEXT echo next pause :PROCESS echo PROCESS pause ` – Arthor Jul 17 '14 at 17:00
  • I would like all the folders to be checked to see if they contain the word `PART` If any folder contains the word `PART` `GOTO:NEXT` If all the folders does not contain the word `PART` `GOTO:PROCESS`. So all the folder would have to be checked in advanced before any `GOTO` can be executed, I hope it is clear. – Arthor Jul 17 '14 at 17:17
0

The solution thanks to Mofi

@echo off
setlocal enabledelayedexpansion
set num=0

cd /d "C:\test\test"

for /f "delims=" %%a in ('dir *part* /b /ad /o-d ') do (
   set "DirName=%%a"
   goto PartDirFound
)

cls
for /f "delims=" %%a in ('dir /b /ad /o-d ') do (
set /a num+=1
set name=000!num!
set name=!name:~-3!
ren "%%a" "%%a - Part !name!"
)
pause
rem Exit this batch file as nothing do.
goto :EOF

:PartDirFound
rem Insert your code here instead of commands echo and pause.
echo "%DirName%" contains the string "Part" in name.
echo.
pause
DirName=
Arthor
  • 666
  • 2
  • 13
  • 40