Hi Powershell followers
my name is davide i am learning powershell and i am practising creating a tool to organize files in subdirectories
Actual directory structure multiple files in multiple folders
Dir\Dir\file.*
Dir\Dir\file.*
Dir\file.*
file*
Desired Directory structure
Year\Year_MM_DD\YYYYMMDD_HHmmss.*
Year\Year_MM_DD\YYYYMMDD_HHmmss_1.*
Year\Year_MM_DD\YYYYMMDD_HHmmss_2.*
(the _count is added in case of duplicates names) and seems to work
I used some codes find here and there and trying to put it toghether.
it seams to work partially
some file are renamed and moved properly but some remain in the original folder (some renamed and some with original name)
Here is what i have done so far.
(feel free to move or format the code as per Good Practice format so i can also learn how shoud be done)
if you are testing make sure to have a test folder with some test files
do not use on persolan files as they are renamed and moved
i dont want you to lose or damage your personal files
[Reflection.Assembly]::LoadFile('C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Drawing.dll') | Out-Null
Function Select-FolderDialog
{
param([string]$Description="Select Folder",[string]$RootFolder="Desktop")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
#Function to promp guest to select the Working Folder
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Rootfolder = $RootFolder
$objForm.Description = $Description
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.SelectedPath
}
Else
{
Write-Error "Operation cancelled by user."
}
}
$folder = Select-FolderDialog
$folder
pause
#Desired Destination Directory Structure
$des_path = "$folder\$year\$new_folder_name"
#Variables
#get the LastwriteTime to rename the Folders
$x = $_.LastWriteTime.ToShortDateString()
#get the Year date to rename the Folders
$year = Get-Date $x -Format yyyy
$new_folder_name = Get-Date $x -Format yyyy_MM_dd
#Trim filename without sequencenumbers
$TrimmedName= $_.lastwritetime.tostring("yyyyMMdd_hhmmss")
# Get extension of filename
$ext= $_.Extension
# Get filename without sequencenumber but with extension
$FullName= $TrimmedName + $ext
# Get the path to be used
$newpath = Join-Path -Path $des_path -ChildPath $FullName
# Reset numerator in case trimmed file name is in use
$num= 1
#Command
Get-ChildItem $folder\*.* -Recurse | ForEach-Object {
# Make sure we don't fail the Test-Path because it finds and conflicts with itself
if(!($NewPath -eq $_.FullName)){
# If path exists loop untill there is a free sequence number
if (Test-Path $newpath) {
while ((Test-Path $newpath))
{
$newpath = Join-Path $des_path ($TrimmedName + "_$num" + $ext)
if(!($NewPath -eq $_.FullName)){
$num+=1
}
else{break}
}
}
# If path does not exist rename file without a sequence number
$_.MoveTo($newpath)
else
if (test-path $des_path){
Rename-Item $_.Fullname $FullName
Move-Item $FullName $des_path
} else {
new-item -ItemType directory -Path $des_path
Rename-Item $_.Fullname $FullName
Move-Item $FullName $des_path
}
}
}