Link to home
Start Free TrialLog in
Avatar of Mattia Minervini
Mattia MinerviniFlag for Italy

asked on

batch solution to move file older than 36 hours, then delete these files from source

Hi all, i have this kind of situation.
Folder C\:Regs where i store call of my voice solution
Voice solution copy at least 1 month, for compliance reasons i have to preserve ONLY 36 hours.
But i need to mantain REALLY 1 month!

So from C:\reg every 36 hours i need to :
MOVE .WAV FILE OLDER THAN 36 HOURS TO C:\SecretBackupReg
THEN DELETE THESE FILE

Maybei need t run this job every hour..
My OS i windows 2008 R2 std

I prefer a solution usable with windows scheduler, no a third party sw.
Really thanks ask me for details!
Mattia
Avatar of Qlemo
Qlemo
Flag of Germany image

The term "move" already implies to "remove" the original file, otherwise it would be a copy. So a simple move will do for your purpose. The only remaining issue is to limit to "old" files. Using PowerShell is easy, a cmd.exe batch is more complex, so I would not use that.
$old = (get-date).Addhours(-36)
get-childitem c:\Regs\*.wav | ? { $_.CreationTime -le $old } | move-item C:\SecretBackupReg\

Open in new window

Put the above into a .ps1 file as e.g. C:\Scripts\MoveVoice.ps1. The scheduled task should consist of
powershell -Windowstyle hidden -command C:\Scripts\MoveVoice.ps1

Open in new window

and scheduled as you like best. But I don't think hourly is necessary - once in a while, e.g. every 8 hours, should suffice.
Avatar of Mattia Minervini

ASKER

you have reason about MOVE.
maybe it's better to do
COPY first
DELETE after
to avoid MOVE problem on network..
isn't right?

how i have to modify?
thanks
another problem, sorry.
into C:\reg there are unpredictable subfolder
001
002
maybe for each month

so script running on 06 june have to check into 006 and so on...
"unpredictable" is an issue. We can only look for the date of the files, either in all folders or a particular one.
And no, "copy then delete" creates more issues than it solves. The move is atomic, i.e. if it fails the file remains in the source location.

I don't think "unpredictable" is the correct term. You see what happens, and the system uses a fixed algorithm to create files. There is nothing unpredictable - you might not know the exact rules maybe, but it cannot be complicated.
Ok, you're right.
Sure algorithm is simple.
I think procedure create a folder for each month, so we need to scan all subfolder in C:\reg
Move is ok, instead of "copy and delete"
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
we not need.
i hope files will have original CREATE DATE (and not date when we move...)
A move should transfer the dates as-is, so no worries.
it works
Hi Qlemo
i don't know if i can reopen this question.
I need to maintain the folder in the new target location C:\SecretBackupReg...
No, you cannot reopen the question, only Topic Advisors, Moderators or Admins can do.

To keep the folder structure we need some more logic. Bad luck we cannot go for "days" instead of hours - that would be a single RoboCopy command to do everything ....
$src = 'C:\Regs\'
$dst = 'C:\SecretBackupReg\'

$old = (get-date).Addhours(-36)
get-childitem -recursive $src *.wav | ? { $_.CreationTime -le $old } |
  % {
     move-item $_ $_.FullName.Replace($src,$dst) -whatIf
  }

Open in new window

Remove the -whatIf to actually perform the move, with the command will only write out what it would do. That way you can check if the paths and names are correct.
Hi...sorry
it was a lon g time ago
I'm trying now, this no works cause of this error in $src,$dst piece...
(i'm translating from italian)

Impossible to convert 'System.Object[]' in type 'System.String' requested by parameter 'Destination'

Can you help me again?
Really Thanks
Interesting. My lines above contain a syntax error, so you can, by no means, have run it. Because the script fails at -recursive.
But let's try this:
$src = 'C:\Regs\'
$dst = 'C:\SecretBackupReg\'

$old = (get-date).Addhours(-36)
get-childitem -recurse $src *.wav | ? { $_.CreationTime -le $old } |
  % {
     $_ | move-item -Destination $_.FullName.Replace($src,$dst) -whatIf
  }

Open in new window

missed "-destination"!!! now i'm trying
thanks man
sorry man.this is more complex, maybe i didn't explain clearly.
Subfolder on destination are not present, so script goes in error for
"Directorynotfoundexception"
So, if there's not that directory, script should create it
I should have expected that ...
$src = 'C:\Regs\'
$dst = 'C:\SecretBackupReg\'

$old = (get-date).Addhours(-36)
get-childitem -recurse $src *.wav | ? { $_.CreationTime -le $old } |
  % {
     $newLoc = $_.DirectoryName.Replace($src,$dst) 
     if (!(test-path $newLoc)) { md $newLoc }
     $_ | move-item -Destination $newLoc -whatIf
  }

Open in new window

you have a coffee for free in Rome..
=;