我有一个使用bcp创建文件的脚本。由于BCP不会导出带有标头的数据,因此我需要将两个文件都加入其中。如果我在另一个脚本中运行代码,它将创建csv文件,但是如果我运行此脚本,则将失败,说明文件不存在,这是下面脚本的最后部分(此处是问题的开始)。 感谢任何帮助。
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction']='Stop'
$bcp = 'c:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe'
$delimiter = '","'
$server = 'BLABLABLA'
$database = 'DMB'
$schema = '.dbo.'
$dbschema = $database + $schema
$atresult = "@result"
$commas = " + ',','') + "
$head1 = 'Use '
$head2 = ' Declare ' # atresult
$head3 = ' varchar(max) select ' # atresult
$head4 = ' = COALESCE(' #atresult $commas
$head5 = 'COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' # tablename
$head6 = ' select ' #atresult
$tables = @('EntityTypes_20181205','FieldFieldTypeInfo')
$output_path = 'D:\Temp\DataModelBuild\'
# Deletes any file in Destination Folder
#if ( (Get-ChildItem $output_path | Measure-Object).Count -ne 0) {}
Get-ChildItem -Path $output_path -Include *.* -File -Recurse | foreach { $_.Delete()}
foreach ($element in $tables)
{
# GET COLUMN NAMES into header file
$outputFile = $output_path + $element + ".header"
$NQ = '"' + $head1 + $database + $head2 + $atresult + $head3 + $atresult + $head4 + $atresult + $commas + $head5 + ''' + $element + ''' + $head6 + $atresult + '"'
#$arglist = "(" + $NQ + " queryout " + $outputFile + ", -S " + $server + " -T -t" + $delimiter + " -c -k)"
$arglist = @('"',
$head1,
$database,
$head2,
$atresult,
$head3,
$atresult,
$head4,
$atresult,
$commas,
$head5,
"'$element'",
$head6,
$atresult,
'"',
" queryout $outputFile",
"-S $server -T -t$delimiter -c -k")
#write-output $arglist
Start-Process -FilePath $bcp -ArgumentList $arglist
#Read-Host
# GET DATA into data file
$outElement = $dbschema + "[" + $element + "]"
$outputFile = $output_path + $element + ".data"
$arglist = @($outElement, "out $outputFile", "-S $server -T -t$delimiter -c -k")
Start-Process -FilePath $bcp -ArgumentList $arglist
#write-output $arglist
#Read-Host
# Merge header and data to csv
#Invoke-Expression "&'$ExtPs1' -FilePath $output_path -FileName $element"
#Write-Output "call "
#Invoke-Expression "&'D:\idna\PedroTemp\cp2csv.ps1' $output_path $element"
#Read-Host
#THE PROBLEM STARTS HERE
$File1 = @("$output_path$element.header")
$File2 = @("$output_path$element.data")
$File3 = @("$output_path$element.csv")
write-output $File1, $File2, $File3
If (-not (Test-Path -Path $File1)) {
Write-Error "File DOES NOT EXIST '$File1'" -ErrorAction Stop
} else {
Write-Host "File EXIST"
}
Add-Content $File3 -value (Get-Content $File1)
Add-Content $File3 -value (Get-Content $File2)
#write-output $File1, $File2, $File3 -ErrorAction Stop
Read-Host
}
我的问题是为什么脚本没有检测到文件? 字符串有问题吗?
答案 0 :(得分:1)
路径字符串未正确构建。在字符串中使用成员时,必须用$()
括住变量和成员。
$File1 = "$output_path$($element.header)"