script copy files from a folder to other folder - filename condition

Hello guys,

I need to your help how to create a script which will be copy all files beginning with resultsYYYYYMMDD* from a folder A to other folder (which must be created automatically by the filenames) 

For example:

C:\folder_A

results20150101aaaa.xls

results20150102bbbb.xls

results20150103aaaa.xls

results20150201aaaa.xls

results20150301aaaa.xls......

I need to copy all files beginning with results201501 to a new created folder named 201501

and files beginning with results201502 to a new created folder named 201502 etc.

Any idea?

Thanks a lot!

  

July 31st, 2015 4:54pm

$Path = 'C:\folder_A'
Get-ChildItem -Path $Path -Filter 'results*.xls' | 
    Group-Object {$_.Name.Substring(7,6)} | ForEach-Object {
        $NewFolder = New-Item -Name $_.Name -Path $Path -ItemType Directory
        $_.Group | Copy-Item -Destination $NewFolder
    }

or

Get-ChildItem C:\folder_A -File | ForEach-Object {
    $dest = '{0}\{1}\{2}' -f $_.Directory, $_.Name.Substring(7,6), $_.Name
    New-Item -Path $(Split-Path $dest -Parent) -ItemType Directory -Force
    Copy-Item -Path $_.FullName -Destination $dest
}

July 31st, 2015 5:55pm

$Path = 'C:\folder_A'
Get-ChildItem -Path $Path -Filter 'results*.xls' | 
    Group-Object {$_.Name.Substring(7,6)} | ForEach-Object {
        $NewFolder = New-Item -Name $_.Name -Path $Path -ItemType Directory
        $_.Group | Copy-Item -Destination $NewFolder
    }

or

Get-ChildItem C:\folder_A -File | ForEach-Object {
    $dest = '{0}\{1}\{2}' -f $_.Directory, $_.Name.Substring(7,6), $_.Name
    New-Item -Path $(Split-Path $dest -Parent) -ItemType Directory -Force
    Copy-Item -Path $_.FullName -Destination $dest
}

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 9:51pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics