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) {
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.
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:
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.
$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 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
}
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.
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
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?