windows - How to append GUID to existing filenames and save to CSV
问题描述
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.
解决方案
我目前有一个 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
可以使用的类型,然后执行其余的逻辑。
推荐阅读
- r - 使用 fileInput (R shiny) 之类的函数获取文件的原始名称
- docker - 对自反泊坞窗图像构建的健全性检查?
- amazon-web-services - 如何解决 tls 的问题:kubernetes 中的内部错误
- r - 创建时间箱并将数据分配给正确的箱
- visual-studio - Visual Studio 中的发布选项 - Web 与文件系统
- python - Django-Nginx 和 Gunicorn 在 POST 请求上超时
- python - 空 html 输入返回的值的 Flask-Python 特征
- c - 从头实现malloc函数的问题
- ios - 为什么 UIView 的 CALayer.speed 为零会阻止 UIAlertController 的按钮工作?
- angular - 如何在角度 4 中使用 ngFor 动态呈现 svg