Code Monkey home page Code Monkey logo

Comments (4)

jdhitsolutions avatar jdhitsolutions commented on August 10, 2024 1

It is Set-Strictmode that is causing the exception. If you comment that line out you won't get an error. But, I can see where it might help to improve the code in the function.

from psscripttools.

jdhitsolutions avatar jdhitsolutions commented on August 10, 2024

I can see where this could be a problem with an empty folder. But I can't duplicate the problem.

image

What version of PowerShell and Windows (I assume) are you running?

from psscripttools.

Erikov-K avatar Erikov-K commented on August 10, 2024

I run this code on Windows 11 and Windows Server 2019, results were the same.

Set-StrictMode -Version 3.0

$psversiontable
$Path = 'C:\Temp\EmptyFolder'
Remove-Item -Path $Path
New-Item -type Directory -Path $Path

$cPath = (Convert-Path -literalpath $path -ErrorAction Stop)
    
if (Test-Path -literalpath $cPath -ErrorAction Stop) {
    $d = [System.IO.DirectoryInfo]::new($cPath)
    $files = [system.collections.arraylist]::new()

    If ($psversiontable.psversion.major -gt 5) {
        #this .NET class is not available in Windows PowerShell 5.1
        $opt = [System.IO.EnumerationOptions]::new()
        $opt.RecurseSubdirectories = $True
        # https://docs.microsoft.com/en-us/dotnet/api/system.io.fileattributes?view=net-6.0
        $opt.AttributesToSkip = "SparseFile", "ReparsePoint"
        $data = $($d.GetFiles("*", $opt))
        if ($data.count -gt 1) {$files.AddRange($data)}
        elseif ($data.count -eq 1) {[void]($files.Add($data))}
    }
}

image

from psscripttools.

jdhitsolutions avatar jdhitsolutions commented on August 10, 2024

Added better handling of null values in this function in v2.43.0.

from psscripttools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.