locked
Need help with renaming files with square brackets in file names. RRS feed

  • Question

  • This script works for renaming incoming files without square brackets, but when I try to run it with square brackets, it breaks.  I have tried using -LiterPath with no positive results.  Any suggestions on what I can do to get this script working?

    ### DO NOT EDIT BELOW THIS LINE ###
    ## Version 1.0.1
                  
         ## Upload Folder

        $inputPath = "F:\phone_tree\incoming"
        $uploadPath = "F:\phone_tree\upload"

        $watcher = New-Object System.IO.FileSystemWatcher
        $watcher.Path = $inputPath
        $watcher.Filter = "*.csv"
        $watcher.IncludeSubdirectories = $false
        $watcher.EnableRaisingEvents = $true
        
        $action = { $path = $Event.SourceEventArgs.FullPath
                     $file_name = (Get-Item $path).Basename
    $outputPath = "F:\phone_tree\upload"
                     
                        Write-Host "$(Get-Date) - Rename request on: $path"
                        Rename-Item $path "01054201.csv"
        $path = "F:\phone_tree\incoming\01054201.csv"
                        Write-Host "$(Get-Date) - File renamed to: 01054201.csv"
                        Move-Item $path $outputPath
                        Write-Host "$(Get-Date) - File moved to upload directory."
                        Write-Host ""
        }

        $uwatcher = New-Object System.IO.FileSystemWatcher
        $uwatcher.Path = $uploadPath
        $uwatcher.Filter = "*.csv"
        $uwatcher.IncludeSubdirectories = $false
        $uwatcher.EnableRaisingEvents = $true
        
        $uaction = { $path = $Event.SourceEventArgs.FullPath
                     $file_name = (Get-Item $path).Basename
                     
                        Write-Host "$(Get-Date) - Upload request on: $path"
                        pscp -pw ****** $path sftp10542@sftp.****.net:/upload
                        Write-Host "$(Get-Date) - File uploaded to Televox: $file_name"
                        Remove-Item $path
                        Write-Host "$(Get-Date) - Source file deleted: $file_name"
                        Write-Host ""
        }

        ## Start
        Register-ObjectEvent $watcher "Changed" -Action $action
        Register-ObjectEvent $watcher "Renamed" -Action $action
        Register-ObjectEvent $uwatcher "Created" -Action $uaction
        Write-Host "Version 1.0.1. Watching folder for .csv files: $inputPath"
        Write-Host "Version 1.0.1. Watching folder for .csv files: $uploadPath"
        Write-Host ""
        Write-Host "$(Get-Date) - Phonetree server started successfully."
        
        # Mising File Checks
        do{
            Get-ChildItem $inputPath -Filter *.csv | 
            Foreach-Object {
                $path = $_.FullName
                $file_name_ext = [io.path]::GetFileNameWithoutExtension($path) + ".csv"
                $lastWrite = (get-item $path).LastWriteTime
                $timespan = new-timespan -days 0 -hours 0 -minutes 5

                if (((get-date) - $lastWrite) -gt $timespan) {
                    # Process Old File
                    $inputPath = "F:\phone_tree\incoming"
                    $tmpPath = "F:\phone_tree\temp"
                    Write-Host "$(Get-Date) - Missed file caught: $path"
                    Move-Item $path $tmpPath\$file_name_ext
                    sleep 1
                    $(Get-Item $tmpPath\$file_name_ext).lastwritetime=$(Get-Date)
                    sleep 1
                    Move-Item $tmpPath\$file_name_ext $inputPath\$file_name_ext
                    sleep 1
                    Rename-Item $inputPath\$file_name_ext "pending-$(Get-Random).csv"
                }
            }
        }until($infinity)
        
        # Check Loop
        while ($true) {sleep 3}

    ### END
    Wednesday, March 6, 2019 6:17 PM

Answers

  • This works:

    Get-ChildItem <path> |Rename-Item -NewName {$_.Name -replace '\[|\]'}


    \_(ツ)_/

    • Proposed as answer by johnbljr Wednesday, March 6, 2019 9:30 PM
    • Marked as answer by Richard MuellerMVP Wednesday, March 13, 2019 11:54 AM
    Wednesday, March 6, 2019 8:57 PM

All replies

  • You can do this to remove the squares:

    get-Childitem c:\temp| Rename-Item -newname {$_.Name -replace '\[\]'}


    \_(ツ)_/

    Wednesday, March 6, 2019 7:20 PM
  • Where would I add that line?
    Wednesday, March 6, 2019 7:58 PM
  • Forget it.  It doesn't work. I just tested.  You will have to manually change the files.

    \_(ツ)_/

    Wednesday, March 6, 2019 8:52 PM
  • This works:

    Get-ChildItem <path> |Rename-Item -NewName {$_.Name -replace '\[|\]'}


    \_(ツ)_/

    • Proposed as answer by johnbljr Wednesday, March 6, 2019 9:30 PM
    • Marked as answer by Richard MuellerMVP Wednesday, March 13, 2019 11:54 AM
    Wednesday, March 6, 2019 8:57 PM