I’m getting an error message when I run this script.
It does copy the file to the target location.
I will be a scheduled task so I just want to C:\Source$subfolder because it will recurse.
The folder nested will be 20180111 or 20180112
This is the error that I’m getting
xtract: C:\Target\New\20180108/D2_August_27_2017-workspace.xml
New-Object : Exception calling “.ctor” with “2” argument(s): “The directory name is invalid.”
At line:32 char:110
- … | Out-Null |New-Object IO.FileSystemWatcher $path, $filter -Property …
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (
[New-Object], MethodInvocationException
- FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
- CategoryInfo : InvalidOperation: (
This is the script
Exit if any command/function sees an error
$ErrorActionPreference = “Stop”
function Create-Dir-If-Not-Exist($fullPath) {
if (!(Test-Path $fullPath)) {
Write-Host “Create dir: $fullPath”
New-Item -ItemType Directory -Path $fullPath
}
}
function Unzip($zipfile, $outdir)
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
foreach ($entry in $zip.Entries)
{
$destFilePath = [System.IO.Path]::Combine($outdir, $entry.FullName)
$isDir = ($destFilePath.EndsWith(“") -or $destFilePath.EndsWith(”/"))
if ($isDir) {
Create-Dir-If-Not-Exist($destFilePath)
} else {
$parentDir = [System.IO.Path]::GetDirectoryName($destFilePath)
# Sometimes its parent directory is not an entry in $zip.
# Therefore, we need to make sure its parent dir exists.
# Also, Create-Dir-If-Not-Exist(..) is an equivalent of `mkdir -p`.
# Therefore, we don't need to worry about the parent's parent.
Create-Dir-If-Not-Exist($parentDir)
Write-Host "Extract: $destFilePath"
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $destFilePath, $true) | Out-Null |New-Object IO.FileSystemWatcher $path, $filter -Property @{
NotifyFilter = [IO.NotifyFilters]‘FileName, LastWrite’
}
}
}
}
$zip_file_path = [IO.Path]::GetFullPath(“C:\Source\20180111\20180108.zip”)
$dest_path = [IO.Path]::GetFullPath(“C:\Target\New”)
Get-Date -DisplayHint Date
Unzip $zip_file_path $dest_path
I left out the part of the script with the email details.
This is the original script before modification:
#powershell Set-ExecutionPolicy RemoteSigned
$Username = “cohen.carryl@gmail.com”;
$Password= “N4ture2017”;
function Send-ToEmail([string]$email, $foldername, $srcOrTarget){
$message = new-object Net.Mail.MailMessage;
$message.From = "ccarrylab@gmail.com";
$message.To.Add($email);
$FileNames = Get-ChildItem -Path $foldername
if(Compare-Object $srcOrTarget "SRC"){
$message.Subject = "$foldername folder recived ";
$message.Body = "$FileNames files are received in the source folder";
}else {
$message.Subject = "$foldername folder copied";
$message.Body = "$FileNames files are copied in the target folder";
}
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);
}
$srcPath = “C:\Source”
$targetPath = "C:\Users\greatness\Target"
$fname = (get-date).ToString(“yyyMMdd”)
$sourceFolder = “$srcPath$fname”
$targetFolder = “$targetPath$fname”
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
if (Test-Path $sourceFolder ){
$Files = Get-ChildItem $sourceFolder*.* -include .zip,.out,*.err
$srcZipFile = $Files | where {($_.extension -eq “.zip”) }#Identifying zip file in the source path#
if ((Test-Path $sourceFolder*.out) -or (Test-Path $sourceFolder*.err)){
$srcOrTarget=“SRC”
Send-ToEmail -email “ccarrylab@gmail.com” $sourceFolder $srcOrTarget;
if (Test-Path $srcZipFile){
$zipFolderName = $([io.fileinfo]“$srcZipFile”).basename
Unzip “$srcZipFile” “$sourceFolder$zipFolderName”
$tempList = get-childitem “$sourceFolder$zipFolderName” -recurse
$zipFileList = $tempList | where {($.extension -eq “.err”) -or ($.extension -eq “.out”) }
if ($zipFileList.length -gt 0 ){
Remove-Item $sourceFolder\$zipFolderName -Force -Recurse
}else{
Remove-Item $sourceFolder\$zipFolderName -Force -Recurse
}
}
Copy-Item $sourceFolder $targetPath -Force -recurse
if (Test-Path $targetPath){
$srcOrTarget = 'TGT'
Send-ToEmail -email "ccarrylab@gmail.com" $targetFolder $srcOrTarget;
}
}
}else{
Write-Host “There is no files received today yet”
}