#1 13 Apr 2015 05:40

Shashidhar
New Member
Registered: 13 Apr 2015
Posts: 2

Help Required on the Script #Bulk Folder Renaming

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.

======================================================================
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 
} 
======================================================================

Offline

#2 13 Apr 2015 19:09

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 899
Website

Re: Help Required on the Script #Bulk Folder Renaming

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.
(http://ss64.com/ps/syntax-esc.html)

so what you need is

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

Last edited by Simon Sheppard (14 Apr 2015 10:01)

Offline

#3 14 Apr 2015 05:11

Shashidhar
New Member
Registered: 13 Apr 2015
Posts: 2

Re: Help Required on the Script #Bulk Folder Renaming

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

Offline

#4 14 Apr 2015 10:01

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 899
Website

Re: Help Required on the Script #Bulk Folder Renaming

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

Offline

Board footer

Powered by FluxBB