cluster files in folders by its modified date
Posted: 28 Jan 2025 16:42
How can I move files in folders by its modified date?
Forum for XYplorer Users and Developers
https://www.xyplorer.com/xyfc/
Code: Select all
setting "BackgroundFileOps", 0;
foreach($file, <get SelectedItemsPathNames>, <crlf>, "e") {
$dst = gpc($file, "path") . "\" . formatdate(property("#date.m", $file), "yyyy-mm-dd");
moveto $dst, $file, , 2, 2;
}
There are several thousand files and can take several days in manual operation.
Thank you! I will try it on next phase. I managed it using powershell script currently like below.highend wrote: ↑28 Jan 2025 22:26 Via scripting?Code: Select all
setting "BackgroundFileOps", 0; foreach($file, <get SelectedItemsPathNames>, <crlf>, "e") { $dst = gpc($file, "path") . "\" . formatdate(property("#date.m", $file), "yyyy-mm-dd"); moveto $dst, $file, , 2, 2; }
Code: Select all
# PowerShell Script to Organize .log Files by Modified Date
param (
[Parameter(Mandatory = $true)]
[string]$FolderPath
)
# Check if the folder exists
if (-not (Test-Path -Path $FolderPath)) {
Write-Host "The specified folder does not exist. Exiting..." -ForegroundColor Red
exit
}
# Get all .log files in the folder
$logFiles = Get-ChildItem -Path $FolderPath -Filter *.log -File
if ($logFiles.Count -eq 0) {
Write-Host "No .log files found in the specified folder." -ForegroundColor Yellow
exit
}
# Process each .log file
foreach ($file in $logFiles) {
# Get the last modified date of the file
$modifiedDate = $file.LastWriteTime.ToString("\log_yyyy-MM-dd")
# Create a folder with the modified date if it doesn't exist
$dateFolder = Join-Path -Path $FolderPath -ChildPath $modifiedDate
if (-not (Test-Path -Path $dateFolder)) {
New-Item -Path $dateFolder -ItemType Directory | Out-Null
}
# Move the file to the corresponding folder
$destinationPath = Join-Path -Path $dateFolder -ChildPath $file.Name
Move-Item -Path $file.FullName -Destination $destinationPath -Force
Write-Host "Moved '$($file.Name)' to '$($dateFolder)'" -ForegroundColor Green
}
Write-Host "Files successfully organized by modified date." -ForegroundColor Cyan