0

so am trying to make python open cmd and navigate within the new folder with an argument form python.

i have a custom command in cmd to start det first batch file and the second argument i have the command is the name of the new folder.

create.bat

cd C:\Users\Eirik\Documents\MyProjects

python "C:\batch command\ACJ\create.py" %1

create.py

import sys
import subprocess

folderName = str(sys.argv[1]) 

def create():
    if not os.path.exists(folderName):
        os.mkdir(folderName) 
        print("Directory " , folderName,  " Created ")
        subprocess.call(["C:\\batch command\\nav.bat", folderName], shell=True )
    else:
        print("Directory " , folderName,  "already exists, choose another name. ")

create() 

nav.bat

cd C:Users\Eirik\Documents\MyProjects\%1
  • 1
    That code looks like it would work. What about it isn't working? For that matter, I don't see any *question* here at all -- what exactly are you looking for? (Note that a script that only does `cd` is pointless because [changing the current working directory in a subprocess does not affect the current working directory in the parent process](https://stackoverflow.com/questions/21406887/subprocess-changing-directory).) – Daniel Pryden Jun 07 '19 at 20:25
  • hi, so what does work for me is subprocess.call, i want it to run that bat file and move into the new directory i just made within the terminal window. Thank you for the link, is was very helpful for me understanding the subprosses. – AnnonymKnuckles Jun 07 '19 at 20:41

1 Answers1

0

my friend rewrited my script so now it works.

create.bat

@echo off

set dir="%1"

set projectPath="C:\Users\Eirik\Documents\MyProjects"

if exist "%projectPath%\%dir%" (
    echo %dir% already exists
    exit /b
)

cd "%projectPath%"

python "C:\batch command\ACJ\create.py" %dir%

cd "%projectPath%\%dir%"

create.py

import os 
import sys

folderName = str(sys.argv[1]) 

def create():
    if not os.path.exists(folderName):
        os.mkdir(folderName) 
        print("Directory " , folderName,  " Created ")

create()  

  • 1
    Just some batch file basics for you... It should be `set "dir=%~1"`, `set "projectPath=C:\Users\Eirik\Documents\MyProjects"`, `if exist "%projectPath%\%dir%\" (` and `cd /d "%projectPath%"`, `python "C:\batch command\ACJ\create.py" "%dir%"` and `cd "%dir%"`. – Compo Jun 07 '19 at 21:28