From 5f152307f23b6a990e0be932e9c86141a7885bb3 Mon Sep 17 00:00:00 2001 From: datechnoman Date: Wed, 20 Dec 2023 04:09:13 +0000 Subject: [PATCH] Add commoncrawl_local_to_share_move.ps1 --- commoncrawl_local_to_share_move.ps1 | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 commoncrawl_local_to_share_move.ps1 diff --git a/commoncrawl_local_to_share_move.ps1 b/commoncrawl_local_to_share_move.ps1 new file mode 100644 index 0000000..413eb56 --- /dev/null +++ b/commoncrawl_local_to_share_move.ps1 @@ -0,0 +1,82 @@ +# Specify the full path to WinSCPnet.dll +$assemblyPath = "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" + +# Load WinSCP .NET assembly using [System.Reflection.Assembly]::LoadFrom +[Reflection.Assembly]::LoadFrom($assemblyPath) | Out-Null + +# Specify the path to winscp.exe +$winscpPath = "C:\Program Files (x86)\WinSCP\winscp.exe" + +# Create a session instance +$sessionType = [WinSCP.Session].FullName +$session = New-Object $sessionType + +# Set up session options +$sessionOptionsType = [WinSCP.SessionOptions].FullName +$sessionOptions = New-Object $sessionOptionsType + +# Set properties for session options +$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp +$sessionOptions.HostName = "192.168.1.248" +$sessionOptions.UserName = "root" +$sessionOptions.Password = "Tcft65rdx!" +$sessionOptions.SshHostKeyFingerprint = "ssh-ed25519 255 xyTP5uPM3w6ebT7P7Mx1945YIYrK7NXWlZNNOGt2geY" + +try +{ + # Set the ExecutablePath property + $session.GetType().GetProperty("ExecutablePath").SetValue($session, $winscpPath, $null) + + # Connect + $session.Open($sessionOptions) + + # Specify the local directory path + $localPath = "E:\CommonCrawl" + + # Specify the remote directory path + $remotePath = "/mnt/user/ArchiveTeam/CommonCrawl_Files/WAT_URLs/CC-MAIN-2023-23_May_June_2023" + + # Get a list of local files matching the pattern "_urls.txt.gz" + $localFiles = Get-ChildItem -Path $localPath -Filter "*_urls.txt.gz" -File -Recurse + + # Shuffle the local files array + $localFiles = $localFiles | Get-Random -Count $localFiles.Count + + # Check if there are any files to upload + if ($localFiles.Count -eq 0) + { + Write-Host "No files found to upload." + } + else + { + # Iterate through each file and upload/delete it + foreach ($fileInfo in $localFiles) + { + # Construct the full local file path + $localFilePath = $fileInfo.FullName + + # Construct the remote file path + $remoteFilePath = [WinSCP.RemotePath]::Combine($remotePath, $fileInfo.Name) + + # Upload the file + $transferResult = $session.PutFiles($localFilePath, $remoteFilePath, $False) + + # Check if the upload was successful + if ($transferResult.IsSuccess) + { + Write-Host "Upload successful: $localFilePath to $remoteFilePath" + + # Attempt to delete the local file + Remove-Item -Path $localFilePath -Force + } + else + { + Write-Host "Failed to upload file: $localFilePath" + } + } + } +} +finally +{ + $session.Dispose() +}