0

I'm making a program in Batch to easily set your IP settings, but I'm getting an error.

This is causing the problem:

for /f "usebackq tokens=3* skip=2" %%D IN (`reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) do set desktop=%%D

So this provides an error:

powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%desktop%\IP Configurator.lnk');$s.TargetPath='%0';$s.IconLocation = 'C:\Windows\System32\Shell32.dll,273';$s.Save();$bytes = [System.IO.File]::ReadAllBytes('%desktop%\IP Configurator.lnk');$bytes[0x15] = $bytes[0x15] -bor 0x20;[System.IO.File]::WriteAllBytes('%desktop%\IP Configurator.lnk', $bytes);Write-Output 'Shortcut created (or there was an error), press a key to continue'"

But it outputs this error, which results in multiple errors because of it:

Unable to save shortcut "C:\Users\Ryan\Desktop\programs\IP_Configurator\%USERPROFILE%\Desktop\IP Configurator.lnk".
At line:1 char:235
+ ... conLocation = 'C:\Windows\System32\Shell32.dll,273';$s.Save();$bytes  ...
+                                                         ~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException

What I don't get is that it doesn't get the desktop right, and I think it's because it isn't actually calculating what the %USERPROFILE% is, and just literally uses the variable name with the parentheses. How can I get it to just read what the variable value is?

2
  • The path C:\Users\Ryan\Desktop\programs\IP_Configurator\%USERPROFILE%\Desktop\IP Configurator.lnk does not exist; note that %USERPROFILE% contains a full path, so it expands to C:\Users\Ryan\Desktop\programs\IP_Configurator\C:\Users\Ryan\Desktop\IP Configurator.lnk... Commented Mar 29, 2016 at 18:13
  • I know that, I just didn't know why it wasn't expanding Commented Mar 29, 2016 at 22:56

1 Answer 1

1
for /f "usebackq tokens=3* skip=2" %%D IN (`reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) do set desktop=%%D
CALL SET "desktop=%desktop%"
ECHO %desktop%

should cure your problem by resolving the %uerprofile%

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.