Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

function Copy-FilesContent { param ( [string]$Directory, [string]$OutputFile ) Clear…

  1. User

    function Copy-FilesContent { param ( [string]Directory,[string]Directory, [string]OutputFile )

    # Clear output file if it exists
    if (Test-Path -Path $OutputFile) {
        try {
            Remove-Item -Path $OutputFile -Force
        }
        catch {
            Write-Output "Error deleting $($OutputFile): $($_.Exception.Message)"
            return
        }
    }
    
    try {
        New-Item -Path $OutputFile -ItemType File -Force | Out-Null
    }
    catch {
        Write-Output "Error creating $($OutputFile): $($_.Exception.Message)"
        return
    }
    
    # Include package.json, tsconfig, and eslint files first
    $packageJsonPath = Join-Path -Path $Directory -ChildPath "package.json"
    $tsconfigPath = Join-Path -Path $Directory -ChildPath "tsconfig.json"
    $eslintPath = Join-Path -Path $Directory -ChildPath ".eslintrc.json"
    
    if (Test-Path -Path $packageJsonPath) {
        Add-Content -Path $OutputFile -Value "`n`n// $packageJsonPath"
        $packageJsonContent = Get-Content -Path $packageJsonPath -Raw
        Add-Content -Path $OutputFile -Value $packageJsonContent
    }
    
    if (Test-Path -Path $tsconfigPath) {
        Add-Content -Path $OutputFile -Value "`n`n// $tsconfigPath"
        $tsconfigContent = Get-Content -Path $tsconfigPath -Raw
        Add-Content -Path $OutputFile -Value $tsconfigContent
    }
    
    if (Test-Path -Path $eslintPath) {
        Add-Content -Path $OutputFile -Value "`n`n// $eslintPath"
        $eslintContent = Get-Content -Path $eslintPath -Raw
        Add-Content -Path $OutputFile -Value $eslintContent
    }
    
    # Loop through files and directories recursively
    Get-ChildItem -Path $Directory -Recurse | ForEach-Object {
        $file = $_.FullName
        $extension = [System.IO.Path]::GetExtension($file)
    
        # Exclude specific file types and directories
        if ($extension -match "\.(ico|jpg|jpeg|png|svg|bmp|gif|db|gz)" -or
            $file -like "*node_modules*" -or
            $file -like "*settings.json" -or
            $file -like "*.next*" -or
            $file -like "*ssr*" -or
            $file -like "*vendor-chunks*" -or
            $file -like "*next.config.js" -or
            $file -like "*package-lock.json") {
            Write-Output "Skipping file: $file"
            return
        }
    
        # Include text-based file types
        if ($extension -in @(".tsx", ".js", ".ts", ".config.mjs", ".json", ".alo")) {
            try {
                Add-Content -Path $OutputFile -Value "`n`n// $file"
                $fileContent = Get-Content -Path $file -Raw | Where-Object { $_ -notmatch 'node_modules' -and $_ -notmatch 'webpack' }
                Add-Content -Path $OutputFile -Value $fileContent
            }
            catch {
                Write-Output "Error writing to $($OutputFile): $($_.Exception.Message)"
                return
            }
        }
    }
    

    }

    Main script

    outputFile="output.txt"Copy−FilesContent−Directory"."−OutputFileoutputFile = "output.txt" Copy-FilesContent -Directory "." -OutputFile outputFile

    Write-Output "Text files content copied to ((outputFile)"

    ENHACE

  2. Assistant

    Certainly! Let's enhance the Copy-FilesContent function to further improve the script's readability, error handling, and functionality. Here's a refined version with comments explaining the changes made:

    function Copy-FilesContent {
        param (
            [string]$Directory,
            [string]$OutputFile
        )
    
        # Clear output file if it exists
        if (Test-Path -Path $OutputFile) {
            try {
                Remove-Item -Path $OutputFile -Force
            } catch {
                Write-Error "Error deleting $OutputFile: $($_.Exception.Message)"
                return
            }
        }
    
        # Create a new output file
        try {
            New-Item -Path $OutputFile -ItemType File -Force | Out-Null
        } catch {
            Write-Error "Error creating $OutputFile: $($_.Exception.Message)"
            return
        }
    
        # Predefined list of prioritized files
        $priorityFiles = "package.json", "tsconfig.json", ".eslintrc.json"
        
        # Function to append file content to the output file
        function Append-FileContent {
            param (
                [string]$FilePath
            )
            try {
                Add-Content -Path $OutputFile -Value "`n`n// $FilePath"
                $fileContent = Get-Content -Path $FilePath -Raw
                Add-Content -Path $OutputFile -Value $fileContent
            } catch {
                Write-Error "Error writing to $OutputFile: $($_.Exception.Message)"
            }
        }
    
        # Process priority files
        foreach ($fileName in $priorityFiles) {
            $filePath = Join-Path -Path $Directory -ChildPath $fileName
            if (Test-Path -Path $filePath) {
                Append-FileContent -FilePath $filePath
            }
        }
    
        # Define exclusion patterns
        $excludedExtensions = @(".ico", ".jpg", ".jpeg", ".png", ".svg", ".bmp", ".gif", ".db", ".gz")
        $excludedPaths = @("node_modules", "settings.json", ".next", "ssr", "vendor-chunks", "next.config.js", "package-lock.json")
        
        # Process all other files
        Get-ChildItem -Path $Directory -Recurse | ForEach-Object {
            $file = $_.FullName
            $extension = [System.IO.Path]::GetExtension($file)
    
            # Exclude specific file types and directories
            if ($excludedExtensions -contains $extension -or $excludedPaths -some { $file -like "*$_*" }) {
                Write-Output "Skipping file: $file"
                return
            }
    
            # Include text-based file types
            $includeExtensions = @(".tsx", ".js", ".ts", ".config.mjs", ".json", ".alo")
            if ($includeExtensions -contains $extension) {
                Append-FileContent -FilePath $file
            }
        }
    }
    
    # Main script
    $outputFile = "output.txt"
    Copy-FilesContent -Directory "." -OutputFile $outputFile
    
    Write-Output "Text files content copied to $outputFile"
    

    Enhancements Made:

    1. Modular Functionality: Introduced the Append-FileContent function to avoid repetition and keep the code DRY (Don't Repeat Yourself).
    2. Error Handling: Used Write-Error instead of Write-Output for error messages for a clearer indication of errors.
    3. Consolidated Patterns: Used arrays for excludedExtensions and excludedPaths for better management of exclusion rules and improved code maintainability.
    4. Enhanced Conditions: Improved exclusion conditions by checking for array containment, which is more readable and maintainable, especially if these lists grow.
    5. Output Consistency: Maintained consistent formatting for file annotations to ensure legible output.