K
K
Konstantin Korotaev2021-01-11 13:14:55
PowerShell
Konstantin Korotaev, 2021-01-11 13:14:55

PowerShell does not execute the -newname command if there are spaces in the path, what should I do?

In case of command execution

dir -Recurse "D:\VideoArchive\Instagram\*qwer*" | ren -newname {$_.name.replace("qwer", "")}

From all file names in all subfolders of the specified path, qwer is removed

If the file path is specified with spaces in the command
dir -Recurse "D:\Instagram\курс по продвижению\*qwer*" | ren -newname {$_.name.replace("qwer", "")}


Nothing happens, I tried this:
dir -Recurse "D:\Instagram\курс` по` продвижению\*qwer*" | ren -newname {$_.name.replace("qwer", "")}


So:
dir -Recurse "& 'D:\Instagram\курс по продвижению\*qwer*'" | ren -newname {$_.name.replace("qwer", "")}

So:
dir -Recurse "& 'D:\Instagram\курс по продвижению\*qwer*'" | Invoke-Expression | ren -newname {$_.name.replace("qwer", "")}


And some more options, in many cases, or an error saying that all the same the path after the space is divided, or nothing happens ...

Please help me write the correct script. Thank you!

PS How did the workaround helped in the right directory right click + Shift , select Open PowerShell window here and execute
dir -Recurse "*qwer*" | ren -newname {$_.name.replace("qwer", "")}
but I want more automation and fewer clicks :-)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-01-11
@vabka

Try

$items = Get-childItem -Path "D:\Instagram\курс по продвижению\*qwer*" -Recurse -Force 
foreach($item in $items) {
    Rename-Item -Path $item -NewName $item.Name.Replace("qwer", "")
}

Or
Get-childItem -Path "D:\Instagram\курс по продвижению\*qwer*" -Recurse -Force | % Rename-Item -NewName {$_.Name.Replace("qwer"m "")}

Rename-Item only renames one file/folder at a time, and you give it a list.

M
MaxKozlov, 2021-01-11
@MaxKozlov

You are not saying something :)
because it normally renames everything even without wrappers around Rename-Item ,
rename can get its -Path through the pipeline, and -newname you have just tested the
script.
Even inside a folder renamed in the process, renames the contents
Add to the original ren -whaitif and see what it produces

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question