Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果文件丢失,Powershell复制文件失败_Powershell - Fatal编程技术网

如果文件丢失,Powershell复制文件失败

如果文件丢失,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

我想将文件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-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语句的末尾