-
Feb 7th, 2025, 08:37 AM
#1
Thread Starter
New Member
Random Photo to Drive Letter
I'm very new to programming, I appreciate some help with a command script I have:
Code:
echo off
setlocal enabledelayedexpansion
set folder="c:/photos"
set count=1
set x=1
for /r "%folder%" %%a in (*.ico) do (
set !count!=%%~a
set /a count+=1
)
set /a x="%random% %% count"
set chosen=!%x%!
Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\C\DefaultIcon" /d "%chosen%" /f >NUL
This will add a set of random icons to an assigned drive letter to \C\
I need to call a vbs script for upto 26 drives, each from A - Z and with random Photos each and no repeated photos on the same drive letter
currently only works as on %chosen% as above command script
looking to replace this drive number A-Z to the registry HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\A - Z\DefaultIcon
example,
Drive Letter
A > Random photo1
B > Random photo2
...
Z > Random photo26
Currently trying to do this with a "Reg add ..." but will not work with Command scripts (I'm bad at scripting!)
Any help to write a vbs script to be called to replace the Reg add command.
Many thanks
Last edited by dday9; Feb 7th, 2025 at 05:06 PM.
-
Feb 7th, 2025, 03:19 PM
#2
Thread Starter
New Member
Re: Random Photo to Drive Letter
I was able to find this extra command script:
Code:
echo off
setlocal enabledelayedexpansion
set folder="c:/photos"
set count=1
set x=1
for /r "%folder%" %%a in (*.ico) do (
set !count!=%%~a
set /a count+=1
)
set /a x="%random% %% count"
set chosen=!%x%!
for /L %%C in (0x44,1,0x5A) do (
cmd /C exit %%C
for /F %%D in ('
forfiles /P "%~dp0." /M "%~nx0" /C "cmd /C echo 0x%%=ExitCode:~-2%%"
') do Reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\%%D\DefaultIcon" /d "%chosen%" /f
)
Now I've tried to combine them together but with no luck... anyone with coding skills can help? ty
Last edited by dday9; Feb 7th, 2025 at 05:07 PM.
-
Feb 7th, 2025, 05:05 PM
#3
Re: Random Photo to Drive Letter
I apologize because I can tell you put a lot of effort into describing your problem, but I don't fully understand what it is that you're wanting to accomplish.
P.S. - I updated your posts, but there are [code][/code] tags that format code and should be used over [quote][/quote] tags.
-
Feb 10th, 2025, 11:03 AM
#4
Thread Starter
New Member
-
Feb 10th, 2025, 12:46 PM
#5
Re: Random Photo to Drive Letter
OK, that makes more sense. Typically, what you'd do in this situation is:
- Store the files in a collection
- Randomly order the collection
- Loop over the collection
Think of it like shuffling a deck of cards where each card represents an individual photo, the deck represents the collection, and shuffling represents randomly ordering.
Take a look at this example, you will need to run it as an administrator:
Code:
Option Explicit
Dim fso, folder, file, filesArray(), count, i, j, temp, letter, filePath
Dim WshShell
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Dim folderPath
folderPath = "C:\photos"
If Not fso.FolderExists(folderPath) Then
WScript.Echo "Folder not found: " & folderPath
WScript.Quit
End If
Set folder = fso.GetFolder(folderPath)
count = 0
For Each file In folder.Files
ReDim Preserve filesArray(count)
filesArray(count) = file.Path
count = count + 1
Next
If count = 0 Then
WScript.Echo "No files found in " & folderPath
WScript.Quit
End If
Randomize
Dim ub
ub = UBound(filesArray)
For i = ub To 1 Step -1
j = Int((i + 1) * Rnd())
temp = filesArray(i)
filesArray(i) = filesArray(j)
filesArray(j) = temp
Next
Dim iterations
iterations = 26
If count < iterations Then iterations = count
For i = 0 To iterations - 1
letter = Chr(Asc("A") + i)
filePath = filesArray(i)
Dim regKey
regKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\" & letter & "\DefaultIcon\"
On Error Resume Next
WshShell.RegWrite regKey, filePath, "REG_SZ"
If Err.Number <> 0 Then
WScript.Echo "Error writing registry key for drive " & letter & ": " & Err.Description
Err.Clear
Else
WScript.Echo "Set drive " & letter & " icon to " & filePath
End If
On Error GoTo 0
Next
I generated the code using a bit of ChatGPT prompting. It is untested, namely because I don't want to futz with my registry, but going over it, it looks like it should work well.
-
Feb 12th, 2025, 06:17 AM
#6
Thread Starter
New Member
Re: Random Photo to Drive Letter
 Originally Posted by dday9
OK, that makes more sense. Typically, what you'd do in this situation is:
- Store the files in a collection
- Randomly order the collection
- Loop over the collection
Think of it like shuffling a deck of cards where each card represents an individual photo, the deck represents the collection, and shuffling represents randomly ordering.
Take a look at this example, you will need to run it as an administrator:
Code:
Option Explicit
Dim fso, folder, file, filesArray(), count, i, j, temp, letter, filePath
Dim WshShell
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Dim folderPath
folderPath = "C:\photos"
If Not fso.FolderExists(folderPath) Then
WScript.Echo "Folder not found: " & folderPath
WScript.Quit
End If
Set folder = fso.GetFolder(folderPath)
count = 0
For Each file In folder.Files
ReDim Preserve filesArray(count)
filesArray(count) = file.Path
count = count + 1
Next
If count = 0 Then
WScript.Echo "No files found in " & folderPath
WScript.Quit
End If
Randomize
Dim ub
ub = UBound(filesArray)
For i = ub To 1 Step -1
j = Int((i + 1) * Rnd())
temp = filesArray(i)
filesArray(i) = filesArray(j)
filesArray(j) = temp
Next
Dim iterations
iterations = 26
If count < iterations Then iterations = count
For i = 0 To iterations - 1
letter = Chr(Asc("A") + i)
filePath = filesArray(i)
Dim regKey
regKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\" & letter & "\DefaultIcon\"
On Error Resume Next
WshShell.RegWrite regKey, filePath, "REG_SZ"
If Err.Number <> 0 Then
WScript.Echo "Error writing registry key for drive " & letter & ": " & Err.Description
Err.Clear
Else
WScript.Echo "Set drive " & letter & " icon to " & filePath
End If
On Error GoTo 0
Next
I generated the code using a bit of ChatGPT prompting. It is untested, namely because I don't want to futz with my registry, but going over it, it looks like it should work well.
Great, it works... thanks
-
Feb 12th, 2025, 07:25 AM
#7
Re: Random Photo to Drive Letter
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\ C]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\ C\DefaultIcon]
@=" %SystemRoot%\\system32\\imageres.dll,1"
On a sidenote:
look at the 3rd line in above quote closely.
Especially this part:
"imageres.dll,1"
Have a guess, what the "1" stands for......
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 12th, 2025, 09:30 AM
#8
Thread Starter
New Member
Re: Random Photo to Drive Letter
 Originally Posted by Zvoni
