Question

I would like to change last access time on files by adding +20 years in future, in windows 2008 environment, I tried with by installing unix utility on my windows server and used touch –a –t command it works but as I have every day new files In my path folder z:\data\backup\*.bkf so second time I’m not able to modify new files by skipping the old ones, which are already modified.

e.g. Command & output:

C:\unix\wbin>touch -a -t 120312002033 z:\data\backup*.bkf

Info: all files *.bkf in the path modified as follows:

created: Today 9th December 2013 modify: Today 9th December 2013 accessed: Saturday 3rd December 2033

e.g. Imagine today I have 1000 files in In my path folder z:\data\backup\*.bkf on which I already changed the last access time to +20 years and tomorrow I will have 1500 more files in the same folder with extension .bkf, so now I would like to change only those new files and take them to +20 years and keep on doing like this every day by skipping the already done files and touching only new ones, by running a batch.

Please Note: once I change the last access time and add +20 years, these files will be immediate locked in folder z:\data\backup for 20 years and cannot be deleted or modify but one can only read them, this is a Retention lock feature of EMC Data Domain.

I`m not a cli or powershell expert. I really appreciate your help and thanks a lot in advance for your time.

Best Regards, Bobby.

Was it helpful?

Solution

Well i give you something to work with:

get-childitem | select LastWriteTime

finally if you want to check each file for a specific date use:

get-childitem | foreach-object {
    if ($_.lastwritetime.year -eq 2013) {
        write-host $_.Name was created 2013
    } else {
        write-host $_ was not created 2013
    }
}

In Batch you would do it like this:

for /d %%f in (*) do @echo %%f is born on %%~tf

so you could in theory have something like:

@echo off
setlocal ENABLEDELAYEDEXPANSION
for %%f in (*) do (
    set FILE_DATE="%%~tf"
    set FILE_YEAR=!FILE_DATE:~7,4!
    if !FILE_YEAR! == 2013 (
        echo %%f was born in 2013 
    ) else (
        echo %%f was not born in 2013
    )
)

But be carefull, first see what %%~tf will output on your machine, since the format of DateTime output often varies in different cultures. Essentially you have to work on the substring in !FILE_DATE!

Final Edit:

So this is as far as I go doing your job :)

new-variable now -value (date)
new-variable nact -value ((date).addyears(20))
get-childitem *.bkf | foreach-object {
    set-variable nact -value (($_.lastaccesstime).addyears(20))
    if ($_.lastaccesstime.year -le $now.year) {
        write-host [$now]: Updateing LastAccessTime of $_.name to $nact
        $_.lastaccesstime = ($nact)
    } else {
        write-host [$now]: Ignoring $_.name
    }
}

This will look at every *.bkf file in the current folder, and if it's last access time by year is lesser or equal to the current year, it will add 20 years to it's last access time. Every other file is left alone.

OTHER TIPS

I personally would do this using C or something similar (not necessary but this is because I worked on something similar few days ago)

You could run over your files using opendir and readdir then stat or lstat all your files one by one you will then get a struct with a field st_atime you can compare it to time() because if st_atime is greater then the current timestamp then you are looking at a already modified one, if not, just update the st_atime.

Hope I was able to help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top