Asked  11 Months ago    Answers:  5   Viewed   13 times

How to run a simple Windows command?

This command:

exec.Command("del", "c:\aaa.txt")

.. outputs this message:

del: executable file not found in %path%

What am I doing wrong?



I got the same error as you. But dystroy is correct: You can't run del or any other command built into cmd because there is no del.exe file (or any other del-executable for that matter).

I got it to work with:

package main


func main(){    
    c := exec.Command("cmd", "/C", "del", "D:\a.txt")

    if err := c.Run(); err != nil { 
        fmt.Println("Error: ", err)
Wednesday, July 28, 2021
C:> assoc .py=Python
C:> ftype Python="C:python27python.exe %1 %*"

Or whatever the relevant path is - you can also set command line args using ftype.

In order to make a command recognized without having to give the suffix (.py), similar to how it works for .exe files, add .py to the semi-colon separated list of the (global) PATHEXT variable.

ETA 2017-07-27

Seems like this is still getting eyeballs, wanted to elevate a useful comment for Win10 users (from @shadowrunner):

For me to get it work under Win10 the actual command was (note the placement of the quotes):

C:> ftype Python="c:Anaconda2python.exe" "%1" %*

ETA 2019-02-01

Talk about evergreen!

First of all, if you're newly installing Python, I highly recommend reviewing the answer by @NunoAndré .

Secondly, to clarify something from a recent comment, please note: you must do both parts (assoc and ftype), or use a pre-existing association label in the ftype command.

By default, at least for Python 3.7 under Windows 8.1, the association for .py is Python.File, so performing the ftype command I wrote above will not work correctly unless the association is first changed. Or you can just use ftype and give the default association instead. Up to you.

Monday, June 7, 2021

The "cd" command changes the directory, but not what drive you are working with. So when you go "cd d:temp", you are changing the D drive's directory to temp, but staying in the C drive.

Execute these two commands:

cd temp

That will get you the results you want.

Tuesday, July 27, 2021

Yes, you have to install a compiler, Eclipse is only an IDE. You can get MinGW here.

Wednesday, August 11, 2021

App Paths registry settings - used by Windows Explorer, to locate programs when you type program name in "Run..." box, and so on. These settings are not affecting cmd.exe behavior.

The only way to set up cmd.exe paths for searching applications is changing PATH environment variable. You can start cmd.exe from your own .bat/.cmd where you set up all required variables for current session, without changing it globally.

Monday, December 27, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :