New-Item -itemtype directory is making a file not a directory

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Brad Baltus Brad Baltus 3 years, 5 months ago.

  • Author
    Posts
  • #11509
    Profile photo of Brad Baltus
    Brad Baltus
    Participant

    $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
    }

    }

  • #11510
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    New-Item -ItemType Container -Name deleteme

  • #11512
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

  • #11566
    Profile photo of Brad Baltus
    Brad Baltus
    Participant

    thanks Dave. That fixed it. I appreciate the help

You must be logged in to reply to this topic.