在VBA中为命令提示符窗口创建实例 [英] Create an instance to Command prompt window in VBA

查看:133
本文介绍了在VBA中为命令提示符窗口创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一些示例,如何使用Chell函数打开和运行命令行命令。

I have seen examples, how to open and run command line commands with Chell function.

不幸的是,这些示例并不直接适合我的目的。

Unfortunately those examples not directly fit for my purpose.

我想打开一个CMD提示窗口,并在需要的时间内保持活动状态。

当我想要时,我可以调用现有实例并提供更多命令那个窗口。

I would like to open a CMD prompt window and keep it alive as long time as it is needed.
And when I want, I can call the existing instance and give further commands in that window.

例如在Excel宏中:

- 打开一个CMD提示窗口并在excel方面给出命令"cd \"
$
-do动作

-go返回CMD窗口并发出命令"cd users"

-etc

注意,它不是一个组合像"cd"这样的命令的选项\\& "cd users"

e.g. in an Excel macro:
-open a CMD prompt window and give command "cd\"
-do actions in excel side
-go back to the CMD window and give command "cd users"
-etc
Note, it is not an option to combine the commands like "cd\ & cd users"

推荐答案

Re:文件,文件夹和驱动器操作



您可以使用VBA中的现有方法来执行指定的任务。

以下文字来自xl97帮助文件。

'---

Jim Cone

美国俄勒冈州波特兰市>
免费&商业excel程序(不适用xl2013 +)

https:// goo .gl / IUQUN2  (Dropbox)



' ~~~~~~~~~~~~~~~~


CurDir功能

退货表示当前路径的Variant(String)。

语法:

CurDir [(驱动器)]



可选的drive参数是一个字符串表达式,用于指定现有驱动器。


如果未指定驱动器或驱动器为零长度字符串("""),则为
  CurDir返回当前路径驱动。



'---

ChDir声明

更改当前目录或文件夹。

语法:
$
ChDir路径



必需的路径参数是一个字符串表达式,用于标识哪个目录


 或文件夹成为新的默认目录或文件夹。

路径可能包括驱动器。如果未指定驱动器,则以
  ChDir更改当前驱动器上的默认目录或文件夹。



备注:

ChDir语句更改默认目录,但不更改默认驱动器。

例如,如果默认驱动器为C,则以下语句将更改

 驱动器D上的默认目录,但C仍为默认驱动器:

ChDir"D:\TMP"



'---

ChDrive声明

更改当前驱动器。

语法:

ChDrive驱动器

所需的驱动器参数是一个字符串表达式指定现有驱动器。


如果提供零长度字符串("""),则当前驱动器不会更改。


如果驱动器参数是多字符串,则ChDrive仅使用第一个字母。



'---

MkDir声明

创建新目录或文件夹。

语法:

MkDir路径

必需的path参数是一个字符串表达式,用于标识要创建的目录或文件夹。


路径可能包含驱动器。如果未指定驱动器,则为
  MkDir在当前驱动器上创建新目录或文件夹。



'---

RmDir语句

删除现有目录或文件夹。

语法:

RmDir路径

必需的path参数是一个字符串表达式,用于标识要删除的目录或文件夹。


路径可能包含驱动器。如果未指定驱动器,则以
  RmDir删除当前驱动器上的目录或文件夹。



备注:

如果尝试在包含文件的目录或文件夹上使用RmDir,则会发生错误。


在尝试删除目录或文件夹之前,使用Kill语句删除所有文件。




'---

杀死声明

删除来自磁盘的文件。

语法:

Kill pathname

必需的pathname参数是一个字符串表达式,指定一个或多个文件名删除。


 路径名可能包含目录或文件夹以及驱动器。



备注:

Kill支持使用多字符(*)和单字符(?)

 通配符来指定多个文件。

如果您发生错误尝试使用Kill删除打开的文件。



注意: 要删除目录,请使用RmDir语句。






Re: file, folder and drive manipulation

You can use existing methods in VBA to do your specified tasks.
The text below is from the xl97 help file.
'---
Jim Cone
Portland, Oregon USA
free & commercial excel programs (n/a xl2013+)
https://goo.gl/IUQUN2 (Dropbox)

'~~~~~~~~~~~~~~~~

CurDir function
Returns a Variant (String) representing the current path.
Syntax:
CurDir[(drive)]

The optional drive argument is a string expression that specifies an existing drive.
If no drive is specified or if drive is a zero-length string (""),
 CurDir returns the path for the current drive.

'---
ChDir statement
Changes the current directory or folder.
Syntax:
ChDir path

The required path argument is a string expression that identifies which directory
 or folder becomes the new default directory or folder.
The path may include the drive. If no drive is specified,
 ChDir changes the default directory or folder on the current drive.

Remarks:
The ChDir statement changes the default directory but not the default drive.
For example, if the default drive is C, the following statement changes
 the default directory on drive D, but C remains the default drive:
ChDir "D:\TMP"

'---
ChDrive statement
Changes the current drive.
Syntax:
ChDrive drive
The required drive argument is a string expression that specifies an existing drive.
If you supply a zero-length string (""), the current drive doesn't change.
If the drive argument is a multiple-character string, ChDrive uses only the first letter.

'---
MkDir statement
Creates a new directory or folder.
Syntax:
MkDir path
The required path argument is a string expression that identifies the directory or folder to be created.
The path may include the drive. If no drive is specified,
 MkDir creates the new directory or folder on the current drive.

'---
RmDir statement
Removes an existing directory or folder.
Syntax:
RmDir path
The required path argument is a string expression that identifies the directory or folder to be removed.
The path may include the drive. If no drive is specified,
 RmDir removes the directory or folder on the current drive.

Remarks:
An error occurs if you try to use RmDir on a directory or folder containing files.
Use the Kill statement to delete all files before attempting to remove a directory or folder.

'---
Kill statement
Deletes files from a disk.
Syntax:
Kill pathname
The required pathname argument is a string expression that specifies one or more file names to be deleted.
 The pathname may include the directory or folder, and the drive.

Remarks:
Kill supports the use of multiple-character (*) and single-character (?)
 wildcards to specify multiple files.
An error occurs if you try to use Kill to delete an open file.

Note:  To delete directories, use the RmDir statement.




这篇关于在VBA中为命令提示符窗口创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