Renaming Files With the Names of Their Parent Directories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Renaming Files With the Names of Their Parent Directories

#1 Post by transitionality » 25 Jul 2014 08:29

I have a directory with a number of directories under it. Each directory has one and only one file with a certain extension xxx in it. Assume they might also contain other files with other extensions. I would like to rename the prefix of each xxx file with the name of its parent directory while maintaining its xxx extension. This will not cause naming conflicts, because each directory will continue to have one and only one file with extension xxx in it. Please help with a batch file. Thanks.
Last edited by transitionality on 25 Jul 2014 08:52, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Renaming Files With the Names of Their Parent Directorie

#2 Post by Compo » 25 Jul 2014 08:44

I'm intrigued, are the files embroidery designs, digital CCTV recordings or photo album panoramas?

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: Renaming Files With the Names of Their Parent Directorie

#3 Post by transitionality » 25 Jul 2014 08:50

I'm sure my problem is shared by people with somewhat different applications, so I omitted specifics and posed the question in as general a manner as possible. This way, people searching for similar solutions, whether on the forum or through general purpose search engines, can find it and make use of the solution.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Renaming Files With the Names of Their Parent Directorie

#4 Post by Compo » 25 Jul 2014 08:52

Try giving us the specifics now then, (the search will not be affected as it will be based upon that which you've already posted).

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: Renaming Files With the Names of Their Parent Directorie

#5 Post by transitionality » 25 Jul 2014 08:54

To satisfy your curiosity, they are zipped fonts, with the full names available in the parent directories, and short, nondescript names on the zips.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Renaming Files With the Names of Their Parent Directorie

#6 Post by Compo » 25 Jul 2014 08:59

So we've now established that when you stated XXX you meant zip. Okay what about the directory structure, based upon what you've said, I'm assuming all directories are only a single level and not nested several levels within each other. What about the base Operating Systems, Windows 95, 2000?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming Files With the Names of Their Parent Directorie

#7 Post by foxidrive » 25 Jul 2014 09:18

transitionality wrote:I'm sure my problem is shared by people with somewhat different applications, so I omitted specifics and posed the question in as general a manner as possible. This way, people searching for similar solutions, whether on the forum or through general purpose search engines, can find it and make use of the solution.


People leave out specific information and it OFTEN makes it hard to give an answer that works without several more questions about why it fails, and people get snitchy.


Here's some code - Remove the echo if the commands are right.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /b /s /a-d') do for /f "delims=" %%b in ("%%~dpa\.") do echo ren "%%a" "%%~nxb%%~xa"
pause

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Renaming Files With the Names of Their Parent Directorie

#8 Post by Compo » 25 Jul 2014 09:37

Here's something which may help your learning:

Code: Select all

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Setting desired extension
Set _ext=.zip
Rem Ensuring that we are working from the script directory
PushD %~dp0
Rem Checking the source location
Echo( Searching %~p0 for Directories containing %_ext% files for renaming
For /D %%a In (*) Do If Exist "%%a\*%_ext%" Call :Sub "%%a"
Exit/B

:Sub
PushD %1
Rem Ensuring only one file with desired extension exists in the directory
Set _cnt=0
For %%a In (*%_ext%) Do Set/A _cnt+=1
If %_cnt% NEq 1 GoTo :EOF
Echo( Renaming files according to their directory name
Ren *%_ext% "%~1%_ext%"
PopD
Last edited by Compo on 25 Jul 2014 16:23, edited 1 time in total.

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: Renaming Files With the Names of Their Parent Directorie

#9 Post by transitionality » 25 Jul 2014 13:23

Thank you, gentlemen, both scripts were helpful.

foxidrive's script doesn't check for extensions when renaming, but Compo's does.

In the end, the work got done, which is what matters.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Renaming Files With the Names of Their Parent Directorie

#10 Post by Squashman » 25 Jul 2014 13:31

transitionality wrote:foxidrive's script doesn't check for extensions when renaming, but Compo's does.

A 2 second fix.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming Files With the Names of Their Parent Directorie

#11 Post by foxidrive » 25 Jul 2014 20:51

transitionality wrote:Thank you, gentlemen, both scripts were helpful.

foxidrive's script doesn't check for extensions when renaming


Are you sure? It uses the extension of the file that is being parsed.

EG: A ZIP file gets a ZIP extension and a PDF gets a PDF extension and a TXT file gets a TXT extension, automatically.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Renaming Files With the Names of Their Parent Directorie

#12 Post by Compo » 25 Jul 2014 22:59

foxidrive, the opening post stated that there are other files in the directories too, not just a single file with a zip extension so as squashman alluded to you may have needed to add *.zip before closing your first 'for' command.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming Files With the Names of Their Parent Directorie

#13 Post by foxidrive » 26 Jul 2014 02:21

Compo wrote:foxidrive, the opening post stated that there are other files in the directories too, not just a single file with a zip extension so as squashman alluded to you may have needed to add *.zip before closing your first 'for' command.


Ohh. I read this

Each directory has one and only one file with a certain extension xxx in it.

and then this

because each directory will continue to have one and only one file with extension xxx in it.

He could always have added *.xxx :D

Post Reply