Reputation: 3020
I have a folder which has many files and folders inside. I want to send that folder to a remote host over ftp. i think windows ftp client cannot do it, so i decided to archive all files (into some zip or whatever) and then send the one file over ftp. how can i do it in powershell (2.0)? compression is not important, important is there has to be only 1 file.
thanks in advance.
Upvotes: 1
Views: 1493
Reputation: 192627
Very simple using DotNetZip.
[System.Reflection.Assembly]::LoadFrom("c:\\dev\dotnet\\Ionic.Zip.dll");
$directoryToZip = "c:\\temp";
$zipfile = new-object Ionic.Zip.ZipFile;
$zipfile.AddEntry("Readme.txt", "This is a zipfile created from within powershell.")
$zipfile.AddDirectory($directoryToZip, "home")
$zipfile.Save("ZipFiles.ps1.out.zip");
$zipfile.Dispose();
The ZipFile object does the recursion for you, as part of the ZipFile.AddDirectory() method.
DotNetZip is a free library.
One add-on comment - I had reason to do FTP uploads in the past, as part of a software release. I found that on successive uploads, most of the files had not changed. So I introduced a "table of contents" file, which mapped the MD5 hash of each file to its name. Before uploading, I'd download the table of contents, then check each MD5 in the table against the MD5 of the file-on-disk. If they match, then no need to upload that particular file. This saved lots of time and data transfer.
I don't have a powershell script for that, though. :<
Upvotes: 0
Reputation: 263
So you can get the files of your folder recursively. After this, you can add them to your ziparchive and send them via FTP. Powershell hasn´t a function to archive files. But there some external tools. Good is the commandline from 7zip and the dll SharpZipLib.
$itemslist = Get-ChildItem C:\\folder -recurse
$filelist = New-Object System.Collections.ArrayList
foreach ($item in $itemslist)
{
if ($item.GetType().FullName -eq 'System.IO.FileInfo')
{
$filelist.Add($item)
}
}
foreach ($file in $filelist)
{
//Add file to your ZipArchive
}
//Send ZipArchive
Upvotes: 1
Reputation: 710
May be this can help you: http://tasteofpowershell.blogspot.com/2008/08/recursion-in-powershell.html
Upvotes: 0