Author Posts

November 15, 2013 at 11:16 am

$path = dir env:userprofile
[Int]$FileCount = 0
[Boolean]$AllFilesFound = $false
[Int]$Sleep10Secs = 10
[Int]$NumOfFiles = 0
[INT]$seconds = 10
function movefiles { move-item ($ListOfFiles) -destination .\$getdate }
function makedir { new-item -name .\$getdate -itemtype directory -path h:\testdir\test }

#Sets the current working location
Set-Location "h:\Testdir\test"

while ($AllFilesFound -eq $false)
{
cls

Write-Host "Searching for files."
$ListOfFiles = Get-ChildItem | where {!$_.PsIsContainer}

foreach ($item in $ListOfFiles)
{
$NumOfFiles++
}

if($NumOfFiles -ge 5)
{
Write-Host "`nFound $NumOfFiles files!!!"
$getdate = get-date -format yyyymmddhmmss
Write-host $listoffiles
Add-Content -value $NumOfFiles -path h:\Testdir\Archive.log
movefiles
makedir
$NumOfFiles = 0
Write-Host "`nExiting Program.`n"
}

for($timer=$seconds;$timer -gt 1; $timer–)
{
$perComplete = 100-($timer * 10)

Write-Progress -Activity "Searching for Files..." -Secondsremaining $Timer -PercentComplete $percomplete -status "Please Wait..."
start-sleep 1
}

}

November 15, 2013 at 11:39 am

New-Item -ItemType Container -Name deleteme

November 15, 2013 at 12:03 pm

New-Item is probably failing altogether. You're calling "MoveFiles" before "MakeDir". By the time you get to calling New-Item, there's already a file with the name of the directory you're trying to create.

If you call MakeDir before MoveFiles, it will probably work.

November 18, 2013 at 1:52 pm

thanks Dave. That fixed it. I appreciate the help