# Set the source and destination directories
$sourceDirectory = "D:\Temp\Source"
$destinationDirectory = "D:\Temp\Target"

# Set the file types to move (e.g., *.txt, *.log)
$fileTypes = "*.tvl", "*.lvl"

# Set the number of days after which to move files
$days = 1

# Get the current date
$currentDate = Get-Date

# Get the files that match the criteria
$filesToMove = Get-ChildItem -Path $sourceDirectory -Recurse -Include $fileTypes | Where-Object {
    $_.LastWriteTime -lt ($currentDate.AddDays(-$days))
}

# Move the files, overwriting if they already exist
foreach ($file in $filesToMove) {
    Move-Item -Path $file.FullName -Destination $destinationDirectory -Force
}

Explanation:

  • Set the directories: Replace "C:\SourceDirectory" and "C:\DestinationDirectory" with the actual source and destination paths.
  • Set file types: Replace ` “.txt”, “.log”` with the desired file extensions.
  • Set the number of days: Modify $days to specify the age threshold for moving files.
  • Get the current date: The script calculates the cutoff date by subtracting the specified number of days from the current date.
  • Get the files to move: The Get-ChildItem cmdlet retrieves files from the source directory that match the specified file types and are older than the specified number of days.
  • Move the files: The Move-Item cmdlet moves each file from the source directory to the destination directory.
Last modified: October 31, 2024

Author

Comments

Write a Reply or Comment