本文介绍了使用“currentDirectory"路径中的变量 (.Vbs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个脚本有什么问题

Set WshShell = CreateObject("WScript.Shell")Set objFSO = CreateObject("Scripting.FileSystemObject")currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))WshShell.Run "C:\Windows\System32\diskpart.exe/s currentDirectory&"\vhd.txt", 1, True

我的意思是我知道currentDirectory&"用法错误但无法纠正

解决方案

(1) 要获取当前目录,请在 .\ 或 .CurrentDirectory 上使用 .GetAbsolutePathName:

>>WScript.Echo goFS.GetAbsolutePathName(".\")>>WScript.Echo CreateObject("WScript.Shell").CurrentDirectory>>E:\trials\SoTrials\answers\28892856\vbsE:\trials\SoTrials\answers\28892856\vbs

(2) 要获取脚本的目录,请在 WScript.ScriptFullName 上使用 .GetParentFolderName:

>>WScript.Echo goFS.GetParentFolderName(WScript.ScriptFullName)>>M:\bin

(3) 要根据路径和文件名构建文件规范,请使用 .BuildPath:

>>WScript.Echo goFS.BuildPath("a\", "\b")>>一\二

对比一下

>>WScript.Echo left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName))) &"\vhd.txt">>M:\bin\\vhd.txt

发明你自己的黑客会让你处于危险之中(哪个原生或更邪恶的 - 用户定义的函数/子会(不)容忍 \\?)而没有任何好处.

(4) 与其他语言不同,例如Perl、VBScript 既不将变量内容插入/替换为字符串文字,也不评估其中的函数或运算符:

body = "身体"WScript.Echo "头& 身& 尾"

头&身体&tail ' <--- 字符串字面量不变

连接运算符 &必须在文字之外使用:

>>身体 = "身体">>WScript.Echo头"&身体&尾巴">>头尾

更新 wrt tarkan 的评论真的很有趣,我总是收到错误,请有人更正代码的最后一行":

证明Serenity的代码一点都不好笑:

>>WScript.Echo "记事本" &Chr(34) &"c:\windows\win.ini" &铬(34)>>记事本c:\windows\win.ini"

What is wrong with this script

Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
WshShell.Run "C:\Windows\System32\diskpart.exe /s currentDirectory&"\vhd.txt", 1, True

I mean I know the "currentDirectory&" usage is wrong but couldn't correct it

解决方案

(1) To get the current directory, use .GetAbsolutePathName on .\ or .CurrentDirectory:

>> WScript.Echo goFS.GetAbsolutePathName(".\")
>> WScript.Echo CreateObject("WScript.Shell").CurrentDirectory
>>
E:\trials\SoTrials\answers\28892856\vbs
E:\trials\SoTrials\answers\28892856\vbs

(2) To get script's directory, use .GetParentFolderName on WScript.ScriptFullName:

>> WScript.Echo goFS.GetParentFolderName(WScript.ScriptFullName)
>>
M:\bin

(3) To build a file spec from a path and a file name, use .BuildPath:

>> WScript.Echo goFS.BuildPath("a\", "\b")
>>
a\b

Compare this to

>> WScript.Echo left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName))) & "\vhd.txt"
>>
M:\bin\\vhd.txt

Inventing your own hacks puts you at risk (which native or - more evil - user defined functions/subs will (not) tolerate the \\?) without any benefits.

(4) Unlike other languages, e.g. Perl, VBScript neither interpolates/substitutes variable content into string literals, nor evaluates functions or operators in them:

The concatenation operator & has to be used out of the literals:

>> body = "BODY"
>> WScript.Echo "head" & body & "tail"
>>
headBODYtail

Update wrt tarkan's comment "really funny I'm always getting an error could someone correct the last line of the code please":

To prove that Serenity's code is not funny at all:

>> WScript.Echo "Notepad " & Chr(34) & "c:\windows\win.ini" & Chr(34)
>>
Notepad "c:\windows\win.ini"

这篇关于使用“currentDirectory"路径中的变量 (.Vbs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:53