0

I have a directory structure as below:

Folder
    > FileName1.abc
    > Filename2.abc
    > .............

Folder2
    > FileName11.abc
    > Filename12.abc
    > ..............

> ..........

etc. I have a command that can prepend folder names to filenames.

Sample:

Folder1_Filename1.abc
Folder1_Filename2.abc

I want to change it like this.

Make the folder name the same as the file names:

Folder1.abc
Folder1 (1).abc
Folder2.xyz
Folder2 (1).xyz

i.e. As a result I want to replace the filenames with the folder name. If there are files with different names with the same extension, they can add (1), (2) to the end of the names.

Script:


@echo off
pushd "Folder"
for /d %%D in (*) do (
  pushd "%%D"
  for /r %%F in (*) do (
    for %%P in ("%%F\..") do (
      ren "%%F" "%%~nxP_%%~nxF"
    )
  )
  popd
)
popd

The above command doesn't do what I want.

This command just adds the folder name to the beginning of the filenames.


PS: I changed the command "%%~nxP_%%~nxF" to "%%~nxP%%~xF" command. And I have almost completed what I wanted. But at one point I saw that only 1 of the files with the same extension changed their name. I've been looking for a command that can add (1), (2), (3) when saving the names of files with the same extension; but I couldn't find it.

Detailed example:


Folder
    > Folder.jpg
    > Folder (1).jpg
    > Folder (2).jpg
    > .............

I would be grateful if you know of a command that can do what I want other than the above command.

You deserve appreciation.

Marco
  • 1
  • 2
  • 1
    That is exactly how you programmed it: `"%%~nxP_%%~nxF"`. I think what you want is `"%%~nxP%%~xF"` – Squashman Oct 12 '21 at 19:27
  • Yes this does what I want. Another thing I want to add to this is this. Is it possible to add (1), (2) when renaming files with the same extension? Because this command changes the name of only 1 of the files with the same extension. – Marco Oct 12 '21 at 19:53
  • Look, what I have done [here](https://stackoverflow.com/a/69541345/2152082) with the `line` counter. You can do the same thing here. – Stephan Oct 12 '21 at 20:38
  • I checked the link; but frankly i didn't understand much. I couldn't figure out which code can do what I want and on which line I should add that code. – Marco Oct 12 '21 at 21:05
  • Yes the file counter could be difficult to implement with your current code logic. Because you are using a FOR /R to iterate the files and some trickery to get the parent folder it would be difficult to keep track of the number of files it has iterated in each folder. You have to keep track of what the previous folder is and when it is different then the current folder then the counter needs to be reset back to 1. – Squashman Oct 12 '21 at 21:24
  • I guess I'll have to find some other code that can do what I want. My coding knowledge is limited. So unfortunately I won't be able to do additional tests. Thank you. – Marco Oct 12 '21 at 22:56
  • You are making it even more difficult if you have multiple files with multiple extensions in each folder. If you have two files with .ABC and two with .XYZ in the same folder you then would need to keep track of the count by folder name and extension. Also if you have the same sub folder name is some other folder path you then would have to basically keep track of the whole path and file extensions. Not just the parent folder name. – Squashman Oct 13 '21 at 03:38
  • Let's say I have a folder named `X`. And there are **200 folders** in this folder and they all have different names. And within these `200 folders` **(no subfolders)**, there are only files with different names and extensions. When I add the fix you provided, the name of each folder replaces the names of the files in it. This is exactly what I want... However, some folders have files with the same extension. For example; There are `20 images` in the `.jpg` extension. At this point, the names of `19 of them do not change`. And that leads me back to the manual fix. – Marco Oct 13 '21 at 14:11
  • I want a solution with this code structure or with a different code, using `CMD` & `Powershell` or a `Macro`. If there are files with the same extension, change the name; and when renaming other subsequent files put (1) at the end of the name and increment it by 1 when renaming other files. Or put `-1`, `-2`, `-3` at the end of the file name and keep increasing the number. This is what I want; I can say I don't have any coding knowledge. That's why I keep researching. – Marco Oct 13 '21 at 14:11
  • do you want to scale the files to the parent folder or do you want to leave them in the folder they are in? – Zano Oct 13 '21 at 20:29
  • They will stay in the folder they were in. Only the names of the files will be the same as the name of the folder. – Marco Oct 13 '21 at 20:36

0 Answers0