Renaming Files With the Names of Their Parent Directories
Moderator: DosItHelp
-
- Posts: 10
- Joined: 09 Jul 2014 03:53
Renaming Files With the Names of Their Parent Directories
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.
Re: Renaming Files With the Names of Their Parent Directorie
I'm intrigued, are the files embroidery designs, digital CCTV recordings or photo album panoramas?
-
- Posts: 10
- Joined: 09 Jul 2014 03:53
Re: Renaming Files With the Names of Their Parent Directorie
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.
Re: Renaming Files With the Names of Their Parent Directorie
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).
-
- Posts: 10
- Joined: 09 Jul 2014 03:53
Re: Renaming Files With the Names of Their Parent Directorie
To satisfy your curiosity, they are zipped fonts, with the full names available in the parent directories, and short, nondescript names on the zips.
Re: Renaming Files With the Names of Their Parent Directorie
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?
Re: Renaming Files With the Names of Their Parent Directorie
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
Re: Renaming Files With the Names of Their Parent Directorie
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.
-
- Posts: 10
- Joined: 09 Jul 2014 03:53
Re: Renaming Files With the Names of Their Parent Directorie
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.
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.
Re: Renaming Files With the Names of Their Parent Directorie
transitionality wrote:foxidrive's script doesn't check for extensions when renaming, but Compo's does.
A 2 second fix.
Re: Renaming Files With the Names of Their Parent Directorie
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.
Re: Renaming Files With the Names of Their Parent Directorie
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.
Re: Renaming Files With the Names of Their Parent Directorie
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