如果我想在用户桌面上创建文件,如何避免使用绝对文件路径?
How do I avoid using absolute filepaths if I want files created on a user's desktop?
现在我的脚本使用绝对文件路径作为 已知 用户桌面上的输出文件夹,但是如果我想让脚本在 用户的桌面上运行怎么办?一个我不知道用户名的用户?
示例 - 这是我当前使用的行。它工作正常,但显然只适用于 "John"。我需要让它与任何潜在的用户名一起使用 - Tom、Dick、Harry 等等。
' Creating log repository
objFSO.CreateFolder "C:\Users\John\Desktop\Output"
如果桌面不在用户通常的位置,最简单但失败的方法是,这可能是公司环境。
dir "%userprofile%\Desktop"
更好的方法是
for /f "skip=2 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"') do set doc=%A
dir "%doc%" /a
(记住批量 %%A
和 %A
如果输入)我认为你的问题是关于批量的。这两种方法在 VBScript 中彼此一样简单。
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%userprofile%\Desktop\Output")
或
bKey = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")
或
strDesktop = WshShell.SpecialFolders("Desktop")
为了避免在评论中进行过多的讨论,我在这里放置了一个 self-explanatory 示例。
' helper function to quote strings
' useful in commands
Function Quot(str)
Quot = Chr(34) & str & Chr(34)
End Function
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
' get user's desktop directory and introduce a variable
currentDesktop = WshShell.SpecialFolders("Desktop") 'another way to do it
' introduce output directory variable <currentDesktop>\Output
outputDir = objFSO.BuildPath(currentDesktop, "Output")
' create <outputDir> if not exists
If Not objFSO.FolderExists(outputDir) Then
objFSO.CreateFolder outputDir
End If
' example usage in a command
' send command output to <outputDir>\network_config.txt
WshShell.Run "%COMSPEC% /c ipconfig /all >" & Quot(objFSO.BuildPath(outputDir, "network_config.txt"))
现在我的脚本使用绝对文件路径作为 已知 用户桌面上的输出文件夹,但是如果我想让脚本在 用户的桌面上运行怎么办?一个我不知道用户名的用户?
示例 - 这是我当前使用的行。它工作正常,但显然只适用于 "John"。我需要让它与任何潜在的用户名一起使用 - Tom、Dick、Harry 等等。
' Creating log repository
objFSO.CreateFolder "C:\Users\John\Desktop\Output"
如果桌面不在用户通常的位置,最简单但失败的方法是,这可能是公司环境。
dir "%userprofile%\Desktop"
更好的方法是
for /f "skip=2 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"') do set doc=%A
dir "%doc%" /a
(记住批量 %%A
和 %A
如果输入)我认为你的问题是关于批量的。这两种方法在 VBScript 中彼此一样简单。
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%userprofile%\Desktop\Output")
或
bKey = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")
或
strDesktop = WshShell.SpecialFolders("Desktop")
为了避免在评论中进行过多的讨论,我在这里放置了一个 self-explanatory 示例。
' helper function to quote strings
' useful in commands
Function Quot(str)
Quot = Chr(34) & str & Chr(34)
End Function
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
' get user's desktop directory and introduce a variable
currentDesktop = WshShell.SpecialFolders("Desktop") 'another way to do it
' introduce output directory variable <currentDesktop>\Output
outputDir = objFSO.BuildPath(currentDesktop, "Output")
' create <outputDir> if not exists
If Not objFSO.FolderExists(outputDir) Then
objFSO.CreateFolder outputDir
End If
' example usage in a command
' send command output to <outputDir>\network_config.txt
WshShell.Run "%COMSPEC% /c ipconfig /all >" & Quot(objFSO.BuildPath(outputDir, "network_config.txt"))