Instantly share code, notes, and snippets.

Embed
What would you like to do?
PerforceCopy
namespace Tools
{
using System.IO
using Perforce.P4;
public class PerforceCopy
{
private static void Copy()
{
const string serverString = "10.10.0.151:1666";
const string username = "jesse.pearson";
const string password = "";
const string passwordKey = "Password";
const string depotFormat = "//ops/custom/{0}/...";
const string ampCompanyCode = "PPL";
const string pathFormat = @"C:\temp\perforce\test\{0}";
var server = new Server(new ServerAddress(serverString));
var repository = new Repository(server);
var options = new Options();
var client = new Client();
var depotFilesOptions = new GetDepotFilesCmdOptions(GetDepotFilesCmdFlags.NotDeleted, 0);
var depotPath = string.Format(depotFormat, ampCompanyCode);
var fileSpec = new FileSpec(new DepotPath(depotPath), null);
options[passwordKey] = password;
client.Options = ClientOption.AllWrite;
repository.Connection.UserName = username;
repository.Connection.Client = client;
repository.Connection.Connect(options);
var files = repository.GetFiles(depotFilesOptions, fileSpec);
foreach (var file in files)
{
var path = string.Format(pathFormat, file.DepotPath.ToString());
var localPath = Path.GetFullPath(path);
var fileContentsOptions = new GetFileContentsCmdOptions(GetFileContentsCmdFlags.Suppress, localPath);
var fileContentSpec = new FileSpec(file.DepotPath, null);
var outputFile = repository.GetFileContents(fileContentsOptions, fileContentSpec);
}
}
}
}
Add-Type -Path "p4apinet.dll"
function CopyFrom-Perforce
{
Set-Variable ServerName -option Constant -value "10.10.0.151:1666"
Set-Variable Username -option Constant -value "jesse.pearson"
Set-Variable Password -option Constant -value ""
Set-Variable AmpCompanyCode -option Constant -value "PPL"
Set-Variable DepotFormat -option Constant -value "//ops/custom/{0}/..."
Set-Variable PathFormat -option Constant -value "C:\temp\perforce\test\{0}"
Set-Variable PasswordKey -option Constant -value "Password"
$server = New-Object Perforce.P4.Server -ArgumentList (New-Object Perforce.P4.ServerAddress -ArgumentList $ServerName)
$repository = New-Object Perforce.P4.Repository -ArgumentList $server
$options = New-Object Perforce.P4.Options
$client = New-Object Perforce.P4.Client
$depotFilesOptions = New-Object Perforce.P4.GetDepotFilesCmdOptions -ArgumentList ([Perforce.P4.GetDepotFilesCmdFlags]::NotDeleted), 1
$depotPath = $DepotFormat -f $AmpCompanyCode
$depotFileSpec = New-Object Perforce.P4.FileSpec -ArgumentList (New-Object Perforce.P4.DepotPath -ArgumentList $depotPath)
$options.Password = $Password
$client.Options = [Perforce.P4.ClientOption]::AllWrite
$repository.Connection.Username = $Username
$repository.Connection.Client = $client
$repository.Connection.Connect($options)
$repository.Connection.Login($Password)
$files = $repository.GetFiles($depotFilesOptions, $depotFileSpec)
foreach ($file in $files)
{
$path = $PathFormat -f $file.DepotPath.ToString()
$localPath = [System.IO.Path]::GetFullPath($path);
$fileContentsOptions = New-Object Perforce.P4.GetFileContentsCmdOptions -ArgumentList ([Perforce.P4.GetFileContentsCmdFlags]::Suppress), $localPath
$fileContentSpec = New-Object Perforce.P4.FileSpec -ArgumentList $file.DepotPath, $null
$outputFile = $repository.GetFileContents($fileContentsOptions, $fileContentSpec)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment