3

Windows 10 system - Powershell tool.

I have 5 folders on my PC each containing 2 subfolders named images and json, each images subfolders has 3 png files numbered 1.png, 2.png and 3.png. The json subfolders have the same structure with 3 files each 1.json, 2.json and 3.json.

I run the command to rename these files in a single sequence that works ok, that is, in all png subfolders, the png's are in sequence and the json too.

eg.
       Folder 1\Images\ 1.png, 2.png, 3.png
       Folder 2\Images\ 4.png, 5.png, 6.png

also with json

eg.
       Folder 1\json\ 1.json, 2.json, 3.json
       Folder 2\json\ 4.json, 5.json, 6.json

See below, the result of this command.

'D:\Files_png_json\' | ForEach-Object{$($_ + "\*.png"), $($_ + "\*.json")} | Get-ChildItem -Recurse | foreach { If ( $_.extension -ne $prevExt ) { $i=1 } Rename-Item $_.FullName -NewName ('{0}' -f $i++ +$_.extension); $prevExt = $_.extension; }

Output command without Copy-Item:

PS C:\WINDOWS\system32> 
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 1\Images\1.png Destination: D:\Files_png_json\Folder 1\Images\1.png".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 1\Images\2.png Destination: D:\Files_png_json\Folder 1\Images\2.png".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 1\Images\3.png Destination: D:\Files_png_json\Folder 1\Images\3.png".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 2\Images\1.png Destination: D:\Files_png_json\Folder 2\Images\4.png".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 2\Images\2.png Destination: D:\Files_png_json\Folder 2\Images\5.png".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 2\Images\3.png Destination: D:\Files_png_json\Folder 2\Images\6.png".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 1\Json\1.json Destination: D:\Files_png_json\Folder 1\Json\1.json".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 1\Json\2.json Destination: D:\Files_png_json\Folder 1\Json\2.json".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 1\Json\3.json Destination: D:\Files_png_json\Folder 1\Json\3.json".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 2\Json\1.json Destination: D:\Files_png_json\Folder 2\Json\4.json".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 2\Json\2.json Destination: D:\Files_png_json\Folder 2\Json\5.json".
What if: Performing the operation "Rename File" in destiny "Item: D:\Files_png_json\Folder 2\Json\3.json Destination: D:\Files_png_json\Folder 2\Json\6.json".

What I want is for all png files and all renamed json files to be together in their own folder.

eg.
       \PNG\ 1.png, 2.png, 3.png, 4.png, 5.png, 6.png
       \JSON\ 1.json, 2.json, 3.json, 4.json, 5.json, 6.json

Using the same command, I inserted an if at the end of the line to check the file extension and, depending on the extension, copy the files being renamed to their respective PNG or JSON folders:

$pathPNG = "D:\JSON"
$pathJSON = "D:\PNG"
$prevExt = ""
'D:\Files_png_json\' | ForEach-Object{$($_ + "\*.png"), $($_ + "\*.json")} | Get-ChildItem -Recurse | foreach { If ( $_.extension -ne $prevExt -or $prevExt -eq $null ) { $i=1 } Rename-Item $_.FullName -NewName ('{0}' -f $i++ +$_.extension); If ( $_.extension -eq ".png" ) {Copy-Item $_.FullName -Destination $pathJSON -Force }else {Copy-Item $_.FullName -Destination $pathPNG -Force}; $prevExt = $_.extension; }

But now, when copying the files to the other folder and the renamed file is read, it shows the message that the file cannot be copied because it doesn't exist?!... but if the folder contains a file, regardless of the name , why it shows message that it cannot be copied because it does not exist?

Output Changed command:

