-1

I'm writing a cmd file that will loop through all the directories in a given directory and generate swagger code for each file in each directory. I cannot get the correct syntax for the 'for' loops.

cd C:\Users\Sora Teichman\Documents\APIJsonModels\models
for /D %%A in ("C:\Users\Sora Teichman\Documents\APIJsonModels\models\*") do (
    set fileFolder=%cd%
    echo %fileFolder%
    for /F %%G in (fileFolder) do (
        set filePath=%f
        echo %filePath%
        java -jar .\swagger-codegen-cli.jar generate -i filePath -l csharp-dotnet2 -o fileFolder
    )
)

I'm getting 'in was unexpected at this time' at the outer for loop. I tried using a variable for the folder path, I tried with the asterisk and without, I tried ('dir "C:\Users\Sora Teichman\Documents\APIJsonModels\models"'). The ss64 does not provide enough detail for my scenario. What is the correct syntax for this? I know that I need double %% for the parameters because this is in a .cmd file, there are many question online for this issue :). For context, I am running this command file from a .csproj file using an <Exec Command="codeGenerator.cmd"/> tag.

Edit: @Stephan pointed out that I needed to set my variables correctly, but now I need help with the inner loop. When I use the fileFolder variable, the error I get is 'The system cannot find the file C:\Users\Sora.' It is getting stuck on the space in the folder path.

  • Please read [about using variables in a loop](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Aug 18 '22 at 09:36
  • What is `%f` supposed to be? – aschipfl Aug 18 '22 at 09:44
  • @aschipfl %f I need to get the current file path with the name into a variable; I haven't figured that out yet – Sora Teichman Aug 18 '22 at 09:52
  • @Stephan, thanks for pointing me in the right direction! Will add my answer when I figure this out fully. – Sora Teichman Aug 18 '22 at 10:12
  • That's because of the space, which splits your string into two. Use quotes. As best practice use them with every folder/file name. Your `java` line should probably be `java -jar ".\swagger-codegen-cli.jar" generate -i "!filePath!" -l csharp-dotnet2 -o "!fileFolder!"` – Stephan Aug 18 '22 at 11:21
  • `%f`: If I read your comment correctly, read the output of `for /?`, especially the "modifiers" section and use `%%~dpG` instead. – Stephan Aug 19 '22 at 17:40

1 Answers1

0

I figured it out on my own thanks to @Stephan's comment. Here's the working code:

set jsonFoldersPath="C:\Users\Sora Teichman\Documents\APIJsonModels\models\*"
@echo off
setlocal enabledelayedexpansion
FOR /D %%f in (%jsonFoldersPath%) do (
set fileFolder=%%f
cd !fileFolder!
for %%a in (*) do (
set fileName=%%a
set filePath=!fileFolder!\!fileName!
cd C:\Users\Sora Teichman\Documents\AmazonAPIGenerator
java -jar .\swagger-codegen-cli.jar generate -i "!filePath!" -l csharp-dotnet2 -o "C:\Users\Sora Teichman\Documents\AmazonAPIGenerator\AmazonGeneratedModels" -c "C:\Users\Sora Teichman\Documents\AmazonAPIGenerator\config.json"
)
)

You need to enable delayed expansion for variables using setlocal enabledelayedexpansion so that they only evaluate at execution in the loop; read an explanation here: How do SETLOCAL and ENABLEDELAYEDEXPANSION work?

I did not end up using a variable for the -o output directory parameters for Swagger because Swagger organizes the code on its own; it generates API, Client and Model namespaces which I customized using a config.json file and the -c parameter. I also plan to replace the hardcoded paths with relative ones.

  • No need for variables (as you don't do substring processing) - you can just use `%%f` and `%%a` directly: `java -jar .\swagger-codegen-cli.jar generate -i "%%f\%%a" -l ...` – Stephan Aug 21 '22 at 09:11