Help Required on the Script #Bulk Folder Renaming

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Help Required on the Script #Bulk Folder Renaming

Post by MigrationUser »

13 Apr 2015 05:40
Shashidhar


Dear All,

Below Script is used to rename the Bulk Folder Renaming,

However i wanted to make this as a Generic Script so users can give input and use it accordingly.

1. This script has 3 inputs a) Enter the Source Folder path b) Extension of the Files and c) New File Name to be renamed

All are fine except the c) New File Name to be renamed

$Rename = input

$newName = '$Rename_{0:d6}{1}' -f $i , $extension # $rename input cannot be used here #
I think some thing wrong on the above line, is there any way to get this done ?
Kindly please help.

======================================================================

Code: Select all

Write-Host "Renaming Bulk Files" -BackgroundColor Green -ForegroundColor White
$Source = Read-Host "Enter the Source Path Of the Folder, Ex: D:\Test"
$SourceExtension = Read-Host "Enter the Destination File Extension, Ex: *.pdf or *.jpg"
$Rename = Read-Host "Enter the File Name to be renamed"

$i = 0 
Get-ChildItem -Path $Source -Filter $SourceExtension | 
ForEach-Object {
    $extension = $_.Extension
    $newName = '$Rename_{0:d6}{1}' -f  $i , $extension 
    $i++ 
    Rename-Item -Path $_.FullName -NewName $newName 
} 
======================================================================

----------------------------

#13 Apr 2015 19:09
Simon Sheppard


When you enclose a string in single quotation marks, any variable names in the string such as '$myVar' will appear exacly as typed when the command is processed. Expressions in single-quoted strings are not evaluated.

When you enclose a string in double quotation marks, any variable names in the string such as "$myVar" will be replaced with the variable's value when the command is processed.
(https://ss64.com/ps/syntax-esc.html)

so what you need is

Code: Select all

$newName = "$Rename`_{0:d6}{1}" -f  $i , $extension  
Last edited by Simon Sheppard (14 Apr 2015 10:01)

----------------------------

#14 Apr 2015 05:11
Shashidhar


Dear Simon Sheppard,

Using DoubleCode Worked but after removing the _ from the the below Line

$newName = "$Rename_{0:d6}{1}" -f $i , $extension # Not Working

$newName = "$Rename {0:d6}{1}" -f $i , $extension # Working

If i want to add _ or any other Character is there any way ?

Out Put is Ok for me now

i am getting Test 001
Test 002
Test 003
As the file names

if i get _ or any other character it would be better, If possible kindly help in getting the expected output.

Thanks in Advance
Shashidhar

----------------------------

#14 Apr 2015 10:01
Simon Sheppard


Just escape the underscore with a backtick
"$Rename`_{0:d6}{1}"

----------------------------

#22 May 2020 13:20
Kskinner


Hello, I am new to scripting and have not found anything else that fits what I want to do more than this topic anywhere.
What I want to do is customize all file names in a folder. They come to me named this way: FOUO-NPS2875_366TRS_20200522_2 and I'd like to have a PS script that will change them to: FOUO-NPS2875_361TRS_05-25-20 thru 02-29-20_1.
The 361 in 361TRS varies from 361TRS -366TRS, and if there is only one file in the group for a TRS designation, there would not be a corresponding _(Number) at the end. Is there a way to write a PS script to do this?
I typically have to change 20-30 files in a folder and it would be better to do it automatically.
Post Reply