Jump to content

Recommended Posts

Posted

Hello, I’m trying to achieve this in powershell, getting stuck with logic.

 

If (test path c:\1.txt) {

Do something 1}

 

If (test path c:\2.txt) {

Do something 2}

 

Else {do something 3}

 

The trouble is that it’s always running ‘do something 3’ as one of the if statements is always not satisfied. How can I get this to only run the else if neither file exists? Easy surely..

Posted

I've not had much success to get multiple if statements to work, try if then elseif then else.

 

if (test path c:\1.txt) {

Do something 1}

 

esleif (test path c:\2.txt) {

Do something 2}

 

Else {do something 3}

Posted (edited)

Based on what you said in terms of how it's outputting, it shouldn't do that even if you're using the code logic you mentioned.

 

As if C:\1.txt exists it'll run 1 (and then 3 as 2 doesn't) - Still not what you want I know, but the code you're using will make a difference to how this acts

If C:\2.txt exists it'll run 2 only (Not 3 as 1 doesn't check for 3)

 

So unless you're trying to do something like assigning it to a variable using T/F and assigning it wrong so it always calculates 3 anyway

 

As Davit said above, using else/if should work, but I'd still be curious to see your original code as it shouldn't be possible to get 3 from both outcomes either way in your current logic

 

If (Test-Path C:\1.txt) {
Write-Host "1"
}
ElseIf (Test-Path C:\2.txt) {
Write-Host "2"
}
Else {
Write-Host "3"
}

 

Steve

Edited by Steve21
Posted

Another thought that as the actual paths are a file in either

Program files

Or

Program files (x86)

 

I could just use a Wild card in the test-path command?

If (test-path ‘C:\program files*’)

 

Much easier?

Posted

last thing

 

with the wildcard in place I need to put a get-item so there's an actual file path to work with later on

 

$FilePath =
Get-Item
"‘C:\program files*\1.txt"

$DesiredVersion = '999.999'

 

then I get the version of the file

 

# Get the version from the computer

If(Test-Path $FilePath) {

$Version = [system.diagnostics.fileversioninfo]::GetVersionInfo($FilePath).FileVersionRaw

} else {

# File not found, set version to 0

$Version = '0.0.0.0'

}

 

 

but it it now not setting $Version = '0.0.0.0' if the path is not found, it just errors with

 

"Cannot bind argument to parameter 'Path' because it is null"

 

 

a minor tweak required, can anyone advise?

Posted

I was having similar issues on Powershell, so swapped to a switch statement.. For example. The default is the catch all that runs if none of the previous lines apply.

 

SWITCH ( $RESULT){ test path c:\1.txt { Do something 1 } test path c:\2.txt { Do something 2 } default { Do something 3 }}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...