帮助将数据库查询结果插入到 CSV 文件中

发布于 2024-10-08 23:03:23 字数 1681 浏览 0 评论 0原文

我有一个表,其中包含指向文件服务器上存储的文件的信息。我正在尝试编写一个 powershell 脚本来验证表中的每条记录在服务器上是否都有与其关联的物理文件(如果它不写入 CSV 文件)。我对 csv 部分没有太多运气,希望你能提供帮助?

这是我到目前为止所拥有的(请让我知道是否有更好的方法来做到这一点,我是 powershell 的新手)。如果测试路径无法找到有问题的文件,我只想将记录添加到 csv。

[Reflection.Assembly]::LoadFile("C:\ora10g\odp.net\bin\2.x\Oracle.DataAccess.dll")
$connectionString = "Data Source=XXXXXX;User Id=XXXX;Password=XXXXXXXX;"
$connection = New-Object Oracle.DataAccess.Client.OracleConnection($connectionString)
$connection.Open()
$queryString = "SELECT Key, Sequence, Type, File FROM FileTable WHERE Type IN (2, 5)"

$command = new-Object Oracle.DataAccess.Client.OracleCommand($queryString, $connection)
$mediaObjRS = $command.ExecuteReader()

# Loop through recordset
while ($mediaObjRS.read()) {

   # Assign variables from recordset.
   $objectKey = $mediaObjRS.GetString(0)
   $objectSeq = $mediaObjRS.GetDecimal(1)
   $objectType = $mediaObjRS.GetDecimal(2)
   $objectFileName = $mediaObjRS.GetString(3)

        # Check if file exists based on filetype.
        if($objectType -eq 2){
            # Type 2 = OLE
            $fileLocation = "\\fileserver\D$\files\" + $objectFileName
        }elseif($objectType -eq 5){ 
             # Type 5 = FILE
             $fileLocation = $objectFileName        
        }
        $fileExists = Test-Path $fileLocation
        if($fileExists -eq $False){
                #Write to output file
                $objectKey | Export-Csv missingfiles.csv
                $objectSeq | Export-Csv missingfiles.csv
                $objectType | Export-Csv missingfiles.csv
                $objectFileName | Export-Csv missingfiles.csv
        }
}

$connection.Close()

I have a table that contains information pointing to files stored on a file server. I'm trying to write a powershell script to verify that each record in the table has a physical file associated with it on the server, if it does not write to the CSV file. I'm not having much luck wit the csv portion and was hoping you could help?

Here is what I have so far (please let me know if there are better ways to do this, Im brand new to powershell). I only want to add records to the csv if the test-path fails to find the file in question.

[Reflection.Assembly]::LoadFile("C:\ora10g\odp.net\bin\2.x\Oracle.DataAccess.dll")
$connectionString = "Data Source=XXXXXX;User Id=XXXX;Password=XXXXXXXX;"
$connection = New-Object Oracle.DataAccess.Client.OracleConnection($connectionString)
$connection.Open()
$queryString = "SELECT Key, Sequence, Type, File FROM FileTable WHERE Type IN (2, 5)"

$command = new-Object Oracle.DataAccess.Client.OracleCommand($queryString, $connection)
$mediaObjRS = $command.ExecuteReader()

# Loop through recordset
while ($mediaObjRS.read()) {

   # Assign variables from recordset.
   $objectKey = $mediaObjRS.GetString(0)
   $objectSeq = $mediaObjRS.GetDecimal(1)
   $objectType = $mediaObjRS.GetDecimal(2)
   $objectFileName = $mediaObjRS.GetString(3)

        # Check if file exists based on filetype.
        if($objectType -eq 2){
            # Type 2 = OLE
            $fileLocation = "\\fileserver\D$\files\" + $objectFileName
        }elseif($objectType -eq 5){ 
             # Type 5 = FILE
             $fileLocation = $objectFileName        
        }
        $fileExists = Test-Path $fileLocation
        if($fileExists -eq $False){
                #Write to output file
                $objectKey | Export-Csv missingfiles.csv
                $objectSeq | Export-Csv missingfiles.csv
                $objectType | Export-Csv missingfiles.csv
                $objectFileName | Export-Csv missingfiles.csv
        }
}

$connection.Close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2024-10-15 23:03:23

每次写入 Missingfiles.cs 都会破坏前一个文件。另外,此 cmdlet 面向保存对象,其中每个属性代表逗号分隔值之一。

最简单的方法是手动写入(追加)到 csv 文件:

if(!$fileExists) {
    #Write to output file
    "`"$objectKey`",`"$objectSeq`",`"$objectType`",`"$objectFileName`" >> foo.csv
}

“灵活”的 PowerShell 方法是为每个文件创建一个对象,然后将它们放入一个数组中,完成后,将该对象数组导出到 CSV 文件,例如:

$files = @() # Initialize array outside loop
...
if (!$fileExists) {
    $obj = new-object -psobject -prop @{
               Key = $objectKey
               Seq = $objectSeq
               Type = $objectType
               FileName = $objectFileName
           }
    $files += obj
}
...
# outside of loop, export array of objects to csv
$files | Export-Csv missingfiles.csv

Each time you write to the missingfiles.cs you clobber the previous one. Plus this cmdlet is oriented towards saving objects where each property represents one of the comma separated values.

The simplest way to do this is to manually write (append) to the csv file:

if(!$fileExists) {
    #Write to output file
    "`"$objectKey`",`"$objectSeq`",`"$objectType`",`"$objectFileName`" >> foo.csv
}

The "slick" PowerShell way to do it would be to create an object for each file and then put those in an array and when you're done, export that array of object to a CSV file e.g.:

$files = @() # Initialize array outside loop
...
if (!$fileExists) {
    $obj = new-object -psobject -prop @{
               Key = $objectKey
               Seq = $objectSeq
               Type = $objectType
               FileName = $objectFileName
           }
    $files += obj
}
...
# outside of loop, export array of objects to csv
$files | Export-Csv missingfiles.csv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文