try to copy files from local folder C: drive to F Drive example.<\/li>\n<\/ol>\n
Advertisement
need to check if the file in destination F Drive exist, if not just copy and log to say it has succesfully copied or the file already exist in destination folder.<\/p>\n
I had to choose one three files from a folder example. abc.txt, abc2.txt and xyz.pdf out of number of files from a folder<\/p>\n
Here is my script, just struggling to use array to list the above three files to put in an array.<\/p>\n
#Copy only new files, saves time by only copying items that don't exist on the destination.\n\n#Gets all of the files from the source directory\n\n$FilesInSource = gci -File -path \"\\\\xyzserver\\ftp\" | select -ExpandProperty Name\n\n#Gets all of the files from destination folder\n$FilesInDest = gci -File -Path \"F:\\ftp\" | select -ExpandProperty name\n\n#list the differences between the source and destination.\nCompare-Object -DifferenceObject $FilesInSource -ReferenceObject $FilesInDest\n\n#Loop through each album in the library\nforeach ($filetocheck in $FilesInSource)\n{\n #Check to see if desitnation directory contains file from the FilesInSource library\n if ($FilesInDest -notcontains $filetocheck)\n{\n #If the files doesn't exist on the destination directory, copy it from source to destination folder\n write-host \"$files is not on destination, copying to destination\" -ForegroundColor Cyan\n Copy-Item -path \"\\\\xyzserver\\ftp\\$filetocheck\" -Destination \"f:\\ftp\\$filetocheck\"\n}\n}\n<\/code><\/pre>","upvoteCount":6,"answerCount":13,"datePublished":"2022-10-13T10:57:27.000Z","author":{"@type":"Person","name":"spiceuser-ss","url":"https://community.spiceworks.com/u/spiceuser-ss"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi Everyone,<\/p>\n
my script below:<\/p>\n
\n
try to copy files from local folder C: drive to F Drive example.<\/li>\n<\/ol>\n
need to check if the file in destination F Drive exist, if not just copy and log to say it has succesfully copied or the file already exist in destination folder.<\/p>\n
I had to choose one three files from a folder example. abc.txt, abc2.txt and xyz.pdf out of number of files from a folder<\/p>\n
Here is my script, just struggling to use array to list the above three files to put in an array.<\/p>\n
#Copy only new files, saves time by only copying items that don't exist on the destination.\n\n#Gets all of the files from the source directory\n\n$FilesInSource = gci -File -path \"\\\\xyzserver\\ftp\" | select -ExpandProperty Name\n\n#Gets all of the files from destination folder\n$FilesInDest = gci -File -Path \"F:\\ftp\" | select -ExpandProperty name\n\n#list the differences between the source and destination.\nCompare-Object -DifferenceObject $FilesInSource -ReferenceObject $FilesInDest\n\n#Loop through each album in the library\nforeach ($filetocheck in $FilesInSource)\n{\n #Check to see if desitnation directory contains file from the FilesInSource library\n if ($FilesInDest -notcontains $filetocheck)\n{\n #If the files doesn't exist on the destination directory, copy it from source to destination folder\n write-host \"$files is not on destination, copying to destination\" -ForegroundColor Cyan\n Copy-Item -path \"\\\\xyzserver\\ftp\\$filetocheck\" -Destination \"f:\\ftp\\$filetocheck\"\n}\n}\n<\/code><\/pre>","upvoteCount":6,"datePublished":"2022-10-13T10:57:27.000Z","url":"https://community.spiceworks.com/t/powershell-array-file-copy-and-log/938148/1","author":{"@type":"Person","name":"spiceuser-ss","url":"https://community.spiceworks.com/u/spiceuser-ss"}},{"@type":"Answer","text":"
This line below references a variable called $files that is not defined.<\/p>\n
Sorry for my limited knowledge, thanks for the correction, i just updated the source path and destination path.<\/p>","upvoteCount":0,"datePublished":"2022-10-13T11:13:52.000Z","url":"https://community.spiceworks.com/t/powershell-array-file-copy-and-log/938148/3","author":{"@type":"Person","name":"spiceuser-ss","url":"https://community.spiceworks.com/u/spiceuser-ss"}},{"@type":"Answer","text":"
When you use -Directory on Get-ChildItem, the command will only return directories rather than files. If you want only files, then use -File instead of -Directory.<\/em><\/strong><\/p>","upvoteCount":0,"datePublished":"2022-10-13T11:27:38.000Z","url":"https://community.spiceworks.com/t/powershell-array-file-copy-and-log/938148/4","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
Many thanks,<\/p>\n
I replaced the -Directory<\/strong> to -File<\/strong> in original post and also used non-reserved variable in for loop ($filetocheck instead of $file)<\/p>\n\n
\n
Now the only thing left is to log the outcome of which file is copied in log.txt file<\/p>\n<\/li>\n
\n
An array to specify the files I want to copy ${abc.txt, abc2.txt, abc.pdf} instead of copy all the files from source to destination.<\/p>\n<\/li>\n<\/ol>\n
$FilesInSource = gci -File -path \"\\\\xyzserver\\ftp\" | select -ExpandProperty Name\n\n#Gets all of the files from destination folder\n$FilesInDest = gci -File -Path \"F:\\ftp\" | select -ExpandProperty name\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2022-10-13T11:34:12.000Z","url":"https://community.spiceworks.com/t/powershell-array-file-copy-and-log/938148/5","author":{"@type":"Person","name":"spiceuser-ss","url":"https://community.spiceworks.com/u/spiceuser-ss"}},{"@type":"Answer","text":"
I always prefer using the right tool for the job and while PowerShell is able to copy files, I always prefer to use robocopy. You can also use PowerShell to evaluate the exit code to see if there where errors.<\/p>\n