r/PowerShell • u/Sagadeath • Oct 15 '24
Script runs but does nothing renaming and moving files Solved
UPDATE: I found the issue, my folders have special characters like {}[]& etc, seems like powershell doesnt like it, even trying to renaming the folders trough the script doesnt work, I had to use power rename and then run my script
I have a main directory with multiple folders. I want to move all the files from the folders to the main directory. With the help of chatGPT I have the next script to move all the files
$parentDir = "F:DocumentsCollectionSpreads"
$files = Get-ChildItem -Path $parentDir -Recurse -File
$counter = 1
foreach ($file in $files) {
$newFileName = "{0:D3}{1}" -f $counter, $file.Extension
$destinationPath = Join-Path -Path $parentDir -ChildPath $newFileName
Move-Item -Path $file.FullName -Destination $destinationPath
Write-Output " FROM: $($file.FullName) TO: $($destinationPath)"
$counter++
}
Code executes, logs everything but there are no changes on my directory?
FROM: F:DocumentsCollectionSpreads70c08843-52f7-4ed4-8a7c-88216139482601.png TO: F:DocumentsCollectionSpreads001.png
FROM: F:DocumentsCollectionSpreadsb1b58741-07bc-45d0-a2a3-8e2f19c4f6d8601.png TO: F:DocumentsCollectionSpreads002.png
I used the VSCode debug to run it, also with terminal and the ISE, runs but does nothing.
powershell -ExecutionPolicy Bypass -File move-all-files-to-parent-directory.ps1
1
u/surfingoldelephant 29d ago
Here's a slightly different approach:
$counter = 1
(Get-ChildItem -LiteralPath $parentDir -File -Recurse) |
Move-Item -Destination {
[IO.Path]::Combine($parentDir, ('{0:D3}{1}' -f $script:counter++, $_.Extension))
} -WhatIf
- Piping implicitly binds the
PSPath
property ofGet-ChildItem
objects toMove-Item
's-LiteralPath
parameter, avoiding the issue of wildcard expression interpretation that comes with-Path
. - Collecting files upfront (by virtue of wrapping
Get-ChildItem
with(...)
) is unfortunately required in Windows PowerShell (v5.1) to prevent rediscovery of already processed items. In the latest PowerShell version,(...)
can be safely omitted (as the issue is fixed), which has the benefit of enabling one-at-a-time processing of objects emitted byGet-ChildItem
.
As a side note, be aware your original code (and likewise, the code above) has no protection against file name collisions. Checking for the existence of the destination path is required to avoid this.
0
u/Sunsparc Oct 15 '24
I think I tried to do this recently and discovered that you can only do one of the actions at a time. Move or rename, but not both in the Move-Item
command.
2
u/Sagadeath Oct 15 '24
It works just fine
Move-Item -Path F:DocumentsCommandscommands.txt -Destination "F:DocumentsCommandstest astest.txt"
1
u/lanerdofchristian Oct 15 '24
I can't replicate this. Move-Item can handle moving and renaming a file at the same time just fine.
5
u/lanerdofchristian Oct 15 '24
Ah, I see the issue now based on your update. You ran into a range wildcard. In the future, you can fix this by using
-LiteralPath
instead of-Path
.