2

I currently have a CSV which contains 1 column that lists many file FullNames. (ie. "\\server\sub\folder\file.ext").

I am attempting to import this CSV, move the file to a separate location and append a GUID to the beginning of the filename in the new location (ie GUID_File.ext). I've been able to move the files, generate the GUID_ but haven't been able to store and reuse the existing filename.ext, it just gets cut off and the file ends up just being a GUID_. I just am not sure how to store the existing filename for reuse.

$Doc = Import-CSV C:\Temp\scripttest.csv

ForEach ($line in $Doc)
{
 $FileBase = $Line.basename 
 $FileExt = $Line.extension 
Copy-Item -path  $line.File -Destination "\\Server\Folder\$((new-guid).guid.replace('-',''))_$($Filebase)$($FileExt)"
}

If possible, I'm going to also need to store and place all the new GUID_File.ext back into a CSV and store any errors to another file.

4

4 回答 4

1

我目前有一个 CSV,其中包含 1 列,其中列出了许多文件全名。(即“\server\sub\folder\file.ext”)。

这不是 CSV。它只是一个带有列表的纯文本文件。

但是,您可以通过以下方式实现目标:

foreach ($path in (Get-Content -Path C:\Temp\scripttest.csv))
{
    $file = [System.IO.FileInfo]$path
    $prefix = (New-Guid).Guid -replace '-'
    Copy-Item -Path $file.FullName -Destination "\\Server\Folder\${prefix}_$file"
}

这将获取您的列表,将项目转换为FileInfo可以使用的类型,然后执行其余的逻辑。

于 2018-08-28T22:40:46.057 回答
1

基于:

$FileBase = $line.basename
$FileExt = $line.extension

听起来您错误地认为$line代表从返回的对象的实例Import-Csv C:\Temp\scripttest.csv[System.IO.FileInfo]实例,但它们不是:

什么Import-Csv输出是[pscustomobject]实例,其属性反映了输入 CSV 的列值,并且这些属性的值总是字符串

因此,您必须使用$line.<column1Name>来引用包含完整文件名的列,其中<column1Name>是为输入 CSV 文件的标题行(第一行)中感兴趣的列定义的名称。

如果 CSV 文件没有标题行,您可以通过将列名数组传递给Import-Csv' 的-Header参数来指定列名,例如,
Import-Csv -Header Path, OtherCol1, OtherCol2, ... C:\Temp\scripttest.csv

我假设感兴趣的列Path在以下解决方案中命名:

$Doc = Import-Csv C:\Temp\scripttest.csv

ForEach ($rowObject in $Doc)
{
  $fileName = Split-Path -Leaf $rowObject.Path
  Copy-Item -Path $rowObject.Path `
            -Destination "\\Server\Folder\$((new-guid).guid.replace('-',''))_$fileName"
}

请注意如何Split-Path -Leaf用于从完整输入路径中提取文件名,包括扩展名。

于 2018-08-29T02:13:19.960 回答
0

如果我仔细阅读了您的问题,您希望:

  • 复制“文件”列中 CSV 文件中列出的文件。
  • 新文件应在文件名前附加一个 GUID
  • 您需要一个新的 CSV 文件,其中存储了新的文件名以供以后参考
  • 您想跟踪任何错误并将其写入(日志)文件

假设您有一个如下所示的输入 CSV 文件:

File,Author,MoreStuff
\\server\sub\folder\file.ext,Someone,Blah
\\server\sub\folder\file2.ext,Someone Else,Blah2
\\server\sub\folder\file3.ext,Same Someone,Blah3

然后下面的脚本希望你想要什么。它通过在它们前面加上 GUID 来创建新文件名,并将列中列出的 CSV 中的文件复制File到某个目标路径。它在目标文件夹中输出一个新的 CSV 文件,如下所示:

OriginalFile,NewFile
\\server\sub\folder\file.ext,\\anotherserver\sub\folder\38f7bec9e4c0443081b385277a9d253d_file.ext
\\server\sub\folder\file2.ext,\\anotherserver\sub\folder\d19546f7a3284ccb995e5ea27db2c034_file2.ext
\\server\sub\folder\file3.ext,\\anotherserver\sub\folder\edd6d35006ac46e294aaa25526ec5033_file3.ext

任何错误都列在日志文件中(也在目标文件夹中)。

$Destination = '\\Server\Folder'
$ResultsFile = Join-Path $Destination 'Copy_Results.csv'
$Logfile     = Join-Path $Destination 'Copy_Errors.log'
$Doc         = Import-CSV C:\Temp\scripttest.csv

# create an array to store the copy results in
$result = @()
# loop through the csv data using only the column called 'File'
ForEach ($fileName in $Doc.File) {
    # check if the given file exists; if not then write to the errors log file
    if (Test-Path -Path $fileName -PathType Leaf) {
        $oldBaseName = Split-Path -Path $fileName.Path -Leaf
        # or do $oldBaseName = [System.IO.Path]::GetFileName($fileName)
        $newBaseName = "{0}_{1}" -f $((New-Guid).toString("N")), $oldBaseName
        # (New-Guid).toString("N") returns the Guid without hyphens, same as (New-Guid).Guid.Replace('-','')

        $destinationFile = Join-Path $Destination $newBaseName
        try {
            Copy-Item -Path $fileName -Destination $destinationFile -Force -ErrorAction Stop
            # add an object to the results array to store the original filename and the full filename of the copy
            $result += New-Object -TypeName PSObject -Property @{
                'OriginalFile' = $fileName
                'NewFile'      = $destinationFile
            }
        }
        catch {
            Write-Error "Could not copy file to '$destinationFile'"
            # write the error to the log file
            Add-content $Logfile -Value "$((Get-Date).ToString("yyyy-MM-dd HH:mm:ss")) - ERROR: Could not copy file to '$destinationFile'"
        }
    }
    else {
        Write-Warning "File '$fileName' does not exist"
        # write the error to the log file
        Add-content $Logfile -Value "$((Get-Date).ToString("yyyy-MM-dd HH:mm:ss")) - WARNING: File '$fileName' does not exist"
    }
}
# finally create a CSV with the results of this copy.
# the CSV will have two headers 'OriginalFile' and 'NewFile'
$result | Export-Csv -Path $ResultsFile -NoTypeInformation -Force
于 2018-08-29T13:16:28.927 回答
0

感谢大家的解决方案。他们都工作得很好。我选择 Theo 作为答案,因为他的解决方案解决了错误日志记录并将所有新重命名的文件与 GUID_File.ext 一起存储到现有 CSV 信息中。

谢谢你们。

于 2018-08-29T16:25:28.637 回答