如果文件丢失,Powershell复制文件失败
我想将文件1、文件2、文件3、文件4和文件5中的数据附加到finalfile 所以副本是如果文件丢失,Powershell复制文件失败,powershell,Powershell,我想将文件1、文件2、文件3、文件4和文件5中的数据附加到finalfile 所以副本是 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-conte
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)
但是如果文件没有退出,它将给出一个错误,那么如果文件中的某些部分丢失,如何重写代码呢?例如,文件2丢失,脚本仍将继续为文件3添加内容。使用try-catch或设置$ErrorActionPreference变量处理错误。使用try-catch或设置$ErrorActionPreference变量处理错误。为什么不尝试类似的操作
if(Test-Path $file2){ add-content -path $finalfile -value (get-content $file2)}
测试路径将测试它是否能够找到目录/文件并返回true/false。如果if语句找到该文件,它将执行addcontent命令。如果找不到该文件,则If不执行任何操作为什么不尝试类似的操作
if(Test-Path $file2){ add-content -path $finalfile -value (get-content $file2)}
测试路径将测试它是否能够找到目录/文件并返回true/false。如果if语句找到该文件,它将执行addcontent命令。如果找不到该文件,则If不执行任何操作特别地将-ErrorAction SilentlyContinue添加到每个add content语句的末尾特别地将-ErrorAction SilentlyContinue添加到每个add content语句的末尾