Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am working on Winpe environment, how to get informations if the disk 0 is partitioned or not to launch specific commands on each case i have tried this, but how to get this works properly in a batch file?

set part_inst=wmic partition list instance

if the disk is not initialized it will return :

No Available Instance.

else if partitions are found it will return :

Index
0
1
2

how to use this information in a batch file to launch the procedure like this

if %part_inst% == "No Available Instance." (
Rem execute sequence_1 of commands if disk is **_not partitioned_**
) else (
Rem execute sequence_2 of commands if disk is **_partitioned_**
)
share|improve this question
    
Look like a solid plan, where exactly does it fail? The only possible problem I see is that there are no quotes around %part_inst% on the IF line. –  EliadTech Mar 31 at 11:18
    
Yes i forget to remove the quotes, the syntax in command prompt would be : if part_inst="No Available Instance." –  madoui20032003 Mar 31 at 12:55
add comment

2 Answers 2

up vote 0 down vote accepted

Expanding Oliver Salzburg's answer, here's an example snippet which works out of the box in Windows PE, where only the find command is available by default. I also changed the actual variable check in order to make it as language-independent as possible:

@echo off

set part_inst=

for /f "usebackq tokens=2 delims==" %%A in (
`wmic partition list instance /format:list ^| find "="`
) do set part_inst=%%A

if not defined part_inst (
REM execute sequence_1 of commands if disk is **_not partitioned_**
) else (
REM execute sequence_2 of commands if disk is **_partitioned_**
)
share|improve this answer
    
when the part_inst is not defined it contains " why? –  madoui20032003 Mar 31 at 14:35
    
@madoui20032003 When a variable is not defined it should be empty, but as long the correct sequence gets executed it's probably not an issue. –  and31415 Mar 31 at 14:39
    
yes the script works fine it was a hidden character in my batch file, sorry. –  madoui20032003 Mar 31 at 14:46
    
@madoui20032003 Good to see you sorted it out. –  and31415 Mar 31 at 14:50
    
PS: The script works fine when there is only one hard disk, and no USB storage must be plugged in. Are there any suggestions to test on disk 0 only? –  madoui20032003 Apr 1 at 9:51
add comment

To store the result of an executed command into a variable, you can't simply assign it using set.

A popular approach is to use for as outlined in the StackOverflow question Windows batch files: How to set a variable with the result of a command?.

You might need to combine that with the suggestion from How can I remove empty lines from wmic output?:

for /f "skip=1 delims=" %%a in (
  'wmic partition list instance' ^| findstr /r /v "^$"'
) do @set part_inst=%%a
share|improve this answer
    
When executed on command prompt, in both cases "new disk" "Partitioned disk", the variable %part_inst% is null? –  madoui20032003 Mar 31 at 12:45
    
Ah, I see the problem now. The output of wmic ends with a new line. So the the last iteration over the output clears the variable. You might need to CALL a subroutine in your batch file to properly parse the output. –  Oliver Salzburg Mar 31 at 13:05
    
yes that is what i was looking for; you can say that the question is answered. –  madoui20032003 Mar 31 at 13:10
    
PS: in windows PE the file findstr.exe is not found, so it is advised to add it before doing the manipulation. –  madoui20032003 Mar 31 at 13:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.