none
Powershell - test if file exists RRS feed

  • Question

  • Can someone help me with this error handling of this script. I basically need to test to see if a file exists - if it does, copy it if not - dont throw an error. The problem i have is if the files does not exist i get an error when i test the files existance here:

    $DMZ_Test_File1 = Test-Path $DMZ_File

    Cannot bind argument to parameter 'Path' because it is null.

    The f ull code below - all the rest works fine when the file exists

    $DMZ_File = Get-ChildItem 'DMZ_Drive:*.xml' | Where {$_.LastWriteTime -gt (Get-Date).AddHours(-24)} -ErrorAction SilentlyContinue
    $DMZ_Test_File1 = Test-Path $DMZ_File
    If ($DMZ_Test_File1 -eq $true) {Copy-Item $DMZ_File LAN_Drive:\}
    Else {Write-Host "File does not exist"}

    Please excuse my rudimentry coding! :)


    • Edited by ianjhardie Thursday, February 16, 2017 8:32 AM
    Thursday, February 16, 2017 8:31 AM

Answers

  • Hi Ian,

    that's because you need to give Test-Path input, even if the path doesn't exist. Since if Get-ChildItem fails there is no content in $DMZ_File, this will cause an exception by the PowerShell Engine (so that's not an issue with Test-Path itself.

    I think the best way to work around that issue is this:

    $DMZ_File = Get-ChildItem 'DMZ_Drive:*.xml' | Where {$_.LastWriteTime -gt (Get-Date).AddHours(-24)} -ErrorAction SilentlyContinue
    If ($DMZ_File.Exists) {Copy-Item $DMZ_File LAN_Drive:\}
    Else {Write-Host "File does not exist"}

    When the file doesn't exist, its "Exists" property is also null, and null is evaluated as false.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by ianjhardie Thursday, February 16, 2017 9:18 AM
    Thursday, February 16, 2017 9:06 AM

All replies

  • Cannotbind argument to parameter 'Path'because it is null.

    You have to use a string.  You cannot use a null to test for a file.

    if($DMZ_File){
         Test-Path $DMZ_File
    }else{
         Write-Host 'you must specify a file name'
         exit
    }


    \_(ツ)_/


    • Edited by jrv Thursday, February 16, 2017 9:02 AM
    • Proposed as answer by SachinWaghmare Thursday, February 16, 2017 10:31 AM
    Thursday, February 16, 2017 9:02 AM
  • I highly recommend that you take some time to learn PowerShell and then al of this will be obvious.

    if($DMZ_File = Get-ChildItem 'DMZ_Drive:*.xml' | Where { $_.LastWriteTime -gt (Get-Date).AddHours(-24) }){
    	Copy-Item $DMZ_File X:\
    }else{
    	Write-Host 'File does not exist'
    }


    \_(ツ)_/

    Thursday, February 16, 2017 9:06 AM
  • Hi Ian,

    that's because you need to give Test-Path input, even if the path doesn't exist. Since if Get-ChildItem fails there is no content in $DMZ_File, this will cause an exception by the PowerShell Engine (so that's not an issue with Test-Path itself.

    I think the best way to work around that issue is this:

    $DMZ_File = Get-ChildItem 'DMZ_Drive:*.xml' | Where {$_.LastWriteTime -gt (Get-Date).AddHours(-24)} -ErrorAction SilentlyContinue
    If ($DMZ_File.Exists) {Copy-Item $DMZ_File LAN_Drive:\}
    Else {Write-Host "File does not exist"}

    When the file doesn't exist, its "Exists" property is also null, and null is evaluated as false.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by ianjhardie Thursday, February 16, 2017 9:18 AM
    Thursday, February 16, 2017 9:06 AM
  • Thanks Fred that was indeed what i needed to do - works perfectly now - thanks!

    $DMZ_Test_File1 = if ($DMZ_File) { Test-Path $DMZ_File }

    Thursday, February 16, 2017 9:14 AM