Change drive letters from command line via a script

Page 3 of 4 FirstFirst 1234 LastLast

  1. Posts : 52
    Windows 10
    Thread Starter
       #21

    Try3 said:
    Run an additional script first
    Denis
    That was the conclusion I came to, but was hoping there was a more elegant way around it. I'm setting this up for non-technical people to run as I'm moving house and will no longer be able to service their computers.

    This all applies to computers that have a freshly installed copy of Windows. So there are no protected folders or anything else on the drive. The goal was "one script to rule them all". But it looks like that may not be doable.

    So I guess I'll do something like this (myscript.cmd);

    Code:
    @echo off
    copy chgdrive.cmd c:\users\%USERNAME%\Desktop
    C:
    CD c:\users\%USERNAME%\Desktop (Can't use pushd as popd will put me back on a drive whose letter will change)
    call chgdrive I A 
    call chgdrive H I
    I:
    cd \postinstall
    call postinstall.cmd
    del c:\users\%USERNAME%\desktop\myscript.cmd
    Ughh... even that won't work. I will have to get the user to do a number of manual steps first. I'll keep searching for a better solution.
    Last edited by jshs2020; 26 Nov 2023 at 18:21.
      My Computer


  2. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #22

    By calling the chgdrive script instead of running it, you are keeping open the original script.
    You'll have a copy across a master script that runs the two 'versions' of chgdrive & the postinstall and the final line of your original script must be to run [not call] that master script.


    Denis
      My Computer


  3. Posts : 52
    Windows 10
    Thread Starter
       #23

    Try3 said:
    By calling the chgdrive script instead of running it, you are keeping open the original script.
    Correct. Intentional.

    Try3 said:
    You'll have a copy across a master script that runs the two 'versions' of chgdrive
    That was a sample. On some PCs there are 10 Chgdrive calls.

    Try3 said:
    ...the postinstall and the final line of your original script must be to run [not call] that master script.
    Denis
    This is proving more challenging for me than I thought.
    The chgdrive calls are currently about 1,000 lines into postinstall (which is a 12,000 line script).

    So I need to automate the copy of a "start" script to the local drive, run the fist 1,000 lines of postinstall (which really means putting it in the start script), run the chgdrive scripts, then execute postinstall (without using call).

    Call preserves the environment. Running a script without it loses the environment. I will need to change some code because of that. Fortunately, postinstall doesn't use exit (it would have to be exit /b)

    Here's a post I found that offers some insight. cmd - To "Call" or "Not to Call" a batch file? - Stack Overflow
      My Computer


  4. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #24

    Not knowing the details, I do not know if anything is stopping you from copying the whole of the 12,000 line script onto the OS drive then running the whole lot from there.
    So your initiating script will just be reduced to something like
    Code:
    copy %~dp0My12KlineScript.bat %Temp%
    %Temp%\My12KlineScript.bat


    All the best,
    Denis
      My Computer


  5. Posts : 52
    Windows 10
    Thread Starter
       #25

    I could modify it to run from C:

    In it's current form it won't work as there are a dozen path variables set up which are based upon the drive the script is started from. That way I don't have to worry about what drive it's on.

    I guess I could code around that. But that doesn't solve my problem. The chgdrive's are actually 1500 lines down, and I'd have to execute a couple of them initially before running the main script.. it's messy.

    I've been experimenting with different methods, but nothing ideal yet.
      My Computer


  6. Posts : 6,361
    Windows 11 Pro - Windows 7 HP - Lubuntu
       #26

    @Try3

    Denis, what's wrong with my suggestion to use Diskpart (post#19)?
      My Computers


  7. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #27

    Megahertz said:
    Denis, what's wrong with my suggestion to use Diskpart (post#19)?
    Luis,

    Nothing as far as I know.
    But the OP's problem will be, whatever solution is used, that the script controlling the drive letter changes cannot be run from any of the drives being re-assigned.


    All the best,
    Denis
      My Computer


  8. Posts : 6,361
    Windows 11 Pro - Windows 7 HP - Lubuntu
       #28

    Try3 said:
    Luis,

    Nothing as far as I know.
    But the OP's problem will be, whatever solution is used, that the script controlling the drive letter changes cannot be run from any of the drives being re-assigned.


    All the best,
    Denis
    Partitions letters are assigned by the OS that is running, so he must boot from the computer main drive and then change the partitions letters he desire.
    As long he doesn't try to change the C: drive can't see any problem.
      My Computers


  9. Posts : 52
    Windows 10
    Thread Starter
       #29

    Yes, of course I boot from C:

    Yes, the chgdrive script does indeed use diskpart, as was provided by @garlin at post 14 of this thread So nothing is wrong with the diskpart idea.

    The problem is you can't change the drive letter that the script is running from.

    By default, Windows allocates drive letters to removable drives last. So, in my case, the portable drive I use for installing windows, running scripts and installing programs is assigned H:

    However, H: is the drive letter where all of my programming stuff is stored.
    So if I am on drive H: and run chgdrive h i, h no longer exists and the script immediately exits.

    The main script, postinstall.cmd, is a 12,000 liner that does thousands of changes, installs applications, restores user data and much much more. The first 1500 lines of code set up almost 100 variables. Each program to be installed is detected using a for loop. This way the program names don't have to be coded in the script, nor does sensitive data like passwords.

    Because not every client computer is the same I have to cater for multiple versions of various programs. It all works perfectly.
    Except I have to manually set drive letters on every computer before the script can be run.

    Optical drives are always A: and B:, to free up drive letters for local and network drives. On some computers there are only 3 spare letters.

    As I am moving, I will not be local to some of the clients so they will be taking some responsibility for stuff that I can't or am not allowed to do remotely.

    Try it for yourself. Write a script that runs from drive I:. Then within that script, change the drive I: to J: and watch what happens to your script execution.
      My Computer


  10. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #30

    Just a small point but once you run the script from the Windows drive
    %HomeDrive%
    [which will normally be C: but can differ]
    then, if you wish to address a specific drive [such as your originally-provided USB stick containing your installation files] without knowing its drive letter then you can do so if you have given the drive a known label.

    This script retrieves the drive letter of a given drive label.
    It is written as a sub-routine to be called repeatedly within a main script.
    GetDriveLetter-SubRoutine.bat
    - Call it using Call :IdentifyDrive "%Drivelabel%"
    - It returns the variable %DriveLetter%
    I wrote some comments about its use in GetDriveLetter-SubRoutine - my post #5 - ElevenForum
    I started using this sub-routine when Windows went through a phase of losing my A & B drive letter assignments a few years ago [It's possible the problem was confined to exFat-formatted drives]. It seems to be stable now, I set a backup SD card as B: an hour ago and it has survived so far. But I have no confidence that it will survive into next week so I'll stick with my sub-routine.

    Added six hours later - The [Fat32] SD card I set as B: seven hours ago has now lost that assignment and been assigned the next free drive letter available.


    Denis
    Last edited by Try3; 05 Dec 2023 at 09:51.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 15:51.
Find Us




Windows 10 Forums