1

I have a database full of .pdf and .dwf files. I need to rename these.

The files are named as follows:

123456 text text.pdf 

And should look like this:

123456000_text_text.text.pdf 

I can replace the spaces with the following command:

dir | rename-item -NewName {$_.name -replace " ","_"} 

Now I need a command to insert "0" three times after the first 6 digits.

Can someone help me?

Thanks already

3
  • What have you tried so far to add the zeros? Are the digit always 6 and you want a 9 digit number? What if it was 12345, should it be 12345000 or 123450000? Commented Feb 3, 2021 at 10:56
  • Its always 6 and i need 9 Commented Feb 3, 2021 at 10:59
  • I am just thinking or trying if I can split the name like a string Commented Feb 3, 2021 at 11:02

2 Answers 2

3

You need to filter on *.pdf and *.dwf files only and also if the filenames match the criterion of starting with 6 digits followed by a space character. Then you can use regex replacements like this:

Get-ChildItem -Path D:\Test -File | Where-Object { $_.Name -match '^\d{6} .*\.(dwf|pdf)$' } | Rename-Item -NewName { $_.Name -replace '^(\d{6}) ', '${1}000_' -replace '\s+', '_'} 

Before:

D:\TEST 123456 text text.dwf 123456 text text.pdf 123456 text text.txt 

After:

D:\TEST 123456 text text.txt 123456000_text_text.dwf 123456000_text_text.pdf 

Regex details of filename -match:

^ Assert position at the beginning of the string \d Match a single digit 0..9 {6} Exactly 6 times \ Match the character “ ” literally . Match any single character that is not a line break character * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) \. Match the character “.” literally ( Match the regular expression below and capture its match into backreference number 1 Match either the regular expression below (attempting the next alternative only if this one fails) dwf Match the characters “dwf” literally | Or match regular expression number 2 below (the entire group fails if this one fails to match) pdf Match the characters “pdf” literally ) $ Assert position at the end of the string (or before the line break at the end of the string, if any) 
Sign up to request clarification or add additional context in comments.

3 Comments

Add $_.FullName instead of just $_.Name.
@AbrahamZinala Nope, I'm piping the FileInfo object.
You just edit it!?! lol could've sworn i didn't see it, my mistake(:
3

What you have is 123456 text text.pdf Want it to look like 123456000_text_text.pdf A systematic way to achieve this would be>>

$const = "123456 text text.pdf" $filename = $const -replace " ","_" $temp = $filename.split("_")[0] $rep1 = ([string]$temp).PadRight(9,'0') $output = $filename -replace $temp,$rep1 Write-Host $output -ForegroundColor Green 

enter image description here

The great thing about this method is that it will always trail with 0s keeping your number string to 9 digits.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.