0

We want to create a batch script that runs a command for each of our .xml files. The command should run for every file that does NOT end with _m. We try to go through each file with forfiles, but we dont know how to use do togheter with it.

This is our current not working script, where we try to go through every file. Identify the filename, and substring it to not run the command if it ends with _m.

forfiles /p /source /m *.xml do (

set "filename=@fname"
set "extension=_m.xml"
set "extended=%filename%extension"


IF NOT %filename:~-2%=="_m"(
    ssoadm import-entity --cot something--realm something--spec saml2 -m @file -x %extended --adminid something--password-file password --debug
)

Thanks for any help!

1
  • 1
    Just use the built-in for command instead of the external forfiles command, then you'll be able to use do. Commented Mar 9, 2017 at 10:27

1 Answer 1

1
for /f "delims=" %%a in (
 'dir /b /a-d *.xml^|findstr /v /e /i /L "_m.xml" '
) do (
   ssoadm import-entity --cot something--realm something--spec saml2 -m "%%a" -x %extended --adminid something--password-file password --debug
)

for each entry in a dir in /b basic form (names only) /a-d without directorynames; finding names which /v do not /e end /i case-insensitive /L literally "-m.xml", assign the complete name found to %%a

No idea what ssoadm does, but

%%~na will yield the basename of the file, and %%~xa the extension.

Your code might work, but you'd need to invoke delayed expansion (search SO) and modify the code to suit delayedexpansion methodology (as documented many, many times on SO)


for /f "delims=" %%a in (
 'dir /b /a-d *.xml^|findstr /v /e /i /L "_m.xml" '
) do (
   echo(%%~na
   echo(%%~xa
   echo(%%~nxa
   echo(%%a
   echo(%%a %%~na_M%%~xa
)

...pick the parts/constants as you will and put them together meccano-style.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. I still dont get it quite how I want. Say if I have four files: file1.xml, file1_m.xml, file2.xml and file2_m.xml and I need to use the pairs in each of the commands. How do I do that? This is the commands I want to run: somecommand -x file1.xml -m file1_m.xml and somecommand -x file2.xml -m file2_m.xml

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.