How to compare if a folder exists, and if it does not exist, create it

I have the following problem: I need to create a script that compares if the directory exists, and if it does not exist, create it. In linux shell, I use parameter -Fto check if directory exists. How to check in PowerShell?

In the Linux shell:

DIR=FOLDER

if [ -f $DIR ]
then
    echo "FOLDER EXIST";
else
    echo "FOLDER NOT EXIST";
    mkdir $DIR
fi

How to make this comparison in Windows PowerShell?

$DIRE = "C:\DIRETORIO"

if ( -e $DIRE ) {
    echo "Directory Exists"
} else {
    md DIRETORIO
}
+4
source share
2 answers

You can also use the cmdlet New-Itemwith a parameter force, you don’t even need to check if the directory exists:

New-Item -Path C:\tmp\test\abc -ItemType Directory -Force
+8
source

Test-Path - PowerShell, :

$DIRE = "C:\DIRETORIO"

if ( Test-Path $DIRE ) {
    echo "Directory Exists"
} else {
    md DIRETORIO
}

Test-Path , . $True, $False, . , .

+3

Source: https://habr.com/ru/post/1675913/


All Articles