[英]VBA Macro to run PowerShell Command
I am trying to create a macro, that would be able to rename all files with ".docx" extension in a folder.我正在尝试创建一个宏,它可以重命名文件夹中所有带有“.docx”扩展名的文件。
The PowerShell command works when used directly, but when used through the VBA code, it's not executing. PowerShell 命令在直接使用时有效,但通过 VBA 代码使用时,它不会执行。
Sub test()
Dim retval
Dim pscmd
pscmd = "PowerShell -ExecutionPolicy Bypass -Command ""Get-ChildItem -Path *.docx -Recurse | Rename-Item -NewName {$_.Name.Substring(0,7) + $_.Extension }"""
retval = Shell(pscmd, vbNormalFocus)
Debug.Print pscmd
End Sub
The script is executing without compilation errors, but the files are not being renamed.脚本正在执行,没有编译错误,但文件没有被重命名。
What do I need to change?我需要改变什么?
You have to provide the path where your files exist Try this and change the path to suit you您必须提供文件所在的路径试试这个并更改路径以适合您
Sub Test()
Dim retval, pscmd
pscmd = "PowerShell -ExecutionPolicy Bypass -Command ""Get-ChildItem -Path " & ThisWorkbook.Path & "\Files\" & " *.docx -Recurse | Rename-Item -NewName {$_.Name.Substring(0,7) + $_.Extension }"""
retval = Shell(pscmd, vbNormalFocus)
Debug.Print pscmd
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.