forked from gocronx-team/gocron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_windows_cmd.go
More file actions
36 lines (29 loc) · 926 Bytes
/
test_windows_cmd.go
File metadata and controls
36 lines (29 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//go:build ignore
// +build ignore
package main
import (
"fmt"
"os/exec"
)
func main() {
// 测试不同的命令构造方式
command := `dir "C:\Program Files (x86)"`
fmt.Println("原始命令:", command)
fmt.Println()
// 方式1: 直接传递
fmt.Println("方式1: cmd /C command")
cmd1 := exec.Command("cmd", "/C", command)
fmt.Printf(" Args: %#v\n", cmd1.Args)
fmt.Printf(" String: %s\n\n", cmd1.String())
// 方式2: 使用 /S /C "command"
fmt.Println("方式2: cmd /S /C \"command\"")
wrappedCommand := `"` + command + `"`
cmd2 := exec.Command("cmd", "/S", "/C", wrappedCommand)
fmt.Printf(" Args: %#v\n", cmd2.Args)
fmt.Printf(" String: %s\n\n", cmd2.String())
// 方式3: 使用 /c 和完整引号包裹
fmt.Println("方式3: cmd /c \"command\"")
cmd3 := exec.Command("cmd", "/c", `"`+command+`"`)
fmt.Printf(" Args: %#v\n", cmd3.Args)
fmt.Printf(" String: %s\n\n", cmd3.String())
}