locked
Powershell - Copy files to dated folder script! RRS feed

  • General discussion

  • Hi, Im new to powershell and need a little help in finalising my script, I want to move files from 2 different locations and move them to destinations into a folder which will be named with the current date. :

    Here is the script so far.... (It just does a straight copy from one place to another)  

    $src = "H:\testing\pickup"
    $dst = 'H:\testing\destination'

    Get-ChildItem $src -Filter "IDITLog*" -Recurse | % {
        #Creates an empty file at the destination to make sure that subfolders exists
        New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force
        copy -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force }

    $src = "H:\testing\pickup2"
    $dst = 'H:\testing\destination2'

    Get-ChildItem $src -Filter "IDITLog*" -Recurse | % {
        #Creates an empty file at the destination to make sure that subfolders exists
        New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force
        copy-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force }

    Can anyone help please...

    Friday, September 8, 2017 12:25 PM

All replies

  • What errors are you getting? why are you copying the item when your goal is to move it?
    Friday, September 8, 2017 1:00 PM
  • You can just use New-Item -force to create a folder.  If the folder already exists the command will do nothing.

    You do not have to use Get-ChildItem as Copy-Item has the same arguments.

    New-Item $dst -Force -ItemType Directory
    Copy-Item -path $src -Destination $dst -Filter IDITLog* -Recurse


    \_(ツ)_/

    Friday, September 8, 2017 1:35 PM