Home > Mobile >  Powershell how to move files with specific file name length
Powershell how to move files with specific file name length

Time:12-29

I am trying to move files with file name length is 14 characters to a folder.

For example,file name CWCA1175034366.pdf will be move to specific folder and file name 12345.pdf will not be moved.

Here is what I tried but failed.

foreach ($file in (Get-ChildItem \\i28699\TW.ASML_GBW\EV\AWB\*.pdf -Recurse)) {
    if ($file.Name.length eq 14) {
        Move-Item $file \\i28699\TW.ASML_GBW\EV\AWB\Renamed
    }
}

Can someone help me with that?Thanks.

CodePudding user response:

Code LGTM but you're missing the '-' from '-eq' to tell it to use that comparison operator.

CodePudding user response:

From your example I think you mean the length of the file's BaseName, so 14 characters without counting the extension.

Try

Get-ChildItem -Path '\\i28699\TW.ASML_GBW\EV\AWB\*.pdf' -File -Recurse |
Where-Object { $_.BaseName.Length -eq 14 } |
Move-Item -Destination '\\i28699\TW.ASML_GBW\EV\AWB\Renamed'
  • Related