renombrar_o_eliminar_directorios_por_fecha

This is an old revision of the document!


Table of Contents

Q:

Cómo quedaría este script:

# Set the path to the directory containing the folders
$directoryPath = "$env:APPDATA\Mozilla\Firefox\Profiles"
 
# Get all folders in the directory
$folders = Get-ChildItem -Path $directoryPath -Directory
 
# Sort the folders by their last write time (modification date) in descending order
$latestFolders = $folders | Sort-Object LastWriteTime -Descending | Select-Object -First 2
 
# Loop through all folders and delete the ones that are not the latest
foreach ($folder in $folders) {
    if ($folder.FullName -notin $latestFolders.FullName) {
        Remove-Item -Path $folder.FullName -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
    }
}

pero que en lugar de borrar los directorios, les agregue '_old' en el nombre?

A:

Claro, puedo ayudarte a modificar el script para que en lugar de eliminar los directorios, les cambie el nombre y les agregue “_old” al final. Aquí tienes la versión actualizada del script:

cleanupOldFFProfiles.ps1
# Set the path to the directory containing the folders
$directoryPath = "$env:APPDATA\Mozilla\Firefox\Profiles"
 
# Get all folders in the directory
$folders = Get-ChildItem -Path $directoryPath -Directory
 
# Sort the folders by their last write time (modification date) in descending order
$latestFolders = $folders | Sort-Object LastWriteTime -Descending | Select-Object -First 2
 
# Loop through all folders and rename the ones that are not the latest
foreach ($folder in $folders) {
    if ($folder.FullName -notin $latestFolders.FullName) {
        $newName = "$($folder.Name)_old"
        Rename-Item -Path $folder.FullName -NewName $newName -Force -ErrorAction SilentlyContinue
    }
}

Este script cambia el nombre de los directorios que no son los más recientes agregando “_old” al final del nombre. Los directorios más recientes no se renombran.

Q:

Excelente. Me gustaría agregar ahora lo siguiente: si un directorio de los del grupo '_old' fue modificado hace más de 7 días, se puede eliminar como se hacía originalmente. De esta manera se puede revertir facilmente un cambio, simplemente renombrando el directorio quitando el '_old' mientras estemos dentro de un rango de 7 días.

A:

Por supuesto, puedes lograr eso agregando una verificación de la fecha de modificación de los directorios con sufijo “_old” y eliminándolos si han pasado más de 7 días. Aquí tienes el script actualizado:

cleanupOldFFProfiles.ps1
# Set the path to the directory containing the folders
$directoryPath = "$env:APPDATA\Mozilla\Firefox\Profiles"
 
# Get all folders in the directory
$folders = Get-ChildItem -Path $directoryPath -Directory
 
# Sort the folders by their last write time (modification date) in descending order
$latestFolders = $folders | Sort-Object LastWriteTime -Descending | Select-Object -First 2
 
# Calculate the date 7 days ago
$sevenDaysAgo = (Get-Date).AddDays(-7)
 
# Loop through all folders
foreach ($folder in $folders) {
    # Check if the folder is not in the list of latest folders
    if ($folder.FullName -notin $latestFolders.FullName) {
        # If the folder has "_old" suffix
        if ($folder.Name -like "*_old") {
            # Check if the folder was modified more than 7 days ago
            if ($folder.LastWriteTime -lt $sevenDaysAgo) {
                # Remove the folder
                Remove-Item -Path $folder.FullName -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
            }
        } else {
            # Rename the folder by adding "_old" suffix
            $newName = "$($folder.Name)_old"
            Rename-Item -Path $folder.FullName -NewName $newName -Force -ErrorAction SilentlyContinue
        }
    }
}

Este script verifica si un directorio con sufijo “_old” fue modificado hace más de 7 días. Si es así, elimina el directorio. Si no, renombra el directorio añadiendo “_old” al final del nombre. Los directorios más recientes no se renombran ni eliminan, y los directorios sin sufijo “_old” simplemente se renombran.

renombrar_o_eliminar_directorios_por_fecha.1691546758.txt.gz · Last modified: 2024/10/17 21:42 (external edit)