【发布时间】:2018-08-08 04:53:28
【问题描述】:
我正在尝试将一批文件(文件名以 6 位开头的文件)从临时文件夹复制到永久位置,不包括新位置中已存在的文件。
复制完成后,我想将复制文件的文件名和新路径导出为 CSV。
获取旧文件位置并导出为 CSV 非常容易,我只是不太确定如何获取新文件位置。
我的脚本如下所示:
# Prompt for file origin
$file_location = Read-Host -Prompt "Where do you want your files to come from?"
# Prompt for file destination
$file_destination = Read-Host -Prompt "Where do you want your files to go? `n(They won't be copied if they're already there)"
# Save contents of file destination - used to check for duplicates
$dest_contents = Get-ChildItem $file_destination
<# For all of the files of interest (those whose names begin with 6 digits) in $file_location,
determine whether that filename already exists in the target directory ($file_destination)
If it doesn't, copy the file to the destination
Then save the filename and the new** filepath to a CSV #>
Get-ChildItem -Path $file_location |
Where-Object { $_.Name -match '^\d{6}' -And !($dest_contents -Match $_.Name ) } |
Copy-Item -Destination $file_destination -PassThru |
Select-Object -Property Name, FullName | # **Note: This saves the old filepath, not the new one
Export-CSV -Path "$file_location\files_copied.csv" -NoClobber
【问题讨论】:
标签: powershell powershell-2.0 export-to-csv