?
Solved

Rename Script - File in folder gets same name as folder

Posted on 2016-07-31
13
Medium Priority
?
59 Views
Last Modified: 2016-07-31
Hi,

I need a script or a handy application that can help me with this.


I have 400 folders - all folders has the correct name - In every folder i got 1 file.

I want this file to get the same name as the folder its placed in.


Exampel

Foldername ;

"Stew - High"

The file inside should then be ;

"Stew - High" + extension.


Thank you in advice
0
Comment
Question by:HITCENTER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 56

Expert Comment

by:McKnife
ID: 41736574
Take his oneliner:
for /f %a in ('dir /b c:\test') do ren c:\test\%a\*.* %a.*

Open in new window

This assumes the 400 folders are below c:\test.
0
 

Author Comment

by:HITCENTER
ID: 41736582
Hmm.

Dosent work.

I get en error ;

"do was unexpected at this time."

Have created it like a CMD file.
0
 
LVL 56

Expert Comment

by:McKnife
ID: 41736585
open cmd.exe, and launch that code line.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:HITCENTER
ID: 41736590
Well somthing is wrong :) - Sorry

Can you figure it out ?

Thank you
0
 

Author Comment

by:HITCENTER
ID: 41736591
Attached file
CMD.png
0
 
LVL 56

Accepted Solution

by:
McKnife earned 1000 total points
ID: 41736601
Your screenshot shows you did not paste the line only, but you added more to it.
I looked at the folders, and of course blanks in folder names need to be taken care of, so open cmd and launch
for /f "delims=" %a in ('dir /b c:\users\jje\desktop\test') do ren c:\users\jje\desktop\test\"%a"\*.* "%a".*

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41736615
Though that works, it is bad style (because it uses a bug's side effect). The double quotes should always enclose the whole path. It is always a good idea to make sure the FOR variable does not already contain the enclosing double quotes, too.
I would also make sure only folders are retrieved by the DIR.
for /f "delims=" %a in ('dir /b /a:d c:\users\jje\desktop\test') do ren "c:\users\jje\desktop\test\%~a\*.*" "%~a.*"

Open in new window

0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 41736625
Just to compare, here the PowerShell (3+) line (doesn't matter if contained in a script or used at the prompt):
get-childitem 'c:\users\jje\desktop\test' -Directory | get-childitem -File | rename-item -NewName {$_.Directory.BaseName+$_.Extension}

Open in new window

Not really a difference.
1
 

Author Comment

by:HITCENTER
ID: 41736664
Thank you! It works.

I used Qlemo's powershell script!

I am a bit unsure who to give the point. McKnife gave the correct answer i think.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41736730
Usually you accept the answer you used and/or are certain it works. However, you might accept both solutions and split the points, equally or not. Both solutions are worth the same IMHO.
0
 
LVL 56

Expert Comment

by:McKnife
ID: 41736740
Qlemo, I don't mind if you call it bad style. If there would be cases where it would lead to unwanted results, I would like to see those so I could learn from it. If there were files at the same level as the folders, it would not lead to unwanted results, nor would the variable already contain enclosing double quotes - if I don't see it, please help me with an example.

The points don't matter to me.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41736748
Files in the root folder lead to an error message, if I'm correct (didn't test yet).

One reason for not to enclose parts of the path only in double quotes is that you then need to enclose each part individually...
0
 

Author Closing Comment

by:HITCENTER
ID: 41736772
Thank you!

Points are splitted
0

Featured Post

Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Optimized for private cloud infrastructures and datacenters, Nano Server is minimalistic, yet super-efficient, OS for services such as Hyper-V and Hyper-V cluster. Learn how you can easily deploy Nano Server and unlock its power!
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question