使用python进行批量scipt导航

时间:2019-06-07 20:21:52

标签: python batch-file

因此,我试图使python打开cmd,并使用python形式的参数在新文件夹中导航。

我在cmd中有一个自定义命令来启动det第一个批处理文件,第二个参数我具有的命令是新文件夹的名称。

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 个答案:

答案 0 :(得分:0)

我的朋友重写了我的脚本,因此现在可以使用了。

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()