根据文件头将文件复制到其他位置

时间:2019-03-21 08:41:47

标签: powershell

我试图获得相同的结果,但是出现了一些我自己无法解决的奇怪行为。

它仅过滤掉第一个if,然后将其余部分视为else。不管我放入多少if...else,然后它仍然只过滤掉第一个if,然后将其他人视为else

我也尝试过移动标头,因此通常被视为else的标头将是第一个if,但仍只运行第一个if

[string] $FileDirectory = "C:\temp\Move by header\input";
[string] $OutputPathHeat = "C:\temp\Move by header\HeatMeter";
[string] $OutputPathWater = "C:\temp\Move by header\WaterMeter";
[string] $OutputPathOther = "C:\temp\Move by header\Other";

foreach ($FilePath in Get-ChildItem $FileDirectory | Select-Object -     
ExpandProperty                 
FullName)
{
    [string] $Header = Get-Content $FilePath -First 1

    if ($Header -match '#serial-number;device-identification;created;value- 
                   data- 
                   count;act-duration,second(s),inst-value,0,0,0;avg- 
                   duration,second(s),inst-value,0,0,0;energy,Wh,inst- 
                   value,0,0,0;volume,m3,inst-value,0,0,0.*') {
        Move-Item $FilePath $OutputPathHeat
    } elseif ($Header -match '#serial-number;device- 
     identification;created;value-data- 
                       count;fabrication-no,,inst- 
     value,0,0,0;datetime,,inst- 
                       value,0,0,0;volume,m3,inst-value,0,0,0.*') {
        Move-Item $FilePath $OutputPathWater
    } else {
        Move-Item $FilePath $OutputPathOther
    }
}

1 个答案:

答案 0 :(得分:1)

尽管您可能没有向我们显示足够多的可能的标题,但我认为您正在尝试在整个第一行中使用<input type="text" name={this.state.editBookData.title} value={this.state.toEditBookData.title} onChange={e => { const { value } = e.target; this.setState(prevState => { return { toEditBookData: { ...prevState.toEditBookData, title: value } }; }); }} />

此外,在您的代码中,您很随意地分解了字符串甚至命令,使得代码和正则表达式确实不可靠。 最好只关注将一个文件与另一个文件区分开并匹配的差异

看看下面的代码:

-match

使用$RootDirectory = 'C:\temp\Move by header' $InputDirectory = Join-Path -Path $RootDirectory -ChildPath 'input' $OutputPathHeat = Join-Path -Path $RootDirectory -ChildPath 'HeatMeter' $OutputPathWater = Join-Path -Path $RootDirectory -ChildPath 'WaterMeter' $OutputPathOther = Join-Path -Path $RootDirectory -ChildPath 'Other' # get an array of Full path and filenames of the files in the input directory. # because you want files only, add the '-File' switch. # if you're on PowerShell version below 3.0, use: # (Get-ChildItem $InputDirectory | Where-Object { !$_.PSIsContainer }) foreach ($FilePath in (Get-ChildItem $InputDirectory -File) | Select-Object -ExpandProperty FullName) { $Header = Get-Content $FilePath -First 1 # test for a string in the header line that distincts it from the other files if ($Header -match ';energy,Wh,') { # the substring ';energy,Wh,' defines this file as a 'HeatMeter' file Move-Item -Path $FilePath -Destination $OutputPathHeat } elseif ($Header -match ';fabrication-no,,') { # the substring ';fabrication-no,,' defines this file as a 'WaterMeter' file Move-Item -Path $FilePath -Destination $OutputPathWater } else { # if both key substrings above did not match, move to the 'Other' directory Move-Item -Path $FilePath -Destination $OutputPathOther } } 命令代替使用上面的if..elseif..else构造,可以使您的代码更具可读性,还可以使您更轻松地在两者之间添加额外的测试。
除此之外,switch还具有一个switch参数,因此不需要每次都写出-Regex。您可以在这里阅读所有内容:about_Switch

上面的if ($Header -match ...块可以写成:

if..elseif..else

注意:如果要匹配的子字符串具有在正则表达式中具有特殊含义的字符,请确保使用语法# test for a string in the header line that distincts it from the other files switch -Regex ($Header) { # the substring ';energy,Wh,' defines this file as a 'HeatMeter' file ';energy,Wh,' { Move-Item -Path $FilePath -Destination $OutputPathHeat; break } # the substring ';fabrication-no,,' defines this file as a 'WaterMeter' file ';fabrication-no,,' { Move-Item -Path $FilePath -Destination $OutputPathWater; break } # if both key substrings above did not match, move to the 'Other' directory default { Move-Item -Path $FilePath -Destination $OutputPathOther } } {p} {3}}