On a sidenote:
look at the 3rd line in above quote closely.
Especially this part:
"imageres.dll,1"
Have a guess, what the "1" stands for......
thats an icon located in imageres
-
Feb 13th, 2025, 01:53 AM
#9
Re: Random Photo to Drive Letter
 Originally Posted by LazyFox
thats an icon located in imageres
Correct. Specifically it's the Index of the Icon within that dll
And noone is preventing you to compile your own dll.
In a nutshell: You could write all 26 entries into the Registry, with pretty much the same Key
Code:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\C]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\C\DefaultIcon]
@="c:\\Icons\\MyIcons.dll,37"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\D]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\D\DefaultIcon]
@="c:\\Icons\\MyIcons.dll,17"
and then it's just pulling random numbers
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 13th, 2025, 09:25 AM
#10
Thread Starter
New Member
Re: Random Photo to Drive Letter
As I know, using a folder to store the icons (as many as i want) and applying to registry is the way I've worked with.
I know that editing imageres.dll is not the way as windows will not allow system resources to be changed and also stored icons is for the systems use, as this will break the system icons...
With your advice, can you elaborate with a another way todo this?
-
Feb 13th, 2025, 09:35 AM
#11
Re: Random Photo to Drive Letter
 Originally Posted by LazyFox
As I know, using a folder to store the icons (as many as i want) and applying to registry is the way I've worked with.
I know that editing imageres.dll is not the way as windows will not allow system resources to be changed and also stored icons is for the systems use, as this will break the system icons...
With your advice, can you elaborate with a another way todo this?
I meant to CREATE your own DLL containing the icons.
NOT editing imageres.dll
https://www.youtube.com/watch?v=SDdiEASQJJU
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 13th, 2025, 11:43 AM
#12
Thread Starter
New Member
Re: Random Photo to Drive Letter
 Originally Posted by Zvoni
OK thanks, I got you.
After checking the guide, its a great way to store icons in a single file, but randomly plucking the icons to use is another bit of scripting process... mmm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|