Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find then copy files from Network Server to local folder from a CSV file (Mac OS X)

Status
Not open for further replies.

AndiZu

Programmer
Mar 23, 2018
2
0
0
GB
Hi

I'm having real problems and hope you can help me?

I wish to search my network backup server (NAS) for images that are listed in a CSV file (one per line - i.e.: 0001.jpg) once found a copy to placed in a local folder on my Mac (/RESULTS/)

I have managed to get this to work but only works on the root folder of the server, I can't get it to search all sub-folders on the Backup have tried -r & --recursive but neither are working!

Here's the code Ive been using:

rsync -r --files-from=‘/Users/AndiZu/Desktop/ebay2.csv’ /Volumes/Studio-1/ /Users/AndiZu/Desktop/RESULTS/

ebay2.csv = List of files to search for

/Volumes/Studio-1/ = Path to my backup server

/Users/AndiZu/Desktop/RESULTS/ = Local folder where files should be copied to!


Thank you in advance!

AndiZu
 
AndiZu,
I would do something like this. You may have to play with the paths to make it work.
Disclaimer: I know almost nothing about the Mac OS, so I have no idea if these commands will work properly or not.
Good luck!

Code:
## These are going to have to be changed to match your environment
$search_files_path = "/Users/AndiZu/Desktop/ebay2.csv"
$source = "/Volumes/Studio-1/"
$dest = "/Users/AndiZu/Desktop/RESULTS/"

## Check for search files path existence
try
	{$search_files_path_found = Test-Path $search_files_path}
catch
	{
	 write-host ""
	 write-host "Search file ($source) cannot be found." -foregroundcolor "red"
	 write-host ""
	}

## Check for source existence
try
	{$source_found = Test-Path $source}
catch
	{
	 write-host ""
	 write-host "Source path ($source) cannot be found." -foregroundcolor "red"
	 write-host ""
	}

## Check for destination existence
try
	{$dest_found = Test-Path $dest}
catch
	{
	 write-host ""
	 write-host "Destination path ($dest) cannot be found." -foregroundcolor "red"
	 write-host ""
	}

if ($dest_found -AND $source_found -AND $search_files_path_found)
	{
	 ## Import search files
	 $search_files = import-csv $search_files_path
	 
	 ## Get all files on the source
	 $all_files = Get-ChildItem -Path $source -Recurse | where {!($_.psiscontainer)}

	 ## Look for the files you want copied
	 foreach ($file in $search_files)
		{
		 $filename = $file.Name
		 write-host $filename
		 $file_found = $all_files | where {$_.name -match $filename}

		 ## Determine how many files were found
		 $count = $file_found.Count

		 switch ($count)
			{
			 1
				{
				 $file_path = ($file_found.VersionInfo).FileName
				 write-host "`t$file_path"
				 Copy-Item -Path $file_path -Destination $dest -Force
				 break
				}
			 0
				{
				 write-host "`tFile not found" -foregroundcolor "red"
				 break
				}
			 default
				{
				 write-host "`tMultiple files found!" -foregroundcolor "yellow"
				 foreach ($found in $file_found)
					{
					 $file_path = ($found.VersionInfo).FileName
					 write-host "`t   $file_path" -foregroundcolor "yellow"
					}
				 break
				}
			}
		}
	}


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Hi blister911

Thanks for this but it didn't seem to work! It didn't throw back any errors but didn't copy / move any of the images!

Thanks anyways!

Andi
 
I think it might be a difference between our input files. My file names column has a header of "Name", which I reference in the script.

Code:
Name         <--- Header
File1.jpg    <--- File
File2.gif    <--- File
File3.txt    <--- File
File4.doc    <--- File



Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top