Hi all,
i need to move some files from C:\yyyy\ to another hdd, for example D:\ … and the script is working fine.
My problem is that, if possible, i would like the script automatically fullfill D:\ and, if the available space in D is not enough for the file, it move the file to E:, if also E is full to F:, and so on.
Firstly you should calcuate the size of the floder or files.
then use if to judge the size is bigger or smaller then the logicaldisk FreeSapce.
if it is true then put the files in.
if it flase, compare to next Drive
if all drives are not suitable, return a result.
here is the script.
param(
$path='C:\yyyy',
$CopyStatu=$false
)
$LogicalDrive=Get-CimInstance -Class CIM_LogicalDisk |Where-Object {$_.DeviceID -ne "c:"} |Sort-Object DeviceID
if($LogicalDrive.Length){
$DiskNumber=$LogicalDrive.Length
}else{
[int]$DiskNumber="1"
}
$isfolder=(Get-Item -Path $path).Mode
if($isfolder -eq "d-----"){
$size = (Get-ChildItem -Path $path -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
"It is a floder and the size is $size"
}else{
$size=(Get-Item -Path $path).Length
"It is a file and the size is $size"
}
for($i=0;$i -lt $DiskNumber;$i++){
if($size -le $LogicalDrive[$i].FreeSpace){
$DrivePath=$LogicalDrive[$i].DeviceID
Copy-Item -Path $path -Destination $DrivePath -Force
$CopyStatu=$true
break
}
}
if($CopyStatu){
Write-Host "Copy Item successfully"
}else{
Write-Host "All Drive is full, no thing copied"
}