Thứ Tư, 12 tháng 4, 2017

Phần mềm copyfile và nén file sử dụng powershell 4.0

Nội dung file check_copy.ps1
$PRIVATE:IL_ARGS_NUM=$args.Length
$Currentlocation=Get-Location
$source=$args[0]
$des=$args[1]

 

$filter=$args[2]
$IG_COM_DATE_FILE=Get-Date -Format "yyyy_MM_dd_HH_mm_ss"
$IG_COM_LOG_FILE=$IG_COM_DATE_FILE+".log"
$flag="FALSE"
function check_copy
{
    param([string]$source,[string]$des)
    if (Test-Path $des)
    {
        $md5source=Get-FileHash($source)
        $md5des=Get-FileHash($des)
        if($md5des.Hash -eq $md5source.Hash)
        {
            $flag="TRUE"
            Write-Host "`nThe file `"$source`" has been copied in `"$des`" successfully.`n" -ForegroundColor DarkGreen
            FUNC_LOG "`nThe file `"$source`" has been copied in `"$des`" successfully.`n"
        }
        else
        {
            Write-Host "`nThe file `"$source`" has been copied in `"$DestinationPath`" but the CRC check failed!`n" -ForegroundColor DarkRed
            FUNC_LOG "`nThe file `"$source`" has been copied in `"$DestinationPath`" but the CRC check failed!`n" "ERROR"
        }
    }
    else
    {
        Write-Host "`nThe file `"$source`" has not been copied in `"$des`"!`n" -ForegroundColor DarkRed
        FUNC_LOG "`nThe file `"$source`" has not been copied in `"$des`"!`n" "ERROR"
    }
    return $flag
}
function create-7zip([String] $aDirectory, [String] $aZipfile){
    [string]$pathToZipExe = "$Currentlocation\7za.exe";
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";
    & $pathToZipExe $arguments;
}
function excute_copy
{
    param([string]$source,[string]$des,[string]$filter)
    $filesx=Get-ChildItem $source -filter $filter
    For ($i=0; $i -lt $filesx.Count; $i++) 
    {
        Copy-Item $filesx[$i].FullName $des
        $filedest=$des+$filesx[$i].Name
        $flag=check_copy $filesx[$i].FullName $filedest
        if($flag -eq "FALSE")
        {
            FUNC_LOG "Error when copy file. Please check again"
            break;
        }
        else
        {
$filezip=$source+$IG_COM_DATE_FILE+".zip"
echo $filezip
            create-7zip $filesx[$i].FullName $filezip
        }
    }
    deletefile $source $des $filter $flag
}

function deletefile
{
    param([string]$source,[string]$des,[string]$filter,$flag)
    
    if($flag -eq "TRUE")
    {
        $filesx=Get-ChildItem $source -filter $filter
        For ($i=0; $i -lt $filesx.Count; $i++) 
        {
            Remove-Item $filesx[$i].FullName
        }
        $filezip=$source+$IG_COM_DATE_FILE+".zip"
      
        Copy-Item  $filezip $des
$filezipdest=$des+$IG_COM_DATE_FILE+".zip"
        $flag=check_copy $filezip $filezipdest
        if($flag -eq "TRUE")
        {
            Remove-Item $filezip
        }else
        {
            FUNC_LOG "Error when copy file zip. Please check again"    
        }

    }else
    {
        FUNC_LOG "System can't create zip file Because it dont completed copy files"
    }
}
function SCRIPT:FUNC_LOG($msg="",$type)
{
    $IL_DATETIME=Get-Date -Format "yyyy/MM/dd HH:mm:ss"
    echo "${IL_DATETIME} ${type} ${msg}" >> ${IG_COM_LOG_FILE}
    Write-host "${IL_DATETIME} ${type} ${msg}"
}


IF($IL_ARGS_NUM -eq 3)
{
    if(Test-Path $source)
    {
        if(Test-Path $des)
        {
            
            excute_copy $source $des $filter
        }
        else
        {
            Write-Host "Please check directory destination"
            FUNC_LOG "Please check directory destination" "ERROR"
        }
    }
    else
    {
        Write-Host "Please check directory source"
        FUNC_LOG "Please check directory source" "ERROR"
    }
}
else
{
    Write-Host "Please input argument"
    FUNC_LOG "Please input argument" "ERROR"
}

Nội dung file excute.bat
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\check_copy.ps1 D:\abc\ D:\x\ "ex1701*"
Trong đó
- D:\abc là thư mục gốc muốn copy
- D:\x là thư mục nguôn
- ex1701: là thực hiện copy nhưng file có chuỗi ex1701 từ thư mục đích đến thư mục nguồn
Download thêm ứng dụng 7za.exe.

Bỏ 3 file ở trên vào cùng 1 thư mục và chạy file excute.bat ( chú ý file excute.bat sửa lại đối số truyền vào cho phù hợp)

Link tải đầy đủ phần mềm
https://drive.google.com/file/d/0B-huld25wi26RTVRM2pKRXFrSk0/view?usp=sharing

Không có nhận xét nào:

Đăng nhận xét