Working fith files and folders

I am trying to first of all detect if one specifik folder is having any files *.xml so far so good.
Now to my problem. There is multiple folders and under them there are one error folder and if that error folder is holding any *.xml that shall be moved to another directory above error directory.
And if that new folder don’t exists that shall be created and then the *.xml file shall be moved to that place.

So far i have this for control and to create but I always get NULL. All the other stuff is working fine.

$Folder = Get-ChildItem 'C:\PSTemp\' -Recurse -Filter *test* -Force -ErrorAction SilentlyContinue
if (Test-Path -Path $Folder){
} else {
    MD $Folder

Do you know the name of the folder in advance? Is *test* the name pattern for the folder? When you’re looking for folders you should specify trhe parameter -Directory for your Get-ChildItem command. Will the XML files have a name or at least a part of their name you know in advance?

If there is no such folder your result variable $Folder is empty or not existing. You should test for these properties before you test the existence with Test-Path. ( mehtod .IsNullOrEmpty() )

When you post error messages you should format them as code as well. :wink:

Thanks in advance

Hmmmm … at the moment you are looking for ANY file OR folder with the string test in its name.

$Folder = Get-ChildItem 'C:\PSTemp\' -Recurse -Filter *test* -Force -ErrorAction SilentlyContinue

If you want to check if there is something in your variable $Folder you simply test it:

    'there is something with the string "test" in its name'
    'there is nothing with the string "test" in its name'

If you want to look only for folders with the string “test” in its name and containing some XML files you will have to use a condition:

$Folder = 
    Get-ChildItem 'C:\PSTemp' -Filter '*test*' -Recurse -Force -Directory -ErrorAction SilentlyContinue | 
        Where-Object { Get-ChildItem -Path $_.FullName -Filter *.xml }

It’s hard to explain.
The code to see * .xml files works and that it is only in the directory error it should check also works.
The problem is when there is a * .xml file in the directory it should be moved to another directory but if that directory is missing it should be created first and then the * .xml file should be moved to the newly created directory.
The new directory will be created on the same level as the error directory where there are already three directories including the error directory.

So you check first if this desired target directory exists with Test-Path. If it does not exist you create it with New-Item.
When you successfully created the target directory or if it’s already there you use Move-Item to move the needed files to the target folder.

OK, and what is your question? I already explained how to do it.

This forum is more about “we can help you with your code if it’s not working as expected” than “we will write your code for you on request” :wink:

Thanks for the tip and help. I think I can get this done now.