一个字符串值被两次添加到从函数调用返回的变量中。我不确定如何添加第二个字符串。
代码是从文件中读取机械单元名称,基于单元名称和当前日期创建文件名,并基于机械单元名称和当前日期创建目录。根据文件名(即get filenameunitday和创建的目录,即lcd newdirectoryunit)创建FTP命令文件。
代码:
$FileName
$FilName
的值$NewDirectory
),则BuildDest函数将构建目录$NewDirectory
并将其分配给$ Dir $Dir
添加到列表对象。我在整个代码中添加了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