Results 1 to 12 of 12

Thread: Random Photo to Drive Letter

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    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.

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    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.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,064

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    Re: Random Photo to Drive Letter

    I basically wanted to automate a script to choose any random photo icon's (no repeats) to add to this registry:

    KLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\A\DefaultIcon
    .
    .
    KLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\Z\DefaultIcon

    From Drive A-Z with a DefaultIcon of a random photo icon.

    The 2 scripts works independently, I'm not sure how to combine them as one scripy for automation,

    So Drive Letter:
    A > Random photo1
    B > Random photo2
    C > Random photo150
    ... and so on...
    Z > Random photo26

    [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"

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\D]
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\D\DefaultIcon]
    @="c:/photos/photo99"

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\Z]
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\Z\DefaultIcon]
    @="c:/photos/photo26"

    Hope this make sense...

    Name:  Dregs.jpg
Views: 1460
Size:  30.7 KB
    Attached Images Attached Images  

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,064

    Re: Random Photo to Drive Letter

    OK, that makes more sense. Typically, what you'd do in this situation is:
    1. Store the files in a collection
    2. Randomly order the collection
    3. 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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    Re: Random Photo to Drive Letter

    Quote Originally Posted by dday9 View Post
    OK, that makes more sense. Typically, what you'd do in this situation is:
    1. Store the files in a collection
    2. Randomly order the collection
    3. 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

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,986

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    Re: Random Photo to Drive Letter

    Quote Originally Posted by Zvoni View Post
    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

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,986

    Re: Random Photo to Drive Letter

    Quote Originally Posted by LazyFox View Post
    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    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?

  11. #11
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,986

    Re: Random Photo to Drive Letter

    Quote Originally Posted by LazyFox View Post
    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

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2025
    Posts
    7

    Re: Random Photo to Drive Letter

    Quote Originally Posted by Zvoni View Post
    I meant to CREATE your own DLL containing the icons.
    NOT editing imageres.dll
    https://www.youtube.com/watch?v=SDdiEASQJJU
    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
  •  



Click Here to Expand Forum to Full Width