Help writing a script to... for sorting files

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
marnediv
Posts: 1
Last visit: Sat Feb 17, 2024 10:17 am

Help writing a script to... for sorting files

Post by marnediv »

Guys,

Hello, first time poster here... I am having an issue putting together a script to do the following:
1. read file names from a *.csv file (Listed in column A)
THEN
2.go to a folder path, compare the *.csv and see if the file exist in the folder
THEN
3. If the file does exist in the source path, get the destination path from the *.csv and copy the file there from the source to the destination, that is in column B of the *.csv


Reason for this is because, a folder directory that a database queries for OCR was deleted. I was able to recover the data only, but the files need to be placed in the exact file structure for the OCR DB to function correctly. I had the DB team export a *.csv that lists the path and the file name, I just need to get them correctly correlated and copied into the correct path. I am not the admin here, I was called in to fix this issue. I have been able to recover the data only (brick level recovery) this cannot be done manually as there are more than 950k of files. here is what I have so far... I am open to suggestions if anyone has had to do this in the past...

[Codebox=powershell file=Untitled.ps1]$Result = ForEach ($file in (Import-Csv -Path C:\Scripts\FileRecoverytoDest.csv))
{
$FoundInFolders = (Get-ChildItem $file.filename -File -Recurse).FullName
if ($FoundInFolders.Count -eq 0)
{
$FoundInFolders = "NOT FOUND"
}
[PSCustomObject]@{
filename = $file.filename
path = $FoundInFolders -join ', '
}
}

$Result #| Export-Csv -Path output.csv -NoTypeInformation

########################################

$csv = Import-Csv -Path "C:Scripts\FileRecoveryToDest.csv"
$filepath = 'E:'

$entries = Get-ChildItem $filepath
ForEach ($entry in $entries)
{
$destinations = $csv | Select-Object -ExpandProperty destination
$find = $csv | select -ExpandProperty find
$a = $entry.fullname


ForEach ($f in $find)
{
If ($a -like "*$f*")
{

ForEach ($destination in $destinations)
{
Copy-Item $a $destination
}
}
}
} [/Codebox]
This topic is 3 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked