When I add a seconond file type in script - it wont work

Dear people,

I try to change my script here, that it also includes *.doc and .doxc. I have tried to add those document types with ,".docx" after the pdf input. I also tried to copy the row and replace pdf with docx.
But the script is not working - where is my error?

$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$folderPath = $scriptPath
$oldWord = Read-Host “Enter the word to be replaced”
$newWord = Read-Host “Enter the new word”
$documents = Get-ChildItem -Path $folderPath -Filter “*.pdf” -File
foreach ($document in $documents) {

$currentName = $document. Name
$newName = $currentName -replace $oldWord, $newWord    
Rename-Item -Path $document.FullName -NewName $newName

}

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.

How to format code on PowerShell.org

The -Filter parameter does not accept an array of values so you can specify only one file type.

Try using -Include instead, this has to be used with the -Recurse parameter, but if you only want to get items in the parent folder, you can limit the depth:

$documents = Get-ChildItem -Path $folderPath -Include '*.pdf','*.doc','*.docx' -File -Recurse -Depth 0
1 Like

Really? :thinking: I always thought you need to provide a path with an asterisk at the end to make -Include work as expected.

I think this is one of those things I’ve always done because it ‘just works’, but you’re right, you can drop the -Recurse if you only want the current folder, by specifying the asterisk as the path.

$doc = Get-ChildItem -Path * -include '*.pdf','*.doc','*.docx' -File

I think they actually messed up the behaviour of Get-ChildItem pretty much and now they are afraid to make a breaking change to actually fix it. :man_shrugging:t3:

Thank you all for your help.

I have adapted the script as seen below:

$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$folderPath = $scriptPath
$oldWord = Read-Host "Enter the word to be replaced"
$newWord = Read-Host "Enter the new word"
$documents = Get-ChildItem -Path $folderPath -Include '*.pdf','*.doc','*.docx' -File -Recurse -Depth 0
foreach ($document in $documents) {
     $currentName = $document. Name
    $newName = $currentName -replace $oldWord, $newWord
    Rename-Item -Path $document.FullName -NewName $newName
}

when i run the script via right click, its not starting. I can copy it directly in the Powershell - then it does work but i have to press enter after the script runs to see the change.

I know its all beginners questions but I just start with powershell

the -recurse seems to effect also all files in the subfolders. Can that be removed?

I can’t replicate that, it works for me. You need to provide more detail.

Yes, as noted in the conversations above, as long as the path ends with an asterisk, you can use the -Include parameter.

$folderPath = "$scriptPath*"

If you’re having trouble, I would suggest starting with a known path rather than using

$MyInvocation.MyCommand.Definition

Depending how and where you run your script, you might not be targetting the files you think you’re targetting.

2 Likes

@matt-bloomfield thank you for your explaination.

I want to use the script under that circumstance;

I am not allowed to use renaming software for my company laptop.
so my idea is to create and folder on my desktop where I can use that script to rename documents. I want to use different scripts like for adding pre-fix, replace parts, remove substrings and so on. But somehow i cant get the script run in any folder where i copy it without affecting the subfolders.

My first version used the -filter but then it was only possible to do it for one document kind and not a second kind like pdf and doc.

on win 10 I can start the script now with right click. on Win 11 its nots working, maybe different setup there?

  • so the solution I still need is that the Depth 0 is not working for this script. it affects the files in the subfolder as well and is not limited to the script directory only
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$folderPath = $scriptPath
$oldWord = Read-Host "Enter the word to be replaced"
$newWord = Read-Host "Enter the new word"
$documents = Get-ChildItem -Path $folderPath -Include '*.pdf','*.doc','*.docx' -File -Recurse -Depth 0
foreach ($document in $documents) {
    # Get the current file name
    $currentName = $document.Name

    # Replace the word in the file name
    $newName = $currentName -replace $oldWord, $newWord

    # Rename the document file
    Rename-Item -Path $document.FullName -NewName $newName
}

:man_shrugging:t3:

Add an asterisk to the path and drop the -Recurse parameter.

I’d recommend to talk to your IT department. Ask them why you’re not allowed and tell them that you need something like a renaming tool to do your job. They may even have a suggestion you haven’t thought about.

@Olaf thank you very much.

I have adapted the script as copied below. It seems to work now! It just affects the file in that directory where the script is copied to an runned in.

$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$folderPath = "$scriptPath*"

$oldWord = Read-Host "Enter the word to be replaced"
$newWord = Read-Host "Enter the new word"
$documents = Get-ChildItem -Path $folderPath -Include '*.pdf','*.doc','*.docx' -File
foreach ($document in $documents) {
    # Get the current file name
    $currentName = $document.Name

    # Replace the word in the file name
    $newName = $currentName -replace $oldWord, $newWord

    # Rename the document file
    Rename-Item -Path $document.FullName -NewName $newName
}

Do you see something which could be done better? I also do believe that its safe to use - so its not affecting other folders or subfolders

Do you really want to copy the script around? That’s a weird approach in my opinion.

How about using a mandatory parameter where you have to provide a folder path to actually run the script? Or use a folder picker. :man_shrugging:t3:

this script might be used by people who dont know much about Power shell.

So one way to solve it would be: let them create a static folder, like “c:\rename” and let them move their files there every time they want to rename it. Or to copy the script to the folder where they want to use it. To get a folder picker might be too time consuming. when they could copy the script to their working folders, it might work as well as creating a static folder somewhere else.

Currently i try to convert it as an exe file. So it can be moved and used as needed.

Or what would be your approach?`

BTW an other question:
I try the same script but to delete the first xxx amount of characters from a file name:

$charactersToDelete = Read-Host “Enter the number of characters to delete:”
dir | rename-item -newname { string.substring($charactersToDelete) }

where could i connect the -include “.pdf", ".docx” here?

It takes a few minutes. :man_shrugging:t3:

https://www.google.com/search?q=powershell+folder+picker

If you have another question start a new thread please. :point_up:t3:

I will close here now. :wink: