views:

235

answers:

2

Not sure what the appropriate tags are here...

A while back I created a batch script which, when run will convert all .xsd files found in C:\api\ into C# classes using the xsd.exe file found in the Microsoft Windows SDK (using v6.1 here).

@ECHO OFF
CLS
ECHO ***
ECHO Runs xsd.exe on all *.xsd files sorted by filename in the current folder.
ECHO ***

FOR /R "C:\api" %%G IN (*.xsd) DO (
  @ECHO ON
  "C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\xsd.exe" "C:\api\base\base.xsd" "%%G" /c /n:Mynamespace /o:"C:\api"
  @ECHO OFF
)

The problem is this... I have 50 or so generated .cs files, including the base class. And inside every single one, it generates a copy of the base class, so I end up with the base class in every class, when I only need it in one.

Is there a way of preventing the class being generated without the base class in each one? I still want the base class to be created (base.cs), but just not in the other 49 classes.

Edit:

I have tried the following:

@ECHO OFF
SET str1=
FOR /R "C:\api" %%G IN (*.xsd) DO (
  SET str1="%%G" %str1%
)

ECHO %str1%

The response returned is always the last file in the list.

+2  A: 

You can pass multiple XML schema files to xsd.exe at once instead of looping through the files and passing each file separately. The list of schema files being passed would not be dynamic though...

Also this creates only one .cs file, named after the first XML schema file in the list, containing all classes.

Vinz
Thanks for your response! I've been looking at string concatenation in batch script, but have failed miserably (it only returns the last item in the loop). Any ideas?
Dan Atkinson
Here is an example of how to concatenate strings: http://www.dostips.com/DtTipsStringOperations.php#Snippets.StringConcatenationBut I guess it is not possible to pass multiple paths to xsd.exe as one string. A solution might be to compose the whole xsd.exe-call as string and then evaluate it, but I don't know if it's possible to execute a string in a batchfile. A pretty ill solution might be to write the whole composed command to another batchfile and execute it...
Vinz
I was looking at this page as a reference, but all I ever got was the last file. I'll update my answer with an explanation of what I tried.
Dan Atkinson
You're right Vinz. I have redirected to another batch file and it works perfectly. Thanks!
Dan Atkinson
+2  A: 

For your second problem, you have to set delayed expansion.

And use !str! instead of %str%

give this a try

SETLOCAL enabledelayedexpansion
@ECHO OFF
SET str1=
FOR /R "C:\api" %%G IN (*.xsd) DO (
  SET str1="%%G" !str1!
)
ECHO !str1!
PA
Wow, string concatenation works! Windows shell scripting is soo cryptic to me..... :(
Vinz
Thanks! I've tried `ECHO "C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\xsd.exe" "C:\api\base\base.xsd" !str1! /c /MyNamespace /o:"C:\api"` but it returns nothing at all. However, if I just echo `!str1!`, then it's returned. Any ideas?
Dan Atkinson
make sure you have added SETLOCAL ENABLEDELAYEDEXPANSION
PA
I think the problem is that you are passing multiple file-paths to xsd.exe in one string. Try to compose the whole xsd command as string, redirect the composed command to another batchfile an execute it.
Vinz