0

Right now my script uses an absolute filepath for the output folder on the desktop of a known user, but what if I want to allow the script to work on the desktop of a user whose username I do not know?

Example - here's the line I'm currently using. It works fine, but will obviously only work for "John". I need to make it work with any potential username - Tom, Dick, Harry, and so on and so forth.

' Creating log repository
objFSO.CreateFolder "C:\Users\John\Desktop\Output"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
wyatt8919
  • 13
  • 1
  • 4
  • see http://stackoverflow.com/a/8051766/893670 – Kul-Tigin Feb 08 '17 at 23:54
  • @Kul-Tigin So, doing it this way, the script would look like this? - `Set objFolder = objShell.Namespace(&H10&\Output)` – wyatt8919 Feb 09 '17 at 00:11
  • close. `objFSO.CreateFolder objFSO.BuildPath(objShell.Namespace(&H10&).Self.Path, "Output")` – Kul-Tigin Feb 09 '17 at 00:13
  • @Kul-Tigin So if I have lots of different functions and outputs pointing to this folder, let's say, using `objShell.run`, can I then reference it like so? - `objShell.run "%COMSPEC% /c ipconfig /all > objShell.Namespace(&H10&).Self.Path, "Output" "network_config.txt" ` – wyatt8919 Feb 09 '17 at 00:19

2 Answers2

0

The easy way but fails if the desktop is not in the usual place for a user is, this may be coporate environments.

dir "%userprofile%\Desktop"

A better way is

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

(Remember %%A in a batch and %A if typing) I thought your question was about batch. Both methods are as easy as each other in VBScript.

set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%userprofile%\Desktop\Output")

or

bKey = WshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop")

or

     strDesktop = WshShell.SpecialFolders("Desktop")
Freddie
  • 269
  • 1
  • 4
  • I'm very new to vbscript, so that first method seems more within my capability than the second, although once I get all the functionality I need built into the script, that second method seems like a good idea. If I wanted to implement the first method, would it look something like this? `objFSO.CreateFolder "%userprofile%\Desktop\Output"` – wyatt8919 Feb 09 '17 at 00:02
0

To avoid extended discussions in comments, here I place a self-explanatory example.

' 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"))
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • I have it working but I don't understand why the Quot function is necessary. Also, if I want to copy files to this newly-made folder, how do I reference it? I'm struggling with getting it to copy stuff to that folder. Here's what I'm using now - `objFSO.CopyFile "C:\Windows\System32\winevt\Logs\Security.evtx", outputdir)` I'm getting a Permission Denied error, despite having ran it in a command prompt as admin – wyatt8919 Feb 09 '17 at 01:37
  • @wyatt8919 feel free to ask a new question for it or start here https://stackoverflow.com/search?q=%5Bvbscript%5D+permission+denied – Kul-Tigin Feb 09 '17 at 14:13