1

I have a batch script for the purpose of setting up a dev environment for a Django instance. Part of this is setting the DJANGO_SETTINGS_MODULE environment variable. If no arguments are supplied the script runs this block of code:

for %%* in (.) do set DIR=%%~n*
set DJANGO_SETTINGS_MODULE=%DIR%.dev_settings

When the script is first called it sets DJANGO_SETTINGS_MODULE to only .dev_settings. When I run the script a second time in the same terminal it sets it correctly from the directory name.

I have no idea why this is the case.

Below is the full script in case you need full context.

@echo off
IF "%1"=="-r" (
    set "DJANGO_SETTINGS_MODULE="
) ELSE IF "%1"=="" (
    start cmd /c prp-sql
    for %%* in (.) do set DIR=%%~n*
    set DJANGO_SETTINGS_MODULE=%DIR%.dev_settings
) ELSE IF "%2"=="" (
    start cmd /c prp-sql
    set DJANGO_SETTINGS_MODULE=%1.dev_settings
) ELSE (
    start cmd /c prp-sql
    set DJANGO_SETTINGS_MODULE=%1.%2
)
Sam Tuson
  • 37
  • 6
  • When you set a variable inside a parenthesis `()` block you will need to call it in order to see the results or have used `enabledelayedexpansion` on a `setlocal` command and used `!` instead of `%` to expand thw variable. I will post the amended code as the answer – Ben Personick Dec 09 '19 at 12:40
  • 1
    Classic [delayed expansion problem](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). But you don't necessarily need delayed expansion. Just do `for %%* in (.) do set "DJANGO_SETTINGS_MODULE=%%~n*.dev_settings"` – Stephan Dec 09 '19 at 13:41

1 Answers1

1

When you set a variable inside a parenthesis () block you will need to call it in order to see the results or have used enabledelayedexpansion on a setlocal command and used ! instead of % to expand thw variable.

Below is the amended code

@(
  SETLOCAL ENABLEDELAYEDEXPANSION
   echo off
)

IF /I "%1"  EQU "-r" (
    set "DJANGO_SETTINGS_MODULE="
   Goto :EOF
) 
start cmd /c prp-sql
IF /I "%2" NEQ "" (
  set "DJANGO_SETTINGS_MODULE=%1.%2"
) ELSE (
  IF /I "%1" NEQ "" (
    set "DJANGO_SETTINGS_MODULE=%1.dev_settings"
  ) ELSE (
    for %%_ in (.) do set "DIR=%%~n_"
    set "DJANGO_SETTINGS_MODULE=!DIR!.dev_settings"
  )
)

Alternatively if you don't need to re-use DIR later you can just set the Django settings variable directly in the loop:

for %%_ in (.) do (
    set "DJANGO_SETTINGS_MODULE=%%~n_.dev_settings"
  )

Below is that version of the amended code

@(
  SETLOCAL
   echo off
)

IF /I "%1"  EQU "-r" (
    set "DJANGO_SETTINGS_MODULE="
   Goto :EOF
) 
start cmd /c prp-sql
IF /I "%2" NEQ "" (
  set "DJANGO_SETTINGS_MODULE=%1.%2"
) ELSE (
  IF /I "%1" NEQ "" (
    set "DJANGO_SETTINGS_MODULE=%1.dev_settings"
  ) ELSE (
    for %%_ in (.) do set "DJANGO_SETTINGS_MODULE=%%~n_.dev_settings"
  )
)

Notes

One final Q, are you sure you want to start start cmd /c prp-sql before setting django?

Whether before or after, put that part in only once after determining it should run (is not -r) and either before or after the if structure to set the Django variable.

I changed the script to just stop aftwr hitting -r since nother after would normally execute.

Ben Personick
  • 3,074
  • 1
  • 22
  • 29
  • Thanks for your help; the if-else structure was just how it ended up after my frustration. Batch files have me feeling like a novice again haha. – Sam Tuson Dec 10 '19 at 08:38