Advertisement

02.29.2008 at 04:42PM PST, ID: 23205499
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Batch file to rename all files to name of parent directory keeping extension

Tags: microsoft, MSDOS
Hi Experts

I am trying to create a batch file to do the following:

With a given drive letter, go through all subdirectories and look in each directory.
If a file with extension .rpt exists, rename it to the same as the parent directory but maintain the .rpt extension.
The caveat is, in some directories there may be 2 files with that extension. If there are 2 I would want them renaming:  <dirname_1).rpt and <dirname_2>.rpt.
e.g. 2 subdirectories in E:\ are "20080228" and "20082702". The 1st has a file within that directory called 12345.rpt. The 2nd has 2 files 23456.rpt and 34567.rpt.
The end result would be have the file in the 1st end up as 20080228.rpt, and the 2 files in the 2nd end up as 20082702_1.rpt and 20082702_2.rpt.

As there are only 7 or so main directories (with up to 100 sub dirs) this can also be done by main dir as opposed to by drive letter if any easier.

Any suggestions gratefully received as I am struggling with this.
(Running on Winxp Pro if relevant).

Thanks

Nick
Start your free trial to view this solution
Question Stats
Zone: OS
Question Asked By: seriousnick
Solution Provided By: RQuadling
Participating Experts: 2
Solution Grade: A
Views: 78
Translate:
Loading Advertisement...
02.29.2008 at 04:59PM PST, ID: 21019123

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 06:29PM PST, ID: 21019564

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 07:00PM PST, ID: 21019655

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 07:01PM PST, ID: 21019663

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 07:05AM PST, ID: 21021907

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 12:56PM PST, ID: 21023488

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 01:16PM PST, ID: 21023577

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.01.2008 at 01:50PM PST, ID: 21023759

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.29.2008 at 04:59PM PST, ID: 21019123
I think this can only be done with some VB Script. Batch command syntax is way too primitive to do something like that without being a wizard.
 
02.29.2008 at 06:29PM PST, ID: 21019564

Rank: Master

A doddle for batch scripting. Can you create a quick zip of the sort of thing you have for testing.
 
02.29.2008 at 07:00PM PST, ID: 21019655

Rank: Master

I hope this is what you are looking for.

I've added a LOT of comments to help you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
@ECHO OFF
REM Remove any local variables and allow for delayed expansion.
SETLOCAL ENABLEDELAYEDEXPANSION
 
REM Change drive and directory - you will probably want to edit this.
CD /D Z:\
 
REM Iterate the directory tree, executing the rest of the FOR in each directory.
FOR /R . %%A IN (.) DO (
 
	REM Report where we are.
	ECHO Examining %%A
	
	REM Store just the name of the parent folder for later use.
	SET ParentFolder="%%~nA"
	
	REM Initialize the file counter.
	SET FileCount=0
	
	REM Iterate the found subdirectory, counting the files and remembering the last name.
	FOR %%B IN ("%%~A"\*.RPT) DO (
		SET /A FileCount+=1
		SET FileName="%%~B"
	)
	
	REM If only 1 file was found then rename it.
	IF !FileCount! EQU 1 (
		ECHO Renaming !FileName! to !ParentFolder!.RPT
		REN !FileName! !ParentFolder!.RPT
	)
	
	REM If many files were found, then reset the counter and re-iterate the folder renaming as we go.
	IF !FileCount! GTR 1 (
		SET FileCount=0
		FOR %%B IN ("%%~A"\*.RPT) DO (
			SET /A FileCount+=1
			ECHO Renaming "%%~B" to !ParentFolder!_!FileCount!.RPT
			REN "%%~B" !ParentFolder!_!FileCount!.RPT
		)
	)
)
Open in New Window
 
02.29.2008 at 07:01PM PST, ID: 21019663

Rank: Master

If you run the same script multiple times, nothing changes.

