Mattia Minervini
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
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
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
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
ASKER
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...
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.
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.
ASKER
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"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
we not need.
i hope files will have original CREATE DATE (and not date when we move...)
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.
ASKER
it works
ASKER
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...
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 ....
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
}
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.
ASKER
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
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:
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
}
ASKER
missed "-destination"!!! now i'm trying
thanks man
thanks man
ASKER
sorry man.this is more complex, maybe i didn't explain clearly.
Subfolder on destination are not present, so script goes in error for
"Directorynotfoundexceptio n"
So, if there's not that directory, script should create it
Subfolder on destination are not present, so script goes in error for
"Directorynotfoundexceptio
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
}
ASKER
you have a coffee for free in Rome..
=;
=;
Open in new window
Put the above into a .ps1 file as e.g. C:\Scripts\MoveVoice.ps1. The scheduled task should consist ofOpen 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.