0

I am trying to write a PowerShell function that will basically create a file in the folder with a name that was passed to that function as $fileName argument.

Here is the function:

$mainFolder = "C:\testFolder\"
function checkIfExist($path, $fName) {
    if (!(Test-Path $path)) {
        $path = $mainFolder + "data"
        new-item -name $fName -type "file" -path $path -force   
    }
    else {}
}

The error:

New-Item : Access to the path 'C:\testFolder\data' is denied.
At C:\testFolder\test.ps1:9 char:3
+         New-Item -Name $fileName -Type "file" -Path $path -Force
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\testFolder\data:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

But for some reason if I switch:

New-Item -Name $fileName -Type "file" -Path $path -Force

to:

New-Item -Name "static name.csv" -Type "file" -Path $path -Force

it works perfectly fine.

I run that script as an administrator but still get the same results.

Update:

I run function by using this line:

checkIfExist($fullPath, $fileName)
Ademir Gotov
  • 179
  • 1
  • 14
  • How are you calling the function when it fails? – EBGreen Mar 16 '18 at 15:18
  • Start by getting rid of the parens. Also you should always edit your question to add details not use a comment. – EBGreen Mar 16 '18 at 15:38
  • 1
    Oh, and get rid of the comma too. Powershell functions are called thusly: `Do-Something Parameter OtherParameter` – EBGreen Mar 16 '18 at 15:40
  • If I get rid of the comma it gives me other error messages: `Unexpected token '$fName' in expression or statement.` – Ademir Gotov Mar 16 '18 at 15:52
  • 1
    That error does not match the code that you say you are executing. – EBGreen Mar 16 '18 at 16:02
  • It was bad on my part. I basically changed the call to `checkIfExist($fullPath $fileName)` which I know now is wrong and that's how I got the error. Thank you for your help! – Ademir Gotov Mar 16 '18 at 16:15

1 Answers1

2

You're calling the function incorrectly, Powershell doesn't use the function($param1, $param2) format, instead it uses function $param1 $param2 (using positional parameters) or function -Param1 $param1 -Param2 $param2 (using named parameters).

You only seem to be testing for the folder path, rather than the file itself.

Join-Path can be used to create the full path to the file, which you then test for and only create the file if it doesn't exist.

If you want to add a data sub-folder, just pass it into the function with the param.

function checkIfExist($path, $fileName) {
    $fullpath = Join-Path $path $fileName
    if (!(Test-Path $fullpath)) {
        New-Item -Path $path -Name $fileName -ItemType File -Force
    }
}

#example 1
checkIfExist "C:\folder" "file.csv"

#example 2
checkIfExist "C:\folder\data" "anotherfile.csv"
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • My error was that I called the function in a wrong matter, thanks for pointing it out. I just picked Powershell yesterday and assumed that the methods are called in the same manner as in Java. Thank you very much for your help. – Ademir Gotov Mar 16 '18 at 16:14
  • 1
    Just for your edification ***methods*** are called like Java using dot notation and parens: $someObject.DoSomething($thing). ***Functions*** are not called that way. – EBGreen Mar 16 '18 at 17:18