简体   繁体   中英

How to replicate the functionality of the open command on Windows using a batch file?

I'm trying to replicate the functionality of the open command on the mac using Windows batch scripts. Here is what I have so far in my file open.cmd that I have on the PATH :

echo off
IF "%1"=="" GOTO HAVE_0
IF "%1"=="." GOTO HAVE_0
IF "%2"=="" GOTO HAVE_1
GOTO ERROR

:HAVE_0
explorer %cd%
GOTO END

:HAVE_1
explorer %1

:ERROR
echo "Incorrect number of arguments."
echo "Please use the command: c:\>open <directory>"
echo "eg c:\>open ."

:END

Now this works. I think there must be a better way or something I've missed. Is there a better way?

My question is: How to replicate the functionality of the open command on Windows using a batch file?

As eryksun points out, start is very similar to open . You can shorten your script to just this:

@start "" %*

and I think it'll work mostly as you intend. If you supply a directory as an argument, the script will open an Explorer window. If you supply a URL, it'll open your default web browser. If you supply a document, it'll open the document in its associated program.

(How's that for code golfing?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM