发生奇数字符串串联

时间:2019-01-28 20:33:39

标签: string powershell concatenation

一个字符串值被两次添加到从函数调用返回的变量中。我不确定如何添加第二个字符串。

代码是从文件中读取机械单元名称,基于单元名称和当前日期创建文件名,并基于机械单元名称和当前日期创建目录。根据文件名(即get filenameunitday和创建的目录,即lcd newdirectoryunit)创建FTP命令文件。

代码:

  • 读取包含机械单元名称的文件的内容。
  • 根据机械单元的名称浏览文件中的单元名称,并将“ Z_”,单元名称,当前日期和扩展名连接起来。然后,将这个串联的字符串分配给变量$FileName
  • BuildDest函数被称为传递$FilName的值
  • 如果该目录不存在($NewDirectory),则BuildDest函数将构建目录
  • 从函数返回变量$NewDirectory并将其分配给$ Dir
  • 将值“ lcd”和$Dir添加到列表对象。
  • 循环完成后,将列表对象的值写入文件,以供以后用作FTP命令文件

我在整个代码中添加了Write-Host cmdlet,以查看变量的值是什么,而看不到问题出在哪里。

Param($FS, $Source, $CurrentDate, $UnitsPath, $JobNum, $Customer)
#FS = E:\TEMP\UDI (destination location)
#Source = source location of the files to be downloaded from the FTP server
#CurrentDate = Current date
#UnitsPath = path to the file containing the mechanical unit names
#JobNum = job number of this job
#Customer = Customer name
try {
    function BuildDest {
        Param([string]$FileName)
        $NewDirectory = $FS + "\" + $Unit + "\" + $Day
        Write-Host "NewDirectory at line 6 " $NewDirectory
        if (!(Test-Path -Path $NewDirectory)) {
            Write-Host "Creating $($NewDirectory)."
            New-Item -ItemType Directory -Path $NewDirectory
        }
        $FileDest = $NewDirectory + "\" + $FileName

        if (Test-Path -Path $FileDest){
            $nr = 1
            if (($FileName).Contains("EJ")) {
                Dir -Path $FileDest | %{Rename-Item $_ -NewName ("duplicate{0}.dat" -f $nr++) | Out-Null}
            } else {
                Dir -Path $FileDest | %{Rename-Item $_ -NewName ("duplicate{0}.cab" -f $nr++) | Out-Null}
            }
        }
        Write-Host "NewDirectory at line 21 " $NewDirectory
        $NewDirectory
    }

    $currentdate = [DateTime]::ParseExact($CurrentDate, "yyyyMMdd", $null)
    $Day = $currentdate.ToString("yyMMdd")

    $Units = Get-Content $UnitsPath
    $command = New-Object System.Collections.Generic.List[System.String]
    $command.Add("cd " + $Source + "`n")

    Write-Host "Building folder structure..."
    foreach ($Unit in $Units) {
        $NewDir = ""

        if (($Unit).Contains("CD")){
            $FileName = "Z_" + $Unit + "_" + $Day + ".dat"
            Write-Host "NewDir at line 39" $NewDir
            $NewDir = BuildDest $FileName
            Write-Host "NewDir at line 41 " $NewDir
        } else {
            $FileName = "Z_" + $Unit + "_" + $Day + ".cab"
            Write-Host "NewDir at line 45" $NewDir
            $NewDir = BuildDest $FileName
            Write-Host "NewDir at line 47 " $NewDir
        }
        Write-Host "NewDir at line 48 " $NewDir
        $command.Add("lcd " + $NewDir + "`n")
        $command.Add("get " + $FileName +"`n")
    }
    $command.Add("quit")
    Write-host $command
    $command | Out-File "E:\DynamicCmd\$($Customer)_$($JobNum).txt" -Encoding ASCII
} catch {
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

命令文件应为:

lcd E:\TEMP\UDI\CD2112\190128
get get Z_CD2112_190128.bat
lcd E:\TEMP\UDI\CD2116\190128
get Z_CD2116_190128.bat

以此类推,但我得到的是:

lcd E:\TEMP\UDI\CD2112\190128 E:\TEMP\UDI\CD2112\190128
get get Z_CD2112_190128.bat
lcd E:\TEMP\UDI\CD2116\190128 E:\TEMP\UDI\CD2116\190128
get Z_CD2116_190128.ej

当我查看日志以查看执行代码时$NewDirectory$NewDir的值时,这就是我看到的:

Building folder structure...
Dir at line 39
NewDirectory at line 6  E:\TEMP\UDI\CD2112\190128
Creating E:\TEMP\UDI\CD2112\190128.
NewDirectory at line 21  E:\TEMP\UDI\CD2112\190128
NewDir at line 41  E:\TEMP\UDI\CD2112\190128 E:\TEMP\UDI\CD2112\190128
NewDir at line 48  E:\TEMP\UDI\CD2112\190128 E:\TEMP\UDI\CD2112\190128
NewDir at line 39
NewDirectory at line 6  E:\TEMP\UDI\CD2116\190128
Creating E:\TEMP\UDI\CD2116\190128.
NewDirectory at line 21  E:\TEMP\UDI\CD2116\190128
NewDir at line 41  E:\TEMP\UDI\CD2116\190128 E:\TEMP\UDI\CD2116\190128
NewDir at line 48  E:\TEMP\UDI\CD2116\190128 E:\TEMP\UDI\CD2116\190128

0 个答案:

没有答案