我一直在网上搜索。有没有办法切断空间和文件名的其余部分,但保留扩展名为VBScript。
说我有这样的文件名:
filename this is a file.txt
VBScript可以在之后切断空间和一切,但是像这样留下扩展名:
filename.txt
答案 0 :(得分:1)
当然,您可以使用vbscript中提供的字符串functions进行一些手术。
dim s
dim s2
s = "filename this is a file.txt"
s2 = Left(s, Instr(s, " ")-1) & Right(s, Len(s) - InstrRev(s, ".") + 1)
msgbox s2
答案 1 :(得分:1)
有几种方法可以达到你想要的效果:
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "^(\S*).*(\..*?)$"
Set f = fso.GetFile("filename this is a file.txt")
f.Name = re.Replace(f.Name, "$1$2")
使用Split
:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("filename this is a file.txt")
f.Name = Split(fso.GetBaseName(f))(0) & "." & fso.GetExtensionName(f)
使用字符串函数:请参阅answer provided by KekuSemau。
答案 2 :(得分:0)
使用RegExp剪切输入中的第一个“单词”和扩展名:
>> Set r = New RegExp
>> r.Pattern = "^(\w+)(.*)(\..*)$"
>> For Each s In Array("filename this is a file.txt", "a.txt", "1a nix ...txt")
>> WScript.Echo s, """" & r.Replace(s, "$1$3") & """"
>> Next
>>
filename this is a file.txt "filename.txt"
a.txt "a.txt"
1a nix ...txt "1a.txt"
如果你坚持使用String ops,请使用Mid()而不是Right():
>> s = "filename this is a file.txt"
>> a = Left(s, InStr(s, " ") - 1)
>> b = Mid(s, InStrRev(s, "."))
>> WScript.Echo a, b, a & b
>>
filename .txt filename.txt