Copy-Item : Copy-Item : Unable to find the path 'D:\Files_png_json\Folder 2\Images\1.png' because it doesn't exist.
No line:1 character:251
+ ... sion -eq ".png" ) {Copy-Item $_.FullName -Destination $pathJSON -Force }else { ...
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (D:\Files_png_json\Folder 2\Images\1.png:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 'D:\Files_png_json\Pasta 2\Images\1.png' because it doesn't exist.
No line:1 character:251
+ ... sion -eq ".png" ) {Copy-Item $_.FullName -Destination $pathJSON -Force }else { ...
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (D:\Files_png_json\Folder 2\Images\1.png:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
+ CategoryInfo          : ObjectNotFound: (D:\Files_png_json\Folder 2\Images\1.png:String) [Copy-Item], ItemNotFoundException

this file that it says not found, is the file that was renamed in the command block before the Copy-Item, its value after being renamed is 4.png and not 1.png.

It looks like the Copy-Item is still looking for the filename before it was renamed and not the new name!

Note: this only happens with renamed files, files that were not renamed, it copies normally!

A detail that I don't know if it can help:

in command If ( $_.extension -eq ".png" ) the true value is moving the JSON files and the false value is moving the PNG files.

for this reason I put the JSON folder in the true value and the PNG folder in the false value.

But wouldn't the logic be the true value moving the PNG files and the false value moving the JSON files?

Does not work:
The above command was renaming and accessing only files at the same time in the same folder.

Command that works:
Rename and copy all files together to another folder:

$fileRenamed = "C:\MyRenamedPngJsonFolder\"
foreach ($e in $("png","json")) {ls 'C:\MyOriginalPngJsonFolder' -r -filt *.$e | % {$i=1} { copy $_.FullName $("$fileRenamed"+$i+"."+$e) -Recurse; $i++}}

The files will be renamed in the output folder as follows:

1.json 1.png, 2.json 2.png, 3.json 3.png, 4.json 4.png, 5.json 5.png...

Note: You can create the renamed files folder anywhere on your computer. The files in the source folder will not be renamed.

@jfrmilner thanks for the help on this command.

8
  • Try this for example: Copy-Item "$($_.DirectoryName)\$i$($_.extension)" -Destination $pathPNG -Force with that command and see if it does it. You need it to match the file name you are renaming it to, so incorporating that in with the copy-item should do the trick. Tag me back once you self-answer if this helps. Note, I did not test this, but I believe this is what you need or some variation of this. The magic part I guess will be using the "$($_.DirectoryName)\$i$($_.extension)" to expand property values and concatenate into the full file path of files to copy post rename operations. Mar 19, 2022 at 22:28
  • Also, I believe it will be If ( $_.extension -eq ".png" ) { Copy-Item "$($_.DirectoryName)\$i$($_.extension)" -Destination $pathPNG -Force } so if the extension is .png then move those to the png folder. The matching true extension should move to that same matching folder correlated to the extension. Mar 20, 2022 at 1:52
  • @Vomit IT - Chunky Mess Style it went on to read the file with the right name, 4.pg, but it keeps showing the error: Copy-Item : Unable to find the path 'D:\Files_png_json\Folder 1\Images\4 .png' because it doesn't exist.... detail it only copies to the folder the files that were not renamed, in this case, the files 1.png, 2.png and 3.png. Only now it is only copying the 2.png and 3.png files, the 1.png was not copied. note: before starting the Copy command, is the ; correct or would it have to be a pipeline?
    – Clamarc
    Mar 20, 2022 at 5:18
  • @Vomit IT - Chunky Mess Style the strange thing is that the 4.png file is in folder 2 and not in folder 1 as the message says!... only from the 2nd line of the error message, that is, in the message referring to the 5.png file, the folder 2 appears, which is the correct one where the files that were renamed are, see: Copy-Item : Unable to find the path 'D:\Files_png_json\Folder 2\Images\5.png' because it doesn't exist.
    – Clamarc
    Mar 20, 2022 at 5:33
  • 1
    @Vomit IT - Chunky Mess Style OK, I'm already doing this, better make it work separately and then I'll try to do it all at once, thanks.
    – Clamarc
    Mar 20, 2022 at 15:48

1 Answer 1

2

This is a simplified approach, breaking the code out into multiple lines for readability

$pathPNG = "D:\PNG"
$filesPng = Get-ChildItem -Path 'D:\Files_png_json\' -Recurse -File -Filter *.png
$filesPng | ForEach-Object -Begin {$i=1} -Process { Copy-Item -Path $_.FullName -Destination $($pathPNG + $i + '.png') -Verbose ; $i++ }

$pathJSON = "D:\JSON"
$filesJson = Get-ChildItem -Path 'D:\Files_png_json\' -Recurse -File -Filter *.json
$filesJson | ForEach-Object -Begin {$i=1} -Process { Copy-Item -Path $_.FullName -Destination $($pathJSON + $i + '.json') -Verbose ; $i++ }

One-Liner of the above at OPs request

foreach ($e in $("png","json")) {ls 'D:\Files_png_json' -r -filt *.$e | % {$i=1} { copy $_.FullName $("D:\"+$e+"\"+$i+"."+$e);$i++}}
7
  • is outputting ok, but the output would have to be inside a subfolder in D: and the names in this subfolder would have to be 1.png, 2.png,... eg: D:\Files_png_json\PNG\1 .png 2.png 3.png... Would you like to do this command in a single line, as I'm trying to do, that is, copy the json files to the JSON folder and the png files to the PNG folder
    – Clamarc
    Mar 20, 2022 at 7:03
  • @Clamarc You can change the destination paths ($pathPNG, $pathJSON) if needed. Why does it need to be a single line?
    – jfrmilner
    Mar 20, 2022 at 10:30
  • I'm a beginner in powershell, I had already tried to change the $paths, but the path name always comes with the name of the copied files, as the $path would have to be for the names to be copied only with 1.png 2.png,. .. the single line would be to run only once, that's all.
    – Clamarc
    Mar 20, 2022 at 11:58
  • Your response in regards to paths is nonsensical. Could you try again to explain why the above does not meet your needs?
    – jfrmilner
    Mar 20, 2022 at 12:26
  • 1
    @Clamarc I've added a one-liner as you've requested. I hope you find it interesting. Please see superuser.com/help/someone-answers and feel free to open new questions with any additional queries.
    – jfrmilner
    Mar 20, 2022 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .