0

Directory structure C:Test\client1,C:\Test\client2....

Here I need to execute _Test.cmd files at time for specific clients only.

Please let me know how to filter the client names in this for loop using Batch script?

@Echo Off
setlocal enableDelayedExpansion
for /r C:\Install %%x in ("*_Test.cmd") do (
 ECHO [ !DATE! !TIME! ] Executing %%x ...
 CD %%~dx%%~px
 CALL _Test.cmd
 IF ERRORLEVEL 0 (
  ECHO [ !DATE! !TIME! ] Successful!
 )
)

1 Answer 1

1

No idea how to distinguish "client name" from "not a client name".

If you mean "directory names" where each client has a directory, then:

@Echo Off
setlocal enableDelayedExpansion
pushd
for /d /r C:\test %%x in (.) do (
 ECHO [ !DATE! !TIME! ] Executing %%x ...
 CD %%~dpx
 CALL _Test.cmd
 IF NOT ERRORLEVEL 1 (
  ECHO [ !DATE! !TIME! ] Successful!
 )
)
popd

Notes: pushd/popd bracket saves the current directory and restores after for loop.

for /d /r means directories, recursive

Target "." means "all" - if you have a mask to apply, put that mask here.

cd %%~dpx is simply shorter than your version.

if errorlevel 0 is always true under normal circumstances. if errorlevel n means "if errorlevel is n or greater than n". cmd can be forced to set errorlevel negative, but it's a contrived situation.


" i need to run _Test.cmd for only particular clients like client24,client15,client40."

Now if you'd said that to start off with....

@Echo Off
setlocal enabledelayedexpansion
pushd
for /f "delims=" %%x in (yourfileofclients.txt) do if exist "c:\test\%%~x\." (
 ECHO [ !DATE! !TIME! ] Executing %%x ...
 CD "c:\test\%%~x"
 CALL _Test.cmd
 IF NOT ERRORLEVEL 1 (
  ECHO [ !DATE! !TIME! ] Successful!
 )
)
popd
Sign up to request clarification or add additional context in comments.

3 Comments

I mean, I have clients folders in the directory like client1,client2,....client60. But i need to run _Test.cmd for only particular clients like client24,client15,client40...
Thanks for your information. But one more issue Iam facing i.e.,
Thanks for your information. It is working fine for one module of application. But I have multiple modules which contains _Test.cmd batch files. I need to execute multiple batch files for specific clients and that to be for multiple modules. But one more issue Iam facing i.e., for /f "delims=" %%x in (yourfileofclients.txt) ->it is for specific clients and for /r C:\test %%x in ("*_regsvc.cmd") -> i need to use this for multiple modules. Iam unable to make two conditions at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.