VBScript - 通过 cmd.exe 创建具有用户定义的名称和扩展名的文件
VBScript - Create file with user defined name and extension via cmd.exe
抱歉缺少缩进。问题是双重的,并且特定于代码的最后一行。
首先,我似乎无法让 %username% 在其上下文中工作……其次是“. & ext”。我尝试输入完整的目标文件夹地址,但无济于事;并尝试为点扩展添加包围引号,再次无济于事...我有一些使用 CMD 的经验,但使用 VBS 的经验很少...感谢您的帮助!
dim name, app, ext
do
name = inputbox ("Please enter file name", "Make File")
if name = "" then
wscript.quit ()
end if
loop until name <> ""
do while (ext = "")
app = inputbox ("Please enter related application", "Make File")
if app = "" then
wscript.quit ()
elseif lcase(app) = "notepad" then
ext = "txt"
elseif lcase(app) = "word" then
ext = "doc"
elseif lcase(app) = "excel" then
ext = "xml"
elseif lcase(app) = "powerpoint" then
ext = "ppt"
else
msgbox("Application not recognised")
end if
loop
msgbox "File created in desktop folder"
set objshell = createobject("wscript.shell")
objshell.run "cmd /c echo. > c:\Users\%username%\Desktop\" & name . & ext
首先,在您的示例中,您在代码最后一行的句号周围遗漏了一些引号,但我怀疑这只是一个拼写错误,因为它仍然可能无法正常工作。你需要像这样双引号:
objshell.run "cmd /c echo. > ""c:\Users\%username%\Desktop\" & name &"." & ext & """"
鉴于桌面文件夹是一个 "special" 文件夹,最好不要像此处那样尝试枚举文件夹 - 在某些环境中,用户配置文件会重新映射别的地方。
相反,您可以像这样修改代码的最后几行:
msgbox "File created in desktop folder"
set objshell = createobject("wscript.shell")
desktopFolder = objShell.SpecialFolders("Desktop") ' This will get you the desktop folder location...
objshell.run "cmd /c echo. > """& desktopfolder & "\" & name & "." & ext & """" ' This is messy, but it will likely work.
或者,您可以将整个代码替换为以下示例。您可以使用 case 语句而不是使用令人困惑的 elseif 代码,而且我不确定您是否知道,但是如果您在 IF 语句中只有一个条件,则可以将它们键入同一行(请参阅下面的例子)。
此外,鉴于您已经在 VBS 中,您也可以使用 vb 命令创建文件,而不是向 运行 发送 CMD...。所以改为使用 cmd /c echo 行,你可以使用这样的东西:
Set objFile = objFSO.CreateTextFile(desktopfolder & "\" & Name & "." & Ext,True)
无论如何,这是应该适合您的脚本的另一个版本。
祝你今天过得愉快 ! :)
修订版
Option Explicit
Dim Name, App, Ext
Dim objShell, objFSO, desktopFolder, objFile
Do
Name = inputbox ("Please enter file name", "Make File")
if Name = "" Then Wscript.Quit ()
Loop until Name <> ""
Do
App = inputbox ("Please enter related application", "Make File")
If App = "" Then Wscript.Quit ()
Select Case (LCase(App))
Case "notepad"
Ext = "txt"
Case "word"
Ext = "doc"
Case "excel"
Ext = "xml"
Case "powerpoint"
Ext = "ppt"
Case else
msgbox("Application not recognised")
App = ""
End Select
Loop Until App <> ""
set objShell = Createobject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
desktopFolder = objShell.SpecialFolders("Desktop")
Set objFile = objFSO.CreateTextFile(desktopfolder & "\" & Name & "." & Ext,True)
objFile.Close
msgbox "File created in desktop folder"
抱歉缺少缩进。问题是双重的,并且特定于代码的最后一行。 首先,我似乎无法让 %username% 在其上下文中工作……其次是“. & ext”。我尝试输入完整的目标文件夹地址,但无济于事;并尝试为点扩展添加包围引号,再次无济于事...我有一些使用 CMD 的经验,但使用 VBS 的经验很少...感谢您的帮助!
dim name, app, ext
do
name = inputbox ("Please enter file name", "Make File")
if name = "" then
wscript.quit ()
end if
loop until name <> ""
do while (ext = "")
app = inputbox ("Please enter related application", "Make File")
if app = "" then
wscript.quit ()
elseif lcase(app) = "notepad" then
ext = "txt"
elseif lcase(app) = "word" then
ext = "doc"
elseif lcase(app) = "excel" then
ext = "xml"
elseif lcase(app) = "powerpoint" then
ext = "ppt"
else
msgbox("Application not recognised")
end if
loop
msgbox "File created in desktop folder"
set objshell = createobject("wscript.shell")
objshell.run "cmd /c echo. > c:\Users\%username%\Desktop\" & name . & ext
首先,在您的示例中,您在代码最后一行的句号周围遗漏了一些引号,但我怀疑这只是一个拼写错误,因为它仍然可能无法正常工作。你需要像这样双引号:
objshell.run "cmd /c echo. > ""c:\Users\%username%\Desktop\" & name &"." & ext & """"
鉴于桌面文件夹是一个 "special" 文件夹,最好不要像此处那样尝试枚举文件夹 - 在某些环境中,用户配置文件会重新映射别的地方。
相反,您可以像这样修改代码的最后几行:
msgbox "File created in desktop folder"
set objshell = createobject("wscript.shell")
desktopFolder = objShell.SpecialFolders("Desktop") ' This will get you the desktop folder location...
objshell.run "cmd /c echo. > """& desktopfolder & "\" & name & "." & ext & """" ' This is messy, but it will likely work.
或者,您可以将整个代码替换为以下示例。您可以使用 case 语句而不是使用令人困惑的 elseif 代码,而且我不确定您是否知道,但是如果您在 IF 语句中只有一个条件,则可以将它们键入同一行(请参阅下面的例子)。
此外,鉴于您已经在 VBS 中,您也可以使用 vb 命令创建文件,而不是向 运行 发送 CMD...。所以改为使用 cmd /c echo 行,你可以使用这样的东西:
Set objFile = objFSO.CreateTextFile(desktopfolder & "\" & Name & "." & Ext,True)
无论如何,这是应该适合您的脚本的另一个版本。 祝你今天过得愉快 ! :)
修订版
Option Explicit
Dim Name, App, Ext
Dim objShell, objFSO, desktopFolder, objFile
Do
Name = inputbox ("Please enter file name", "Make File")
if Name = "" Then Wscript.Quit ()
Loop until Name <> ""
Do
App = inputbox ("Please enter related application", "Make File")
If App = "" Then Wscript.Quit ()
Select Case (LCase(App))
Case "notepad"
Ext = "txt"
Case "word"
Ext = "doc"
Case "excel"
Ext = "xml"
Case "powerpoint"
Ext = "ppt"
Case else
msgbox("Application not recognised")
App = ""
End Select
Loop Until App <> ""
set objShell = Createobject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
desktopFolder = objShell.SpecialFolders("Desktop")
Set objFile = objFSO.CreateTextFile(desktopfolder & "\" & Name & "." & Ext,True)
objFile.Close
msgbox "File created in desktop folder"