1

I am working in Windows 10.

This is my code saved in .bat file:

%windir%\System32\cmd.exe "/K" C:\Users\Alex\AppData\Local\Continuum\anaconda3\Scripts\activate.bat C:\Users\Alex\AppData\Local\Continuum\anaconda3

"c:\Arch\Upload from DB to Redcap.py"

If I open Command Prompt (cmd) manually and execute each line everything works fine. But if I execute .bat file with content above, first line executes fine but second line just changes current folder to c:\Arch\ folder and Upload from DB to Redcap.py file is not executed. How to fix my code so second line of my code execute Upload from DB to Redcap.py file that is located at c:\Arch\Upload from DB to Redcap.py ?

1
  • 1
    Maybe the first command started a new instance of cmd within cmd, if you type exit then it might continue with the second line.. which isn't what you want. You can try 'start' in the manner suggested.. or for calling a bat file you can use call i.e. call blah.bat
    – barlop
    Commented May 2, 2020 at 14:10

2 Answers 2

1
  • cmd /keep vs cmd /continue

If you are using /k, and you don't need quotes, until you type exit command, the next command on the bat/cmd will never be executed.

You started a cmd /keep "interpreter" and did not leave/exit/close it, only after this that your next command will be executed...

When you use the terminal by opening a new interpreter by typing 'cmd /k', it works because you are interacting directly with it, in this case in bat, onli line that work is: cmd /k "...\enable.bat", the bat file in command is also executed because it is on the same line.

In the bat, cmd / k somefile.bat [or] some command it will start a new interpreter and keep it open for input until the moment you type exit or close the current cmd window. Otherwise, it will stay waiting for you to insert one or more commands to execute them, and in the code, the next execution is on the next line: ("C:\Arch\Upload from DB to Redcap.py"), it will never run unless you type it for this current interpreter, or type exit to quit and return to previous interpreter run you next line command.

Try replace with cmd /c and your interpreter will goon execute your code line by line...

But I think it’s not even a cmd call running his bat/cmd.


The command interpreter (cmd.exe) will execute your bat/cmd just by executing or, when you click on it, there is no need to add a specific instruction to execute your bat script.

Save this code below as file.bat and try:

@echo off 

cd /d "%UserProfile%\AppData\Local\Continuum\Anaconda3" 
call .\Scripts\Activate.bat "%UserProfile%\AppData\Local\Continuum\Anaconda3" 

py "c:\Arch\Upload from DB to Redcap.py"

In addition, if you are using in one bat file to to execute another batfile, so use call c:\file.bat...

[√] cmd

[√] cmd /MS Docs

[√] How to run multiple bat files within a bat file

7
  • Thank you. Your code worked after I replace py "c:\Arch\Upload from DB to Redcap.py" with python "c:\Arch\Upload from DB to Redcap.py"
    – vasili111
    Commented May 2, 2020 at 17:42
  • Also thank you for good explanation. This is very helpful!
    – vasili111
    Commented May 2, 2020 at 17:46
  • 1
    @vasili111 Thank you too, good to know that work
    – Io-oI
    Commented May 2, 2020 at 17:48
  • @vasili111 If you have official distro from Python, then py will be in c:\windows\system32... see here
    – Io-oI
    Commented May 2, 2020 at 17:54
  • No py file in C:\windows\system32 or `c:\windows`. I have Python from Anaconda installation. It seams Anaconda does not have Python Launcher for Windows stackoverflow.com/a/30794388/1601703 But at that link and here stackoverflow.com/questions/51861943/… are ways to install it. Thank you very much for that information I did not knew about Python Launcher for Windows.
    – vasili111
    Commented May 2, 2020 at 18:11
2

If python.exe is in your PATH, you could try to prepend python.exe to the offending line:

python.exe "c:\Arch\Upload from DB to Redcap.py"

Another option is to use the start command:

start /b "" "c:\Arch\Upload from DB to Redcap.py"

/b tells start not to open a new cmd window, and "" is to avoid it interpreting "c:\Arch\Upload from DB to Redcap.py" as a title.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .