Home > Enterprise >  Simple renaming - remove (JOB_0000_) from file name
Simple renaming - remove (JOB_0000_) from file name

Time:02-19

I work with software that generates an output files with the names: JOB_1_sample-file_20220211083122.txt in a specific folder. There are thousands of files at the same time.

The beginning of the text file is variable (JOB_1,JOB_2,JOB_3,etc).

Example: JOB_2_sample-file_20220211083122.txt, JOB_34_sample-file_20220211083122.txt, JOB_1007_sample-file_20220211083122.txt.

I need to remove the initials from the files, leaving only the 'sample' forward. Preferably using Powershell, CMD or javascript.

Is there such a possibility?

CodePudding user response:

Use Rename-Item with a delay-bind script block, combined with a -replace operation that uses a regex to remove the prefix of interest:

$dir = '.' # specify target dir.
Get-ChildItem -LiteralPath $dir -Filter *.txt |
  Rename-Item -NewName { $_.Name -replace '^JOB_\d _' } -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

  • Related