I get output like this ...

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Examining Z:\.
Examining Z:\Reports\.
Examining Z:\Reports\20080201\.
Renaming "Z:\Reports\20080201\.\20080201.RPT" to "20080201".RPT
Examining Z:\Reports\20080202\.
Renaming "Z:\Reports\20080202\.\20080202_1.RPT" to "20080202"_1.RPT
Renaming "Z:\Reports\20080202\.\20080202_2.RPT" to "20080202"_2.RPT
Renaming "Z:\Reports\20080202\.\20080202_3.RPT" to "20080202"_3.RPT
Renaming "Z:\Reports\20080202\.\20080202_4.RPT" to "20080202"_4.RPT
Renaming "Z:\Reports\20080202\.\20080202_5.RPT" to "20080202"_5.RPT
Renaming "Z:\Reports\20080202\.\20080202_6.RPT" to "20080202"_6.RPT
Renaming "Z:\Reports\20080202\.\20080202_7.RPT" to "20080202"_7.RPT
Renaming "Z:\Reports\20080202\.\20080202_8.RPT" to "20080202"_8.RPT
Examining Z:\Reports\20080203\.
Renaming "Z:\Reports\20080203\.\20080203_1.RPT" to "20080203"_1.RPT
Renaming "Z:\Reports\20080203\.\20080203_2.RPT" to "20080203"_2.RPT
Open in New Window
 
03.01.2008 at 07:05AM PST, ID: 21021907
RQuadling - this is fantastic and works 100% with the info I gave.
However, I failed to mention (sorry) that some of the directory names have a time extension (if created twice in a day for example) and could be of the format  20080229.1200
In this case, the batch drops everything after the "."

Is it possible to alter this to take this into account?

Many thanks for your "wizardry".
 
03.01.2008 at 12:56PM PST, ID: 21023488

Rank: Master

Changed

      REM Store just the name of the parent folder for later use.
      SET ParentFolder="%%~nA"


to

      REM Store just the name of the parent folder for later use.
      SET ParentFolder="%%~nxA"
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
@ECHO OFF
REM Remove any local variables and allow for delayed expansion.
SETLOCAL ENABLEDELAYEDEXPANSION
 
REM Change drive and directory - you will probably want to edit this.
CD /D Z:\
 
REM Iterate the directory tree, executing the rest of the FOR in each directory.
FOR /R . %%A IN (.) DO (
 
	REM Report where we are.
	ECHO Examining %%A
	
	REM Store just the name of the parent folder for later use.
	SET ParentFolder="%%~nxA"
	
	REM Initialize the file counter.
	SET FileCount=0
	
	REM Iterate the found subdirectory, counting the files and remembering the last name.
	FOR %%B IN ("%%~A"\*.RPT) DO (
		SET /A FileCount+=1
		SET FileName="%%~B"
	)
	
	REM If only 1 file was found then rename it.
	IF !FileCount! EQU 1 (
		ECHO Renaming !FileName! to !ParentFolder!.RPT
		REN !FileName! !ParentFolder!.RPT
	)
	
	REM If many files were found, then reset the counter and re-iterate the folder renaming as we go.
	IF !FileCount! GTR 1 (
		SET FileCount=0
		FOR %%B IN ("%%~A"\*.RPT) DO (
			SET /A FileCount+=1
			ECHO Renaming "%%~B" to !ParentFolder!_!FileCount!.RPT
			REN "%%~B" !ParentFolder!_!FileCount!.RPT
		)
	)
)
Open in New Window
Accepted Solution
 
03.01.2008 at 01:16PM PST, ID: 21023577

Rank: Master

Should change the comment with it too.

REM Store the name and extension of the parent folder for later use.
 
03.01.2008 at 01:50PM PST, ID: 21023759
Fantastic. Points are yours with a much deserved A grade.

This solution will also be very useful in something else.
Rather than "bleed" from this question, I have opened another here:
http:Q_23206697.html.


Hopefully you can get to it .......

Thx again

Nick
 
 
03.01.2008 at 01:58PM PST, ID: 21023795
Answered.
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628