Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

Question

Batch renaming file in a folder with the folder name

May 26, 2017 8:14PM PDT

Hi,

I don't know if this is possible but I need your help regarding this.

I am managing a shop and received about 2600 pictures of the product from the manufacturer.
The data I received had every single picture in each separate folder.
So basically 2600 single pictures each in 2600 different folders.
The folder name is in SKU (Stock Keeping Unit, aka part number)
and the picture file name is in the name of the product.

The problem is that I need all the pictures file name to be the SKU inorder to use it in my shop system.
So my question is this, is there a way to batch rename the picture file name to the folder name(SKU) with out copy/pasting the name manually 2600 times.

I also have asked the manufacturer to send me the pictures in SKU file name, but I wanted to know a way to do this in case they say no.

Any help is appreciated.

Discussion is locked

- Collapse -
Answer
batch files
May 26, 2017 9:21PM PDT
https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/

I think you will need someone to write you a batch file which can do that.

Wouldn't it be easier to use it "as is" since there's one picuture per SKU if I read this correctly? If that's the case, you could use the SKU folder name in the path and *.jpg or whatever image format is used to link to the images.

For instance, if the folder and image path was;

C:/SKU123456789/product_pic.jpg

then you could link to it by

C:/SKU123456789/*.jpg

thereby not needing to use the actual name of the image file in each folder.
- Collapse -
Thank you James for your reply.
May 26, 2017 9:56PM PDT

Unfortunately the shop system I am using doesn't let you link the path of the picture.
It automatically picks up the product image if the file name is the same as the product SKU.

And thank you the batch renaming link.
So I guess there is not simple way to do this......
I don't know which is faster, learning how to script a batch command (since I have never done it) or renaming 2600 files manually. (laughing sadly emoji )

- Collapse -
Only one image per SKU?
May 27, 2017 6:52AM PDT

What happens if you want to have more than one image per product? Have you tried your software with the supplied folder/file to see if maybe it works OK in that software "as is"?

Your problem is to create a list of folder names in a certain order, then rename files, in the proper order, with those folder names. You may also need to strip off characters other than the SKU number from folder names before making list to rename the files.

It's been a while since I've done batch files, but I do remember in DOS and probably also in Windows still, one could send a list of folder or file names to a text file to list them.

dir /ad >c:\names.txt

the list of folders in order would be in that text file, in the root directory.

Here's more use of the dir command which can help create a list of the files without the folders.

https://ss64.com/nt/dir.html

Ask around, you may find someone who can write a "script" or batch file to accomplish what you want for not too much moola. Just be sure to move a copy of all folders and files into a safe area to test the script, so you don't mess up the originals.

- Collapse -
This might help you
May 27, 2017 7:54AM PDT
Here's one for windows users.

a simple windows command can solve your problem. Read HELP FOR and the try this in the Windows command line:

for /d %a in (*) do @for %b in (%a\*) do @ren %a\%b %a%~xb

let's analyze it

the first for will iterate over all (*) the directories /d and for each found, passed in %a the second for will iterate over all the files it contains (%a\*) and for each file found %b it will do rename ren it %a\%b with the name of the folder it is contained in %a keeping the same extension it had %~xb.
shareimprove this answer

answered Feb 16 '15 at 20:01 "

Using the FOR command

https://www.windows-commandline.com/windows-for-loop-examples/

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true

Using Linux; (can be done from a LIVE DVD if you also create the script file, which runs in a "shell". (usually with *.sh file ending)

https://ubuntuforums.org/showthread.php?t=2126713

Using the FOR command in Linux.

https://askubuntu.com/questions/746860/rename-a-file-to-parent-directorys-name-in-terminal
- Collapse -
Answer
Renaming files to folder names
May 27, 2017 9:05AM PDT
https://stackoverflow.com/questions/28548929/rename-file-names-with-folder-names-maybe-use-python

"a simple windows command can solve your problem. Read HELP FOR and the try this in the Windows command line:

for /d %a in (*) do @for %b in (%a\*) do @ren %a\%b %a%~xb

let's analyze it

the first for will iterate over all (*) the directories /d and for each found, passed in %a the second for will iterate over all the files it contains (%a\*) and for each file found %b it will do rename ren it %a\%b with the name of the folder it is contained in %a keeping the same extension it had %~xb.
shareimprove this answer

answered Feb 16 '15 at 20:01 "


or;

Booting from a Linux LIVE DVD along with the script file to run, since LIVE Linux DVD has the Python program in it.

https://ubuntuforums.org/showthread.php?t=2126713

Using commands in Windows "run".

https://superuser.com/questions/483726/renaming-files-according-to-the-folder-theyre-in

remember, batch files in windows uses the "bat" file ending, such as *.bat

How to use the FOR command.

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true

Using the BRU or Bulk Rename Utility.

http://www.bulkrenameutility.co.uk/forum/viewtopic.php?t=648
- Collapse -
I will try it out
May 28, 2017 5:28PM PDT

Thank you for all the information, and sorry for my late response.

I will try them out and see which ones work out.

- Collapse -
Answer
Best BatchRenameFiles Tool
Aug 13, 2017 1:22AM PDT

Rename Folder with BatchRenameFiles Tool is very easy. It's don't wast time . I think It is the best BatchRenameFiles tool. thanks

- Collapse -
Re: best
Aug 13, 2017 3:22AM PDT

For the OP there is no "best". Any rename utility (and there are many) that has the option to include the folder name in the filename is equally good.
Are you sure the tool you recommend can do that? If it can't it's not "best" for the OP, but "wrong".

Post was last edited on August 13, 2017 3:23 AM PDT