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
orx:
?While right now using the
x:
works,.. just want to expand. -
does this webpage help?
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy
-
I don't think there's an
or
operator, as a workaround you can accept both by using two if statements thatgoto
the same:label
.I haven't done much with batch scripts for a while so :man_shrugging:
-
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.
-
@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
orx:
?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 alwaysx:
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.
-
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
-
@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
-
@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!
-
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!" }