script0207
script0207

Reputation: 385

check for respective elements in arrays in powershell

I have two arrays with three elements(file name parts) each. I need to join first element of first array and first element of second array and test if is not null and if the combination(file name) exists and like wise i need to do it for other two elements in a same manner.

$file_nameone_array = ( table, chair, comp)
$file_nametwo_array = ( top, leg , cpu)
foreach ($input_file in $file_nameone_array) {
foreach ($input_rev in $file_nametwo_array) {
$path = "D:\$input_file-$input_rev.txt"
    If (test-path $path -pathtype leaf) {
        write-host "$path exists and not null"}
    else{ 
        write-host "$path doesnot exist"
        exit 1}

I expect to test for "table-top.txt", "chair-leg.txt" , "comp-cpu.txt" whereas my code checks for "table-leg.txt" and exits saying table-leg.txt doesnot exist.

Upvotes: 0

Views: 800

Answers (3)

mclayton
mclayton

Reputation: 9975

If you change your example slightly to just display the $path variable ike this:

$file_nameone_array = @( "table", "chair", "comp" )
$file_nametwo_array = @( "top", "leg" , "cpu" )
foreach ($input_file in $file_nameone_array) {
    foreach ($input_rev in $file_nametwo_array) {
        $path = "D:\$input_file-$input_rev.txt"
        write-host $path
    }
}

you get this output

D:\table-top.txt
D:\table-leg.txt
D:\table-cpu.txt
D:\chair-top.txt
D:\chair-leg.txt
D:\chair-cpu.txt
D:\comp-top.txt
D:\comp-leg.txt
D:\comp-cpu.txt

so you can see why it's looking for "D:\table-top.txt" as the first file.

what you can do instead is this:

$file_nameone_array = @( "table", "chair", "comp" )
$file_nametwo_array = @( "top", "leg" , "cpu" )
for( $index = 0; $index -lt $file_nameone_array.Length; $index++ ) {
    $input_file = $file_nameone_array[$index];
    $input_rev  = $file_nametwo_array[$index];
    $path       = "D:\$input_file-$input_rev.txt"
    write-host $path
}

and now you get this output

D:\table-top.txt
D:\chair-leg.txt
D:\comp-cpu.txt

You can replace the write-host with your original file checking logic and you should get the behaviour you were after.

Note - This requires your arrays to be exactly the same length, so you might need to put some error handling in before this bit of code in your script.

Upvotes: 0

Ambrose Leung
Ambrose Leung

Reputation: 4215

This sounds like a coding problem for a homework assignment (i.e. something you should figure out), so I'll just give you hints instead of the answer.

  • Your elements of array need to be wrapped in quotes.
  • Use Write-Output $path to see what you're actually checking for.
  • Use a regular for loop
  • This is the syntax to write output of the first element in the array: Write-Output "$($file_nameone_array[0])"

Hopefully you can get this answer from this.

Upvotes: 1

Nirav Mistry
Nirav Mistry

Reputation: 989

You can try this way :

$file_nameone_array = ( "table","chair", "comp") # INITILIZING ARRAY
$file_nametwo_array = ( "top", "leg", "cpu") # INITILIZING ARRAY

if ($file_nameone_array.Count -ne $file_nametwo_array.Count )  # CPMPARING BOTH ARRAY'S SIZE
{
    Write-Error "Both array must have same size..." # THROW ERROR
    exit # EXIT
}

for($ind = 0 ; $ind -lt $file_nameone_array.Count ; $ind++) # FOR LOOP 0 TO ARRAY'S LENGTH - 1
{
    $path = "D:\\" + $file_nameone_array[$ind] + "-" + $file_nametwo_array[$ind] + ".txt" # COMBINING BOTH ELEMENTS
    If (test-path $path -pathtype leaf) # CHECKING PATH EXIST OR NOT
    {
        write-host "$path exists and not null" # PRINT PATH EXIST
    } # END OF IF
    else # ELSE 
    {
        Write-Error "$path doesnot exist" # THROW ERROR : FILE NOT EXIST
        exit 1 # EXIT SCRIPT
    } # END OF ELSE

} # END OF FOR LOOP

Upvotes: 0

Related Questions