Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过excelvba运行批处理文件_Excel_Vba_Batch File - Fatal编程技术网

通过excelvba运行批处理文件

通过excelvba运行批处理文件,excel,vba,batch-file,Excel,Vba,Batch File,我有一个运行Python脚本的批处理文件 我可以手动单击以执行该批处理文件 当我试图通过Excel VBA运行它时,文件不会执行 批处理文件中的代码如下所示: %cd file.py 批处理文件和我要执行的文件位于同一目录中 如果我键入准确的目录地址,则Excel VBA在运行批处理文件时没有问题,而不是在上述代码中键入%cd%。例如,如果我的批处理文件看起来像这样,那么就没有问题 c: cd c:\folder file.py 在批处理文件中使用以下代码: @echo off cd /D

我有一个运行Python脚本的批处理文件

我可以手动单击以执行该批处理文件

当我试图通过Excel VBA运行它时,文件不会执行

批处理文件中的代码如下所示:

%cd
file.py
批处理文件和我要执行的文件位于同一目录中

如果我键入准确的目录地址,则Excel VBA在运行批处理文件时没有问题,而不是在上述代码中键入%cd%。例如,如果我的批处理文件看起来像这样,那么就没有问题

c:
cd c:\folder
file.py

在批处理文件中使用以下代码:

@echo off
cd /D "%~dp0"
python.exe file.py
如果批处理文件总是在同一台机器上执行,并且python的安装位置已知,那么最好使用完整路径指定
python.exe

命令
cd/D“%~dp0”
将当前目录设置为批处理文件的目录,即使该批处理文件位于与当前驱动器不同的驱动器上。在命令提示窗口中运行
cd/?
call/?
,了解详细信息


*.py文件不是可执行文件。这是一个Python脚本,需要一个Python解释器来执行某些操作。因此,最好以脚本文件作为参数显式运行Python解释器,而不是让Windows通过Windows注册表找到用于打开的*.py文件,如果Python.exe是为打开操作注册的应用程序,则会执行脚本。

使用批处理文件中的以下代码:

@echo off
cd /D "%~dp0"
python.exe file.py
如果批处理文件总是在同一台机器上执行,并且python的安装位置已知,那么最好使用完整路径指定
python.exe

命令
cd/D“%~dp0”
将当前目录设置为批处理文件的目录,即使该批处理文件位于与当前驱动器不同的驱动器上。在命令提示窗口中运行
cd/?
call/?
,了解详细信息


*.py文件不是可执行文件。这是一个Python脚本,需要一个Python解释器来执行某些操作。因此,最好以脚本文件作为参数显式运行Python解释器,而不是让Windows通过Windows注册表找到用于打开的*.py文件,如果Python.exe是注册用于打开操作的应用程序,则会执行脚本。

谢谢很长一段时间以来,我一直对此感到困扰。现在它更有意义了。我要求执行文件夹中的所有SQL。 **我不熟悉批处理文件

以下是我的答案示例:-

Excel命令按钮代码:

Private Sub CommandButton1_Click()
Dim Username1 As String
Dim Password1 As String
Dim Server1 As String
Dim Database1 As String
Dim CommandString As String

Username1 = Cells(5, 6).Value
Password1 = Cells(6, 6).Value
Server1 = Cells(7, 6).Value
Database1 = Chr(34) + Cells(8, 6).Value + Chr(34)
CommandString = ThisWorkbook.Path & "\Place_SQL_Scripts_in_here\ExecuteAllSQLWithExcelParameters.bat" + " " + Username1 + " " + Password1 + " " + Server1 + " " + Database1
Call Shell("cmd.exe /c" & CommandString, vbNormalFocus)
End Sub
批处理文件代码:@Mofi-我使用了您的代码片段

@Echo off 

goto :init

:init
setlocal 
set Username1=%1
set Password1=%2
set Server1=%3
set Database1=%4
Echo.
Echo Executing All SQL Scripts in Folder "Place_SQL_Scripts_in_here"
Echo.

cd /D "%~dp0"

for %%G in (*.sql) do (echo [Executing %%G] & echo. & sqlcmd -S %Server1% -d %Database1% -U %Username1% -P %Password1% -i "%%G" & echo.)
Goto :End

:End
Echo Completed
Echo.
PAUSE
endlocal
goto :EOF

我也从这篇文章中得到了帮助()

非常感谢你,我在这方面一直有问题。现在它更有意义了。我要求执行文件夹中的所有SQL。 **我不熟悉批处理文件

以下是我的答案示例:-

Excel命令按钮代码:

Private Sub CommandButton1_Click()
Dim Username1 As String
Dim Password1 As String
Dim Server1 As String
Dim Database1 As String
Dim CommandString As String

Username1 = Cells(5, 6).Value
Password1 = Cells(6, 6).Value
Server1 = Cells(7, 6).Value
Database1 = Chr(34) + Cells(8, 6).Value + Chr(34)
CommandString = ThisWorkbook.Path & "\Place_SQL_Scripts_in_here\ExecuteAllSQLWithExcelParameters.bat" + " " + Username1 + " " + Password1 + " " + Server1 + " " + Database1
Call Shell("cmd.exe /c" & CommandString, vbNormalFocus)
End Sub
批处理文件代码:@Mofi-我使用了您的代码片段

@Echo off 

goto :init

:init
setlocal 
set Username1=%1
set Password1=%2
set Server1=%3
set Database1=%4
Echo.
Echo Executing All SQL Scripts in Folder "Place_SQL_Scripts_in_here"
Echo.

cd /D "%~dp0"

for %%G in (*.sql) do (echo [Executing %%G] & echo. & sqlcmd -S %Server1% -d %Database1% -U %Username1% -P %Password1% -i "%%G" & echo.)
Goto :End

:End
Echo Completed
Echo.
PAUSE
endlocal
goto :EOF

我也从这篇文章中得到了帮助()

这对我很有用:
Shell“test.bat”
您有Excel VBA运行一个运行python的批处理文件。你有一个问题要解决,现在有三个。这对我来说很有用:
Shell“test.bat”
你有Excel VBA运行一个运行python的批处理文件。你有一个问题要解决,现在你有三个。