Recursive issue in Powershell

Maglio

Weaksauce
Joined
Feb 15, 2008
Messages
64
i'm pretty new to powershell, and haven't been able to figure out a solution to the following problem.

the company i work for need to retain files from terminated workers for a certain amount of time. i am trying to automate the task through a powershell script. for the most part i have it figured out how to pull only the files i want from the folders i want.

the issue i have is with the desktop folder. i need it to pull all information whether it is on the desktop or in a folder that is on the desktop. the problem comes in with the copy-item -recurse function. if there are no extra folders on the desktop, it will error. if i don't include the recurse option, it will only pull in the data on the desktop and no data from the subfolders.

so my question is this...using the powershell script, how would i check for subfolders and then if subfolders exist, run the command with the recurse option, else run it without.

if there is anything i'm missing, please let me know. like i said, i'm new to powershell and am still trying to figure it out.

Mag
 
How about something like this at the top of your script? (update $yourObject)

trap [System.Exception]
{
write-host ("Exception caught: " + $yourObject.Exception.GetType().Name);
continue;
}
 
actually, just got it figured out. you can actually do if/then statement.

here is what i found works for me:
if (Get-ChildItem $source -force | Where-Object {$_.mode -match "d"}) {Get-ChildItem $source -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $fnlDest $_.FullName.Substring($source.length)}}
else {Copy-Item -Recurse $source $fnlDest}

hope that helps anyone else looking for a similar situation
 
Back
Top