Windows 7 command to add a leading zero to a batch of filenames

3

Having trouble getting the correct command syntax. We need to add a leading zero to a batch of files in a directory on a regular basis. I cannot download and install a software item to do so. The operating system is Windows-7. The length of the filenames varies. i.e. 000165-CityName1.pdf to 0000165-CityName1.pdf 000166-CityNameLonger2.pdf to 0000166-CityNameLonger2.pdf etc. Looking for a rename command that would work, can someone please suggest one, it would be appreciated. I have tried a half dozen without success.

Techexpressinc

Posted 2016-06-24T13:38:38.137

Reputation: 31

Windows 7 does not rely on MS-DOS at all. MS-DOS doesn't support file names longer than 8+3 characters. Do you mean the Windows 7 command prompt? – a CVn – 2016-06-24T13:40:58.293

1Yes, I mean the Windows 7 command prompt. – Techexpressinc – 2016-06-24T13:54:54.747

Answers

2

This powershell script should work just fine to add a 0 to the beginning of each filename in a folder. Save this script as a .ps1.

Get-ChildItem -Path "C:\temp\cityfiles\" | 
  Rename-Item -NewName {$_.BaseName.insert(0,'0') + $_.Extension}

This takes every file inside the folder cityfiles and adds a '0' to the beginning of each filename.

Narzard

Posted 2016-06-24T13:38:38.137

Reputation: 2 276

This worked! I was un-aware of the "Powershell" normal command prompt failed using the code. Thank you. – Techexpressinc – 2016-06-24T15:04:06.763

1@Techexpressinc Would you mind marking it as the answer for future users to search on? Glad it worked for you! – Narzard – 2016-06-24T15:18:15.733

1

While you could put this in a bat file and put some options and checking, going to a cmd prompt and cd to the folder you want

  • ren .pdf 0.pdf

Will add a zero in front of the name for any pdf

bvaughn

Posted 2016-06-24T13:38:38.137

Reputation: 733

This did not work, I changed saved it as a bat file and click on it and the files with 4 leading zeroes stayed the same. – Techexpressinc – 2016-06-24T15:03:01.293

there is * or shift 8 that is not displaying, not sure why - It is ren .pdf 0.pdf – bvaughn – 2016-06-24T18:08:05.180

You have to preface code with 4 spaces. Or surround it with backticks. Stars have a special meaning in markdown. – Kat – 2016-06-30T19:38:19.237

1

How do I add a leading zero to a batch of filenames?

Use the following command:

for /f %f in ('dir /b *.pdf') do ren "%f" "0%f"

To use in a batch file replace % with %%:

for /f %%f in ('dir /b *.pdf') do ren "%%f" "0%%f"

Example usage:

F:\test\test>dir
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\test

24/06/2016  21:39    <DIR>          .
24/06/2016  21:39    <DIR>          ..
24/06/2016  21:38                 0 000165-CityName1.pdf
24/06/2016  21:38                 0 000166-CityNameLonger2.pdf
               2 File(s)              0 bytes
               2 Dir(s)  1,769,011,425,280 bytes free

F:\test\test>for /f %f in ('dir /b *.pdf') do ren "%f" "0%f"

F:\test\test>ren "000165-CityName1.pdf" "0000165-CityName1.pdf"

F:\test\test>ren "000166-CityNameLonger2.pdf" "0000166-CityNameLonger2.pdf"

F:\test\test>dir
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\test

24/06/2016  21:40    <DIR>          .
24/06/2016  21:40    <DIR>          ..
24/06/2016  21:38                 0 0000165-CityName1.pdf
24/06/2016  21:38                 0 0000166-CityNameLonger2.pdf
               2 File(s)              0 bytes
               2 Dir(s)  1,769,011,425,280 bytes free

Further Reading

DavidPostill

Posted 2016-06-24T13:38:38.137

Reputation: 118 938