0

I will be consistently copying files from an external server to a local server. The file names are of the following format:

Example 1     Invoice MM500780 10 26 2012 3 52 15 PM.PDF
Example 2     Invoice PS100679-02 10 30 2012 9 31 35 AM.PDF
Example 3     Return RTN001116 10 26 2012 8 59 56 AM.PDF

I would like to rename all the files by trimming any characters after the second space (which represent the date and time). Files names should be:

Example 1     Invoice MM500780.PDF
Example 2     Invoice PS100679-02.PDF
Example 3     Return RTN001116.PDF

Is there anyway I could create a powershell file and run it via task manager?

Thanks for any assistance.

Invnet
  • 27
  • 1
  • 1
  • 6
  • Why do you want to use PowerShell for this task? Do you need to rename only one bunch of that files or you plan to rename multiple bunches in future? – Mikhail Nov 02 '12 at 20:47
  • @Christian I tried using a bat file == '@echo off for /f "tokens=*" %%a in ('dir /b /a-d *.pdf') do ( for /f "tokens=1" %%b in ("%%a") do ren "%%a" "%%b.pdf" ) )' However, this only renames the files as the first word – Invnet Nov 02 '12 at 20:56
  • @Mikhail, Yes I will be renaming files daily. – Invnet Nov 02 '12 at 20:57

4 Answers4

2

try:

dir c:\mydir -filter *.pdf | 
% {rename-item -path $_.fullname -newname ( ($_.name.split('')[0..1] -join ' ' ) + $_.Extension )}
CB.
  • 58,865
  • 9
  • 159
  • 159
1

Following from Christian's post, this is his code adapted to use a Regex pattern match.

[regex]$regex = "\A\S*[\s]\S*"

dir c:\mydir -filter *.pdf | % {rename-item -path $_.fullname -newname ($regex.matches($_.BaseName)[0].value + $_.Extension )}

Change the regex to change the naming pattern. With the right pattern you should be able to pull out any part of the original name to use as the new name. The pattern is \A start of the string, \S non-whitepace characters (* any number), \s whitespace, \S non-whitepace characters (* any number)

Stuart M
  • 11
  • 1
0

I believe what you're looking for is something along these lines?

Split string with PowerShell and do something with each token

Basically you'd be splitting the file names up at the spaces, and then renaming the file to whatever is in the first and second tokens.

Community
  • 1
  • 1
isaacparrot
  • 1,918
  • 17
  • 24
  • Thanks for your reply ParrotMac, I tried : @echo off for /f "tokens=*" %%a in ('dir /b /a-d *.pdf') do ( for /f "tokens=1" %%b in ("%%a") do ren "%%a" "%%b.pdf" ) ) But got invoice.pdf return.pdf I am not sure how to combine and output both tokens – Invnet Nov 02 '12 at 20:41
  • @ ParrotMac Thanks for your reply I tried : '@echo off for /f "tokens=*" %%a in ('dir /b /a-d *.pdf') do ( for /f "tokens=1" %%b in ("%%a") do ren "%%a" "%%b.pdf" ) )' But got **invoice.pdf** and **return.pdf** as the filenames I am not sure how to combine and output both tokens – Invnet Nov 02 '12 at 20:48
0

This powershell one liner should do what you need.

Get-ChildItem | %{$splitName = $_.Name.Split(" ", 3); if($splitName.Length -gt 2) {$finalName = [string]::Join("",[string]::Join(" ",$splitName[0],$splitName[1]),$_.Extension); Rename-Item -Path $_.FullName -NewName $finalName}}
Matthew Brubaker
  • 3,097
  • 1
  • 21
  • 18
  • ...Thanks for your reply...I am a novice at this ...I tried your code and got error Rename-Item : Cannot bind argument to parameter 'NewName' because it is null. – Invnet Nov 02 '12 at 21:20