ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Windows Batch: Select Drive

    IT Discussion
    6
    9
    319
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • gjacobseG
      gjacobse
      last edited by

      At some point I plan to move a script to Powershell, until then:

      In the script I am working on, it is set to ask for the drive to back up to, you currently enter x:.

      Is there syntax that would allow for either x or x: ?

      While right now using the x: works,.. just want to expand.

      1 1 Reply Last reply Reply Quote 0
      • WrCombsW
        WrCombs
        last edited by

        does this webpage help?

        https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy

        1 Reply Last reply Reply Quote 0
        • ObsolesceO
          Obsolesce
          last edited by Obsolesce

          I don't think there's an or operator, as a workaround you can accept both by using two if statements that goto the same :label.

          I haven't done much with batch scripts for a while so :man_shrugging:

          1 Reply Last reply Reply Quote 0
          • J
            JasGot
            last edited by

            Can you post the line or two that gathers the input? Set /p would ignore the colon, and Choice doesn't allow colons, so I need to see your approach to provide you with a solution.

            ObsolesceO 1 Reply Last reply Reply Quote 0
            • 1
              1337 @gjacobse
              last edited by 1337

              @gjacobse said in Windows Batch: Select Drive:

              At some point I plan to move a script to Powershell, until then:

              In the script I am working on, it is set to ask for the drive to back up to, you currently enter x:.

              Is there syntax that would allow for either x or x: ?

              While right now using the x: works,.. just want to expand.

              It would be very confusing to enter x when the script is asking for a drive.
              Drive letters are always x: in windows. It's the colon than makes it a drive and not just a directory or file.

              If you enter something like this in a script, you might want to put a check in the script that the drive is actually available.

              1 Reply Last reply Reply Quote 0
              • J
                JasGot
                last edited by JasGot

                Actually Set /p does work:

                echo off 
                
                SET /P _inputname= Please enter an input:
                IF "%_inputname%"=="x" GOTO :they_typed_x
                IF "%_inputname%"=="x:" GOTO :they_typed_xcolon
                ECHO Invalid Answer... 
                GOTO :end
                :they_typed_x
                Echo Should be x =  %_inputname%
                GOTO :end
                :they_typed_xcolon
                Echo Should be x: = %_inputname%
                :end
                
                1 Reply Last reply Reply Quote 0
                • ObsolesceO
                  Obsolesce @JasGot
                  last edited by

                  @JasGot said in Windows Batch: Select Drive:

                  Can you post the line or two that gathers the input? Set /p would ignore the colon, and Choice doesn't allow colons, so I need to see your approach to provide you with a solution.

                  Was going to say it worked for me but looks like I replied too late with an example:

                  @echo off
                  echo What are drive you is want?
                  set /p drive=""
                  cls
                  if "%drive%" == "x" GOTO AWWYEAH
                  if "%drive%" == "x:" GOTO AWWYEAH
                  echo FAIL!
                  pause
                  exit
                  :AWWYEAH
                  echo YEAH! [%drive%]
                  pause
                  
                  J 1 Reply Last reply Reply Quote 0
                  • J
                    JasGot @Obsolesce
                    last edited by

                    @Obsolesce said in Windows Batch: Select Drive:

                    Was going to say it worked for me but looks like I replied too late with an example:

                    That's usually me! I was so happy to see I corrected myself before someone else did! 🙂 I like how our Set /P lines show two different ways to capture the input!

                    1 Reply Last reply Reply Quote 0
                    • GreyG
                      Grey
                      last edited by Grey

                      I was a little bored, so here you go:

                      ##  Example drive letter selection
                      # Only allow X, Y or <enter>
                      do{Write-Host -NoNewline -ForegroundColor Cyan "What drive letter do you prefer as a backup?"
                      $letter = (Read-Host -prompt "(X) or Y?").ToUpper()} while ($letter -notin @('x','y',''))
                      
                      # This sets the default to X, allowing the user to simply press enter if X is OK.
                      if($letter -eq ''){$letter = 'X'}
                      # Now set the drive letter.
                      New-PSDrive –Name $letter –PSProvider FileSystem –Root “\\server\backup” –Persist -ErrorAction SilentlyContinue
                      

                      Basic drive letter set is done, but we can be more complicated:

                      # Do actions based on a user that has an network location from IP.
                      $ipaddr = Get-NetIPAddress|?{$_.SuffixOrigin -eq "Dhcp" -and $_.AddressState -like "*preferred*"}|select -ExpandProperty ipaddress
                      # Set the preferred drive letter.
                      $letter = X
                      
                      If($ipaddr -like '192.168.0.*'){
                      New-PSDrive –Name $letter –PSProvider FileSystem –Root “\\alpha\backup” –Persist -ErrorAction SilentlyContinue
                      Write-Host -ForegroundColor Green "You're in Alpha!`nBackup drive connected to Alpha site.`nHave a sumptuous day!"
                      }
                      # if you have more networks, insert here with more if statements.
                      Else{
                      New-PSDrive –Name $letter –PSProvider FileSystem –Root “\\bravo\backup” –Persist -ErrorAction SilentlyContinue
                      Write-Host -ForegroundColor Green "You're in Bravo!`nBackup drive connected to Bravo site.`nHave a sumptuous day!"
                      }
                      
                      1 Reply Last reply Reply Quote 1
                      • 1 / 1
                      • First post
                        Last post