Jump to content

Help to create a script to sort files based on timestamp.

fabiosapplou

Hi guys, so, i have an issue were i need to somehow separate a lot of files based on the time the file was created, and i thought a good way to do that would be with a script of some sort, that would take an input folder, check the time every file was created and if it was created between specific range of time it would copy or move the file to another folder. Does anyone have any ideas on how to pull this off? Thank you very much for the help.

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Powershell script, save it as a .ps1 file and you can run it. Seems to work fine, though annoying how it defaults to mm/dd/yyyy for the CreationTime property. 

 

$source = Read-Host "Enter Source Directory"
$destination = Read-Host "Enter Destination Directory"
$date1 = Read-Host "Enter Start Date (MM/DD/YYYY)"
$date2 = Read-Host "Enter End Date (MM/DD/YYYY)"

$files = Get-ChildItem -File -Path $source | Select-Object -Property Name, CreationTime, FullName | Where-Object {$_.CreationTime -ge "$date1" -AND $_.CreationTime -le "$date2"}

foreach ($file in $files)
{Move-Item $file.fullname $destination}

 

 

EDIT: You can also specify times. Just enter it like: 09/01/2021 21:00

Link to comment
Share on other sites

Link to post
Share on other sites

If you use Total Commander - https://www.ghisler.com/download.htm - , you can select files in folder by specific rules.

for example you can say select all files with date between 1st of august and 31st of august, which contain the word "test"

Then simply drag them to the other panel to move them (or press F6 key which is shortcut for move)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 year later...
  • 4 weeks later...
On 9/1/2021 at 5:04 AM, fabiosapplou said:

Hi guys, so, i have an issue were i need to somehow separate a lot of files based on the time the file was created, and i thought a good way to do that would be with a script of some sort, that would take an input folder, check the time every file was created and if it was created between specific range of time it would copy or move the file to another folder. Does anyone have any ideas on how to pull this off? Thank you very much for the help.

 

 

 

 

should be pretty easy to do with python if thats an option

 

import os
import shutil
import time

# Define a function that takes in the source folder, destination folder, and the start and end timestamps
def move_files(src_folder, dest_folder, start_time, end_time):
  
    # Loop through each file in the source folder
    for file_name in os.listdir(src_folder):
      
        # Construct the full file path
        file_path = os.path.join(src_folder, file_name)
        
        # Get the creation time of the file
        file_time = os.path.getctime(file_path)
        
        # Check if the creation time of the file is between the start and end timestamps
        if start_time <= file_time <= end_time:
          
            # Construct the full destination path
            dest_path = os.path.join(dest_folder, file_name)
            
            # Move the file to the destination folder
            shutil.move(file_path, dest_path)

# Define the source folder and destination folder paths
src_folder = '/src/folder'
dest_folder = '/dest/folder'

# Convert the start and end timestamps from strings to Unix timestamps
start_time = time.mktime(time.strptime("2022-01-01", "%Y-%m-%d"))
end_time = time.mktime(time.strptime("2022-12-31", "%Y-%m-%d"))

# Call the `move_files` function with the source folder, destination folder, and start and end timestamps
move_files(src_folder, dest_folder, start_time, end_time)

it worked for me.

 

[edit]

 

it seems that it can also be done with bash and the find command with `-newermt` argument. shouldn't be that hard but im not very sure. ill update again if i find a solution

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×