1

In a batch I have a

for /d /r %%a in (*) do 

I want to do an action if %%a contains " - " (ie white space + hyphen + white space) and another action if it doesn't contain this string.

I cannot make it work! Do you have any idea? Thanks.

[PS : My question is similar to this one : Batch file: Find if substring is in string (not in a file) but I'm unable to adapt it to make it work!]

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673
  • With `for /d /r %%a in ("* - *")`, how to get the part before " - " (as %%b for example) and the part after " - " as %%c ? – Basj Mar 28 '13 at 21:45

2 Answers2

1

Please try this:

for /d /r %%a in ("* - *") do 

it should work.

Edit: the for /d /r loop does only find folders with "-" in the name, so additional tests for the path aren't necessary.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • thanks! how to make a second loop With folders not containing " - " then ? – Basj Mar 28 '13 at 19:37
  • You can try this: `for /d /r %%a in (*.*) do set "pname=%%~nxa"&if "%pname: - =%"=="%%~nxa" call ECHO %pname%` – Endoro Mar 28 '13 at 19:57
  • thanks! last question : how do I get the **folder name** only of %%a? example : if `%%a = "d:\blablabla\myfolder12"` => how to get "myfolder12"? – Basj Mar 28 '13 at 20:11
  • 1
    try this (from root): `for /f "delims=" %%i in ('dir /ad /s /b "myfolder12"') do set "folder=%%~i"` `echo %folder%` – Endoro Mar 28 '13 at 21:06
  • thanks again! if I do `for /d /r %%a in ("* - *")`, how to get the part before " - " (as `%%b` for example) and the part after " - " (as `%%c` for example) ? – Basj Mar 28 '13 at 21:46
  • 1
    Thats a bit more tricky, one example: `@echo off &setlocal` `set "fname=bla bla - biz baz"` `set "right=%fname:* - =%"` `call set "left=%%fname: - %right%=%%"` `echo [%left%] * [%right%]` – Endoro Mar 28 '13 at 22:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27132/discussion-between-jos-bas-and-endoro) – Basj Mar 28 '13 at 22:35
  • 1
    Sorry but my time is over, is midnight here ... :) – Endoro Mar 28 '13 at 22:58
  • I don't understand why this doesn't work : `for /d %%a in (*) do (` `set "fname=%%a"` `set "right=%fname:* - =%"` `call set "left=%%fname: - %right%=%%"` `echo [%left%] * [%right%] )` – Basj Mar 28 '13 at 23:04
  • 1
    Inside a code block, eg. a `for` loop, you need an other syntax: `@echo off &setlocal´ `for /d %%a in (*) do (´ ` set "fname=%%~a"´ ` call set "right=%%fname:* - =%%"´ ` call set "left=%%fname: - %right%=%%"´ ` call echo [%%left%%] * [%%right%%]´ `)´ – Endoro Mar 28 '13 at 23:18
1
@ECHO OFF
SETLOCAL
FOR /d /r %%a IN (*) DO (
 ECHO "%%~nxa"|FIND " - " >NUL
 IF ERRORLEVEL 1 (ECHO DO action ONE ON "%%a"
 ) ELSE (ECHO action TWO gets done ON "%%a")
)

FIND is used to locate "-" in the "leaf" directory, so only if the lowest level contains a "-" will action two be performed, else action 1.

If you want it so that a "-" occurring ANYWHERE in the filepath makes action two take place, then change %%~nxa to %%a

Magoo
  • 77,302
  • 8
  • 62
  • 84