Skip to content

Instantly share code, notes, and snippets.

@csereno
Last active May 29, 2021 22:58
Show Gist options
  • Save csereno/860825bbaee09ab61dfec3c4c7b8b8a9 to your computer and use it in GitHub Desktop.
Save csereno/860825bbaee09ab61dfec3c4c7b8b8a9 to your computer and use it in GitHub Desktop.
Use Powershell to find a file in a directory from a list of files
$SearchDir = "C:\Source\Dir\"
$Filenames = Get-Content C:\filelist.txt
$Pics = Get-ChildItem -Path $SearchDir -File -Recurse
foreach ($File in $Filenames) {
$File = ($File.split('\')[-1]) #Gets just the file name
if(Get-Childitem -Path $SearchDir -Include $File -File -Recurse -ErrorAction SilentlyContinue) {
echo File Found: $File
}else{
echo Not Found: $File >> C:\NotFoundLog.txt
}
}
# Loops through a text file listing file names and searches for them in a directory
# Then copies that file down from S3 if it is not found locally
$SearchDir = "C:\Source\Dir\"
$Filenames = Get-Content C:\filelist.txt
$Pics = Get-ChildItem -Path $SearchDir -File -Recurse
foreach ($File in $Filenames) {
$File = ($File.split('\')[-1])
if(Get-Childitem -Path $SearchDir -Include $File -File -Recurse -ErrorAction SilentlyContinue) {
echo File Found: $File
}else{
echo Not Found: $File >> C:\NotFoundLog.txt
$FilePath = $File.split('\')[-2] + '/' + $File.split('\')[-1] #This takes the last subdirectory and filename. This could be better...
aws s3 cp "s3://bucket-name/folder/$FilePath" "C:/folder/path/$FilePath" #If doing this from Wasabi add: --endpoint-url https://s3.wasabisys.com
}
}
$Year = "2019"
$LocalDir = "D:\Pictures\"
$Pics = Get-ChildItem -Path $SearchDir -File -Recurse
#echo "syncing dryrun ..."
aws s3 sync "s3://S3BUCKETNAME/Memories/$Year/" "$LocalDir$Year" --endpoint-url https://s3.wasabisys.com --dryrun > C:\Log\dryrun.txt
sleep 5
$Filenames = Get-Content C:\Log\dryrun.txt
foreach ($File in $Filenames) {
#echo ($File.Substring(10, $File.lastIndexOf('\')))
$FileName = ($File.split('\')[-1])
echo $FileName
if(Get-Childitem -Path $LocalDir -Include $FileName -File -Recurse -ErrorAction SilentlyContinue) {
}else{
echo $File >> C:\Log\CopyThese.txt
$FilePath = $File.split('\')[-2] + '/' + $File.split('\')[-1]
$FilePath2 = $File.split('\')[-2] + '\' + $File.split('\')[-1]
echo $FilePath
#aws s3 cp "s3://S3BUCKETNAME/Memories/$Year/$FilePath" "$LocalDir$Year\$FilePath2" --endpoint-url https://s3.wasabisys.com
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment