本帖最后由 hubeiwdlue 于 2025-3-1 13:19 编辑
我没有powershell的知识,也不会写命令流,只是借助ai,将高飞鸟大师的函数修改了一点点。增加了3个参数,分别是对话框的名称、初始目录,后缀过滤,并将PowerShell弹窗隐藏了,打开返回地址列表,取消返回nil。但是最初还是会闪一下蓝色的窗口。
(vlax-invoke wsh 'Run myCommand 0 1)返回的是0,而不是地址,不然用这个方法窗口不会闪。
高飞大神的帖子
http://bbs.mjtd.com/thread-192194-1-1.html
 - (defun hfb:filesDlg (title initDir filter / lst mycommand mysplit res txt wsh)
- ;;;分割字符串
- (defun mysplit (str delim / lst i len)
- (setq len (strlen delim))
- (setq ch (ascii delim))
- (while (setq i (vl-string-position ch str)) ;(vl-string-search delim str)
- (setq lst (cons (substr str 1 i) lst))
- (setq str (substr str (+ 1 len i)))
- )
- (setq lst (cons str lst))
- (reverse lst)
- )
- (setq myCommand ;; 构建PowerShell命令字符串
- (strcat
- "powershell.exe -WindowStyle Hidden -Command " ;; 调用PowerShell解释器
- "Add-Type -AssemblyName System.Windows.Forms; " ;; 加载Windows窗体组件[4](@ref)
- "$dialog = New-Object System.Windows.Forms.OpenFileDialog; " ;; 创建文件对话框对象
- "$dialog.Multiselect = $true; " ;; 启用多选模式[4](@ref)
- (strcat "$dialog.Title = '" title "'; ") ;; 设置对话框标题(需转义单引号)
- (strcat "$dialog.InitialDirectory = '" initDir "'; ") ;; 设置初始目录为用户文档[4](@ref)
- (strcat "$dialog.Filter = '文本文件 (*." filter ")|*." filter "|所有文件 (*.*)|*.*'; ") ;; 文件类型过滤器[4](@ref)
- "$dialogResult = $dialog.ShowDialog(); " ;; 显示对话框并获取结果
- "if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK) {" ;; 判断是否点击确定
- " $selectedFiles = $dialog.FileNames ; " ;; 获取所选文件全路径数组
- " $selectedFiles | ForEach-Object {Write-Host $_}" ;; 逐行输出文件路径[5](@ref)
- "}"
- )
- )
- (if (and
- (setq wsh (vlax-create-object "wscript.shell")) ;; 创建WScript.Shell COM对象
- (setq res (vlax-invoke wsh 'exec myCommand)) ;; 执行PowerShell命令
- (setq txt (vlax-invoke (vlax-get res 'stdout) 'readall)) ;; 读取标准输出内容
- )
- (progn
- (setq lst (mysplit txt "\n")) ;; 自定义函数分割换行(假设mysplit已定义)
- (setq lst (vl-remove "" lst)) ;; 移除空行
- )
- )
- (vlax-release-object wsh) ;; 释放COM对象
- lst ;; 返回文件路径列表
- )
- (hfb:filesDlg "试验" "D:\" "txt")
|