简体   繁体   English

如何让我的“终端”命令从vba(在OSX上)的正确文件夹中运行

[英]How do i get my “terminal” commands to run in the right folder from vba (on OSX)

I have an excelfile (Excel 2011, running on OSX 10.9.4) which exports its data into a textfile. 我有一个excelfile(Excel 2011,在OSX 10.9.4上运行),它将数据导出到文本文件中。 I want to automatically upload this textfile to Git once it is exported. 我希望在导出后自动将此文本文件上传到Git。

I use the execShell function described in this post: VBA Shell function in Office 2011 for Mac Which gives me nice output for my executed commands. 我使用本文中描述的execShell函数: Office 2011 for Mac中的VBA Shell函数为我执行的命令提供了很好的输出。

I want to go to my folder (where my new file was put) and from there call my git commit commands ("git add *", "git commit -m commitmessage" and "git push"). 我想去我的文件夹(我的新文件放在那里),然后从那里调用我的git commit命令(“git add *”,“git commit -m commitmessage”和“git push”)。 But the problem is i cannot get to my folder. 但问题是我无法访问我的文件夹。 If i try cd /Users/username/folder and then pwd i keep getting back "/ " as location (from where i cannot call my git commands) 如果我尝试cd /Users/username/folder然后pwd我一直回到“/”作为位置(从那里我不能调用我的git命令)

the complete code for this looks like this: 完整的代码如下所示:

commitMessage = """New feature file: " & ActiveSheet.Name & """"
gotoPath = Replace(Replace(CurDir(), ":", "/"), "Macintosh HD", "") 'make a "normal" path from CurDir()
result = execShell("cd " & gotoPath, exitCode)
result = execShell("pwd", exitCode)
result = execShell("git add *", exitCode)
result = execShell("git commit -m """ & commitMessage & """", exitCode)
result = execShell("git push", exitCode)

If i execute my git commands from the terminal myself (while in the right folder), the commit works fine. 如果我自己从终端执行我的git命令(在右侧文件夹中),提交工作正常。 If i execute a "say" command with the execShell function, it works fine as well 如果我使用execShell函数执行“say”命令,它也可以正常工作

Anybody have any suggestions about how to get to the right location? 有人对如何到达正确的位置有任何建议吗? or alternatives to use for this purpose? 或用于此目的的替代方案?

Each time you call execShell it starts a new shell. 每次调用execShell都会启动一个新的shell。 That shell executes the command and then exits. 该shell执行命令然后退出。 So your new shell does change directory for that shell then exits. 所以你的新shell确实改变for that shell目录然后退出。 The next new shell you exec starts in exactly the same place your last one started - not where it exited because it is a new, different process. 你执行的下一个新shell在你最后一个开始的同一个地方开始 - 而不是它退出的地方,因为它是一个新的,不同的过程。

You need to do all things in one shell: 你需要在一个shell中做所有事情:

execShell("cd somewhere && do something && do something else")

The above will only do each subsequent command if the previous was successful, If you want to do additional commands unconditionally, regardless of whether the previous one failed, separate the commands by a semi-colon: 如果前一个命令成功,上面只会执行每个后续命令。如果要无条件地执行其他命令,无论前一个命令是否失败,请用分号分隔命令:

execShell("cd somewhere; do something; do something else")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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