Foreach - filecopy script help.

Hi all,

I’m hoping someone could assist me with part of a script. I need to make a slight amendment but I’m not sure how to.

Here is the part I need to change…

$CopyDir = “C:\logs”

$a = Get-ChildItem $CopyDir
foreach($x in $a)
{
$y = ((Get-Date) - $x.CreationTime).Days
if ($y -gt 90 -and $x.PsISContainer -ne $True)
{$x.Delete()}
}

What I would like to do is not delete the files older than 90 day, but move them to another folder. I understand what the script is doing but I’m not sure how to go about changing it from deleting the files to moving them, could someone help me understand? I’ll be looking for more information but thought I would post here first as you’ve been so helpful in the past.

Thanks!

So obviously $x.Delete() is the part that needs to be changed. In this case we want to do something like this:

$CopyDir = “C:\logs”
$a = Get-ChildItem $CopyDir
foreach($x in $a)
{
$y = ((Get-Date) – $x.CreationTime).Days
if ($y -gt 90 -and $x.PsISContainer -ne $True)
{
Move-Item -Path $x.FullName -Destination “C:\DestinationFolder”
}
}

Basically we’re using the Move-Item cmdlet to move the file $x by telling it that the path to the file we want to move is $x.FullName (FullName is the absolute path to the file plus the file name) and that the destination folder is C:\DestinationFolder. Obviously, you can change the destination to anything you like; you could even make it a variable if you want to.

off-topic edit: the code tags seem really buggy on these forums for some reason; I wonder what’s up with that

I don’t care for the Code tags on these forums either; I always manually surround my code with <Pre> tags instead.

Would Remove-Item be of help?

Good morning,

thanks for all your responses.

Nacimota - I had been trying to use the move-item cmdlet, but I was having problems incorporating this into my script. I’ll give this a whirl today, thanks

Dave - just a suggestion, but do you think it would be possible to have an “answered” button on the forum? I’ve seen it on other sites and it seems to work quite well for pointing out the best answer to the original question.

Dave - I think using move-item will be better for my task. Do you think it’s better to use remove-item?

Thanks again guys!

Hi Nacimota,

for some reason the script doesn’t seem to work and I receive no error message? The only parts I’ve change is the “-gt” parameter from 90 to 1 (as my test files aren’t 90 days old) and the destination parameter.

Any suggestions?

Heres the script…

$CopyDir = “C:\Output_logs_from_tests”

$a = Get-ChildItem $CopyDir
foreach($x in $a)
{
$y = ((Get-Date) – $x.CreationTime).Days
if ($y -gt 1 -and $x.PsISContainer -ne $True)
{
Move-Item -Path $x.FullName -Destination “C:\testfolder”
}
}

That would be a better question for Don, but I don’t think the current forum software supports that. It could be added as a custom feature, but that work would have to be repeated every time there’s a patch, which is annoying and time consuming.

Move-Item is appropriate for what you’re trying to do. I’m not sure what Remove-Item would accomplish here. You can add some output to the code to see what’s happening:

# For debugging:
$VerbosePreference = 'Continue'
$WhatIfPreference = $true

#$CopyDir = "C:\Output_logs_from_tests"
$CopyDir = "C:\temp"

$a = Get-ChildItem $CopyDir
foreach($x in $a)
{
    $y = ((Get-Date) – $x.CreationTime).Days
    
    Write-Verbose "$($x.FullName) was created $y days ago. (Is Directory? $($x.PSIsContainer))"

    if ($y -gt 1 -and $x.PsISContainer -ne $True)
    {
        Move-Item -Path $x.FullName -Destination "C:\testfolder"
    }
}

# Restoring the defaults for this session:
$VerbosePreference = 'SilentlyContinue'
$WhatIfPreference = $false

This code was probably written back when PowerShell 2.0 was current. If you’re using 3.0 or later, you can use the -File switch on Get-ChildItem instead of having to check the PSIsContainer property on every object.

Hi,

I got it to work using the script below. Do you foresee any issue from doing it this way?

foreach ($a in Get-ChildItem C:\Output_logs_from_tests) { if ($a.CreationTime -lt ($(Get-Date).Adddays(-1))) { move-Item $a.FullName C:\testfolder } }

Thanks.

[quote=12695]I got it to work using the script below. Do you foresee any issue from doing it this way?[/quote]As long as you understand the difference between this and your original script.

Your original script is saying “for each object in the directory ‘C:\logs’ that is older than 90 days and is not a folder, delete that object”

Your new script is saying “for each object in the directory ‘C:\Output_logs_from_tests’ that is more than one day old, move that object to ‘C:\testfolder’”

The key differences being (apart from delete/move and the directory names) that the first script only works on files that are older than 90 days whereas the new script works on files AND folders that are older than 1 day. Is this what you want to happen? It doesn’t seem consistent with your original post.

Hi Nacimota,

thanks for your assistance.

The original script I posted I wanted to change, I asked for help making it into a script for moving files as the original one deleted files. You provided a version of the script that moved files instead of deleting but I couldn’t get it to work.

The changes of directory names are not of great concern, they were changed while I was testing. I realise I should have kept them consistent on this thread to save confusion, sorry. I changed the days from 90 to 1 for testing purposes (the files I was working with were all less than 90 days).

Thanks for pointing out that the latest version of the script will move files and folders, I shall keep that in mind. I think for the purpose of this script it will be ok.

Once again thanks for your help.

Regards,

John.