I have been given a business requirement to move some files every month into a file folder format similar to the below. I have used powershell before to move files to a folder that already exists and I have read some things about powershell creating and naming folders but is there a way to do it monthly without changing the folder name it creates every month?
Client 1
Reports 2018
01Jan
02Feb
03Mar
etc.
Client 2
Reports 2018
01Jan
02Feb
03Mar
etc.
So ideally, it would be a script that would create a folder for the month it is run and then move all files in a directory that were created/modified that month into their monthly folder.
Is this possible?
some code I have tried:
Get-ChildItem \testd$Reports*.xlsx -Recurse | foreach {
$x = $_.LastWriteTime.ToShortDateString()
$new_folder_name = Get-Date $x -Format yyyy.MM
$des_path = "c: est est2$new_folder_name"
if (test-path $des_path){
move-item $_.fullname $des_path
} else {
new-item -ItemType directory -Path $des_path
move-item $_.fullname $des_path
}
}
::
cd "\testd$Reportsclient1"
$fso = new-object -ComObject scripting.filesystemobject
$fso.CreateFolder("3Mar")
cd "\testd$Reportsclient2"
$fso = new-object -ComObject scripting.filesystemobject
$fso.CreateFolder("3Mar")
repeat for 20 clients but as you could tell this gets hard to keep up with every month since you have to go update the 03 to 04 and mar to april and further on.
EDIT:
As requested the structure is as follows:
Client 1 - folder
Saved Reports - folder
2017 - folder
01Jan - folder
report 1
02Feb - folder
2018 - folder
01Jan - folder
02Feb - folder
Repeating for 20+ clients
Also EDIT:
# Get the files which should be moved, without folders
$files = Get-ChildItem '\testd$Reportsclientsaved reports' -Recurse | where {!$_.PsIsContainer}
# List Files which will be moved
$files
# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = '\testd$Reportsclient2018 client reports'
foreach ($file in $files)
{
# Get year and Month of the file
# I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString("00")
$monthname = (Get-Culture).DateTimeFormat.GetAbbreviatedMonthName($month)
# Out FileName, year and month
$file.Name
$year
$month
$monthname
# Set Directory Path
$Directory = $targetPath + "" + $month + $monthname
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
# Move File to new location
$file | Move-Item -Destination $Directory
}
The above seems to be working, but I have to have this entire block of code for every client and will have to change the year each time, any way to generalize this or not?
Last Edit:
Also -- is there a way to do $month -1 so the folder says the last month name not the current month name?
Thanks,
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…