0
votes

I want to append data from file1,file2,file3,file4 and file5 to finalfile.

so the copy would be

Copy-Item $file1 $finalfile
add-content -path $finalfile-value (get-content $file2)
add-content -path $finalfile-value (get-content $file3)
add-content -path $finalfile-value (get-content $file4)
add-content -path $finalfile-value (get-content $file5)

But if the file does not exits it will give an errors, so how can rewrite the code in case if some of the file is missing example file2 is missing, the script will still continue add content for file3 onward.

2

2 Answers

2
votes

why not try something like this?

if(Test-Path $file2){ add-content -path $finalfile -value (get-content $file2)}

Test-Path will test if it can find the directory/file and return a true/false. the if statement will then execute the add-content command if it found the file. If the file is not found the if does nothing

1
votes

Handle your errors either with try catch or set the $ErrorActionPreference variable.