Renaming files with unnecessary zeros in Powershell

时间:2016-02-12 20:45:57

标签: powershell batch-file rename batch-processing

I've been looking into batch renaming files with Powershell and I've made some good progress. To put it simply I'm looking to remove all extra zeros from the beginning of my files. So far I have a folder of images named as such:

Internet - WebDesign - Php
Internet - WebDesign - Html
House - Garden - Machines

All the way up to 900~. I created a Powershell script that takes the first four characters and adds the .jpg extension back. Here is that script:

0001_random_name.jpg
0002_random_name.jpg
0003_random_name.jpg

This renames the files to

Get-ChildItem 'G:\InvaluableNumbered' | rename-item -newname { $_.name.substring(0,4) + ".jpg" }

For this project I need to name them

0001.jpg
0002.jpg
0003.jpg

All the way up to 968.jpg. Is there any way I can use the script that I wrote and then have another command that removes all 0s up to where it hits the first number greater than zero?

Thank you for your time.

1 个答案:

答案 0 :(得分:1)

You can use TrimStart string method to achieve desired result:

class Obarray(np.ndarray):
    def __new__(cls,input_array, klass, dtype):
       obj = np.assarray(input_array).view(cls) 
       obj.klass = klass
       obj.dtype = dtype
       A = np.array(obj,dtype=np.object)
       N = np.empty(shape=A.shape, dtype=dt ype)
       for idx in np.ndindex(A.shape):
            for name, type in dtype:
                N[name][idx] = type(getattr(A[idx],name))
       obj.N = N.view(np.ndarray) 
       return obj

Other possibility could be cast value to $_.name.substring(0,4).TrimStart('0') and then back to [int]:

[string]