1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with using Rename/Ren in win10 cmd prompt/Powershell

Discussion in 'Operating Systems' started by Phil Tuncap, Sep 12, 2019.

  1. Phil Tuncap

    Phil Tuncap Member

    Messages:
    21
    Likes Received:
    0
    GPU:
    GTX 970 4096mb
    I'm trying to rename several files at once but nothing is coming out right. Under Powershell I'm not able to do anything at all, and under the cmd prompt i'm getting the wrong desired output. My files are in this format:

    Band Name - Album Name - Track Number - Track Name.wav

    and I'm trying to rename all of them to be:

    Band Name - Album Name - Track Number - Track Name-old1.wav

    Here's a screenshot of what I've tried:

    [​IMG]
     
  2. Caesar

    Caesar Master Guru

    Messages:
    675
    Likes Received:
    236
    GPU:
    GTX 1070Ti Titanium
    First, go to a folder with files to be renamed. Click on tab File, Open Windows PowerShell, and Open Windows PowerShell as administrator.

    [​IMG]

    Let's do the same rename operation like we did using File Explorer. Copy the script and paste it in PowerShell window with right mouse click:

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0}{1}' -f $i++, $_.extension)}

    Press Enter to execute.

    [​IMG]

    As you can see there are no parentheses around counter anymore and space symbol disappeared.

    [​IMG]

    To use the script you need to change marked part to set your file name.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('your_file_name_here_{0}{1}' -f $i++, $_.extension)}

    You can set where counter will appear. Just move marked part within apostrophes. They determine the begin and the end of the name.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0}{1}' -f $i++, $_.extension)}

    For example, you can place counter at the beginning of the name like this:

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('{0}_tumblr_photo{1}' -f $i++, $_.extension)}

    The result will look like this.

    [​IMG]

    Note that you already got rid of few limitations of File Explorer rename function. Now you can set a template by yourself and place the counter in any place of the name.

    PowerShell. How does rename script work?
    If you don't want to spend your time on understanding what all those special words and symbols mean just skip this part. If you are familiar with programming it will be much easier to understand.

    Let's disassemble the script and see what each part does.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0}{1}' -f $i++, $_.extension)}

    $i=1 — declare a new variable with the name i and initiate it with value 1. It will be used to generate a counter number at the end of each name. You can set this variable to any number and the whole sequence will begin with it.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0}{1}' -f $i++, $_.extension)}

    ; — used to separate commands in PowerShell. This is similar to a programming language because it actually is.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0}{1}' -f $i++, $_.extension)}

    • Get-ChildItem *.* | — call Get-ChildItem command which returns list of files in current folder;
    • *.* — allow to enable filter by name and extension. That will be used a bit later;
    • | — mean that each result (each file name) will be send to the next command. Which is Rename-Item.
    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0}{1}' -f $i++, $_.extension)}

    • %{Rename-Item $_ -NewName — apply a new name to each file according to the template;
    • $_ — a variable that was sent to rename command (in our case each file name);
    • 'tumblr_photo_{0}{1}' — a template for new file name;
    • tumblr_photo_ — text that will be placed in each file name;
    • {0} — that part will be replaced with the first variable which is $i (counter);
    • {1} — that part will be replaced with the second variable which is $_.extension (file extension);
    • -f $i++, $_.extension — list of variables (first and second one) that will be used in the template;
    • $i++ — return a unique number for the counter. The first variable which will be used in the template as a counter. Please note a post-increment operator ++ that will increase the variable value by one after sending it to the template. That small part allows to have a unique number in each name;
    • $_.extension — return the file extension. The second variable which will be used in the template as the file extension.
    PowerShell. Add leading zeroes to the counter
    If you want to add leading zeroes to the counter, use this script. Note marked part. D means digital and 3 means the number of zeroes. Change this number to get more leading zeroes if needed.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0:D3}{1}' -f $i++, $_.extension)}

    The result will look like this.

    [​IMG]

    PowerShell. Filter input files
    You can filter files that will be processed by extension. Edit marked part to change the extension.

    $i=1; Get-ChildItem *.jpg | %{Rename-Item $_ -NewName ('tumblr_photo_{0:D2}{1}' -f $i++, $_.extension)}

    I have added few png files to the test folder. As you can see only jpg files were renamed. Png files are untouched.

    [​IMG]

    Now I have added few files that include test word in the name. You can filter files by keyword using this script. Edit marked part with your keyword.

    $i=1; Get-ChildItem *.* -Filter "*test*" | %{Rename-Item $_ -NewName ('tumblr_test_photo_{0:D2}{1}' -f $i++, $_.extension)}

    The folder content before script execution.

    [​IMG]

    The folder content after script execution. Only files with test keyword were processed.

    [​IMG]

    You can combine both scripts to filter by keyword and extension at the same time.

    PowerShell. Change only a file extension
    You can change file name and extension using next script. Set new name and extension by changing marked part.

    $i=1; Get-ChildItem *.* | %{Rename-Item $_ -NewName ('tumblr_photo_{0:D2}.png' -f $i++)}

    If you need to change only an extension use this script. Set your extension by changing the marked part.

    Get-ChildItem *.* | Rename-Item -NewName { [System.IO.Path]::ChangeExtension($_.Name, ".jpg") }

    If you want to apply a filter by name or extension, combine those scripts with previous ones. As you can see PowerShell is a pretty flexible tool.

    PowerShell. Convert all letters to lowercase
    Let's see one more common example. You can convert all letters to lower case using this script.

    Get-ChildItem *.* | Rename-Item -NewName { $_.Name.ToLower() }

    There are a lot more things you can do. Despite PowerShell eliminates all limitations of the File Explorer, it has few weighty disadvantages.

    • You can't recover renamed files. The procedure is irreversible. But that is possible in File Explorer;
    • it is pretty complicated for beginners. You can make a mistake easily;
    • it takes much more time to open PowerShell and form correct script.
    PowerShell is often used by system administrators, programmers, and other professionals. Don't take on too much, you may try something simpler.

    PowerShell in action
    The demonstration below shows how to:

    • Rename files with a png extension and add leading zeroes to the counter;
    • rename files with a jpg extension and add leading zeroes to the counter;
    • rename all files and add leading zeroes to the counter;
    • rename all files and change the extension.
    [​IMG]
    <-------------------------->
     
    Last edited: Sep 12, 2019
  3. Phil Tuncap

    Phil Tuncap Member

    Messages:
    21
    Likes Received:
    0
    GPU:
    GTX 970 4096mb
    Ok the counters might help with some other stuff I have to organize, but with my audio .wav files I'm wanting to keep the setup as-is with spaces and no counters, and just add '-old1' to the end of each file like so:

    [​IMG]

    Reason being the *-old1.wav files are the original version, and the others are after I cleaned them up in Audacity to be prepped for audio editing in FL Studio.
     

Share This Page