Move-Item is not working for existing folders and files

This topic contains 3 replies, has 4 voices, and was last updated by Profile photo of Jack Neff Jack Neff 3 months, 3 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #42563
    Profile photo of Gina Bautista
    Gina Bautista
    Participant

    My code snippet is below. I am trying to have items move into folders that already exist. The folders might actually have the files in them too. I want the code to move the file in to the folder and I want it to overwrite the existing file regardless, no matter what.

    [PRE]

    #If a date folder with the date of this item is already created, it is moved there
    If(test-path "\\172.17.200.20\CDC\archive\$datefolder")
    {
    Add-content -Path $logpath -value "Date file already created. Moving item to date file in archive on old server."
    $archive = "\\172.17.200.20\CDC\archive\$datefolder"
    Move-Item $item $archive -Force
    }

    #If the date folder with the date of this item is not already created, the folder is created and then the item is moved to that location.
    else
    {
    Add-content -Path $logpath -value "Date file not already created. Creating date file in archive on old server."
    New-Item -ItemType directory -Path "\\172.17.200.20\CDC\archive\$datefolder"
    $archive = "\\172.17.200.20\CDC\archive\" + $datefolder
    Add-content -Path $logpath -value "Now moving itme to date folder just created."
    move-item $item $archive -Force
    }

    [/PRE]

    #42567
    Profile photo of Justin King
    Justin King
    Participant

    without knowing the format of the $datefolder I have to guess a bit. Is it possible you have brackets or other special characters so you need to call out -litteralpath instead of the default -path? At face value the syntax looks correct.

    #42569
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Also, use angle brackets around PRE, not square brackets, to format code. See the syntax in the three bullets above the reply textbox.

    #42580
    Profile photo of Jack Neff
    Jack Neff
    Participant

    In your script $archive contains only a string. It doesn't represent an array of files. Try instead...

    $archive = Get-ChildItem "\\172.17.200.20\CDC\archive\$datefolder" -Recurse

    Edit: Never mind what I wrote, just re-read your post you're moving the whole folder. Carry on.

    • This reply was modified 3 months, 3 weeks ago by Profile photo of Jack Neff Jack Neff.
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.