0

I am trying to loop the list through the function that i made. What I want to do with the loop is that the variable: "$huidige_folder" inside the function is filled with one location at a time that is in the list, loop and continue with the next location in the list

  1. So first item in the list: "C:\Users\Nick\Desktop\Test1". It uses this location in the function
  2. Then C:\Users\Nick\Desktop\Test2". It uses this location in the function
  3. Then C:\Users\Nick\Desktop\Test3". It uses this location in the function

But when i run the code it will only run the function for the first item in the list. That works and it does what i want. But after that it stops and doenst run the funtion for the other 2 items in the list.

What am i doing wrong?

$path_dagelijkse_controle = "C:\Users\Nick\Desktop\Test1",
                            "C:\Users\Nick\Desktop\Test2", 
                            "C:\Users\Nick\Desktop\Test3"



function Dagcontrole_folders_maken {($huidige_folder)

$Dagelijkse_controle = "Dagelijkse controle"
$datum_vandaag = $(Get-Date).toString('yyyy-MM-dd')
$jaar = $datum_vandaag.Substring(0,4)
$maand = $datum_vandaag.substring(5, 2)
$dag = (get-date).DayOfWeek

$path_jaar = Get-Item -Path "$huidige_folder\**" -Include *$jaar*
$path_maand = Get-Item -Path "$huidige_folder\$jaar\**" -Include *$maand*
$folder_maand = Get-Date -UFormat "%m - %B"


if ($path_jaar) {
    Write-Output "Do Nothing"
}
Else {   
    md -Path "$huidige_folder\$jaar"   
}

if ($path_maand) {
  md -Path "$huidige_folder\$jaar\$folder_maand\Dagelijks\$datum_vandaag"  
}
Else {
    md -Path "$huidige_folder\$jaar\$folder_maand"                              # Makes the month folder
    md -Path "$huidige_folder\$jaar\$folder_maand\Dagelijks\$datum_vandaag"     # Makes a folder with current date inside the month folder
}

}


Foreach ($i in $path_dagelijkse_controle) {
    Dagcontrole_folders_maken($i)
    }
2
  • 2
    Change function Dagcontrole_folders_maken {($huidige_folder) to function Dagcontrole_folders_maken ($huidige_folder) { Commented Aug 6, 2020 at 12:08
  • 2
    You can simplify your code substantionaly by using md .. -Force. Commented Aug 6, 2020 at 12:11

2 Answers 2

1

I would change the declaration of your function:

function Dagcontrole_folders_maken ([String]$huidige_folder) {

It means that $huidige_folder is declared as a String. But the main issue was the { character placed at the wrong place. It must be placed after the arguments.

I would also suggest you to parse all the elements within your function. You can write your ForEach into the function and give an array of strings as an argument. That way, your function would work with one or more elements.

function Dagcontrole_folders_maken ([String[]]$huidige_folder) {
ForEach($folder in $huidige_folder) {
...
Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the parentheses around $i.

Unlike most languages, Powershell does not require parentheses around the actual parameters on a function call. They may be doing harm.

Also, thanks to lieven Keersmaekers, the left brace in the definition should be after the parameter definiton, not before it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.