Avatar of Lionel MM
Lionel MMFlag for United States of America

asked on 

Rename file to be the same as the folder it is in

I have a client who downloads file from the internet using uTorrent. Not sure how this works but as he does he creates folders to put the files into. The folders are named in the format he wants the files to be named. Example--he downloaded this file and it was created and named sample-nova.2011-09-28.xls. It is located in folder named "Sample Nova 2011-09-28". He wants the files to be renamed to mirror the name of the folder, so can we read the folder name and then use that to rename the file. There will be only one file of each file type in the folder, so there may be a .xls or a .pdf or both but never two of the same type in the same folder. Thanks.
Windows BatchVB ScriptScripting Languages

Avatar of undefined
Last Comment
Bill Prew
Avatar of sirbounty
sirbounty
Flag of United States of America image

May depend on the OS, but this is generally not possible.
See the example here, where I create a file, and then immediately try to create a folder by the same name: (this is on Win7)
User generated image
Avatar of sirbounty
sirbounty
Flag of United States of America image

Whoops - misread your requirements...

Try this, it should work as a simple batch file.
Remove the ECHO from the code to have it actually make the changes - otherwise, it just displays the command that would be processed...

@echo off
cd /d c:\Downloads
for /f "delims=" %%a in ('dir /ad /b') do call :process "%%a"
goto :eof

:process
set folder=%1
echo Reading %folder%
for /f "delims=" %%f in ('dir %folder% /b') do ECHO ren %folder% "%%f"

Open in new window

Avatar of sirbounty
sirbounty
Flag of United States of America image

Arg - forgive me...not enough coffee today...this should do what you've asked.
Same deal - remove ECHO once confirmed.

@echo off
for /f "tokens=* delims=" %%a in ('dir /ad /b') do call :process "%%a"
goto :eof

:process
set folder=%1
echo Reading %folder%
for /f "delims=" %%f in ('dir %folder% /b') do ECHO ren "%folder:~1,-1%\%%f" "%folder:~1,-1%"

Open in new window

Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

Thanks so far but not working, not all the way. So far it is reading and renaming the file using the folder name as desired. Great, but the extension, the .pdf or the .doc from the file name is being lost in the renaming. So you end up with a file name without an extension.
Avatar of Bill Prew
Bill Prew

If you just need to do the files in a single folder, no subfolders, then you can do the following at a command line if you CD to the folder first:

for %N in ("%CD%") do ren *.* "%~nN.*"

Open in new window

or you could build a small BAT file as:

@echo off
pushd c:\basedir && (
  for %%N in ("%CD%") do ren *.* "%%~nN.*"
  popd
)

Open in new window

~bp
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

Unfortunately he downloads from 3 to 10 files each day, each in their own folder. Right now he manually copies the folder's name and then uses that to rename the file, leaving the existing extension as is. I will give [for %N in ("%CD%") do ren *.* "%~nN.*"] a try anyways.
Avatar of Bill Prew
Bill Prew

So are all the folders under one common folder?

Or are they always the same name?

I need a little more to go on if you want a solution that handles all files in one pass.

~bp
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

for %N in ("%CD%") do ren *.* "%~nN.*"
Does not work, at least not completely because the file names have more than one period (.) it renames the first section before the first period as needed but then leaves the rest of the filename, the characters on the right side of the first period in place.
Example
sample-nova.2011-09-28.xls becomes
Sample Nova.2011-09-28.xls (note the period) when what he wants is for it to become is
Sample Nova 2011-09-28.xls (no period after Nova)
Avatar of Bill Prew
Bill Prew

Here's a small BAT file that renames a specified file in the manner needed.  Once I understand where the other folders and files are that also need renaming we can adapt this.

@echo off

call :RenameFile "c:\temp\Sample Nova 2011-09-28\sample-nova.2011-09-28.xls"

exit /b

:RenameFile [file-path]
  if exist "%~1" (
    for %%A in ("%~1") do (
      for %%B in ("%%~dpA.") do (
        ren "%~1" "%%~nB%%~xA"
      )
    )
  )
  exit /b

Open in new window

~bp
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

All folders are located in C:\Downloaded\Reports--each day when he downloads files with Utorrent--it asks for a location and at that time he gives it the proper name, but only of the folder but he cannot control what the file name ends up being that is controlled by the creators of the reports in another city. Each day he downloads from 3 to 10 reports that he then renames manually.
Avatar of Bill Prew
Bill Prew

So, would the desired script look at EVERY folder contained in " C:\Downloaded\Reports" and rename all files in each of those folders to match their folder name? Or could there be other items in there that would get messed up?

~bp
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

Yes--the desired would script look at EVERY folder contained in " C:\Downloaded\Reports" and rename all files in each of those folders to match their folder name. There will be only one file most of the time but never two of the same type; never two .doc, never 2 .pdf's, if more than 1 it would be 1 .doc and 1 .pdf.
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

Any further ideas PLEASE.
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

PERFECT--works great thank you so much, thank you. Just one last question if I may -- on my test folders it was lightning fast and on the live folders it was also quite fast but each time I run it it goes through all the folders, is there a way to make it just read the folders created today. If that is easy for you then great if it is difficult then it is not worth your time--just would be nice to if quick to add. Again, thanks so much.
Avatar of Bill Prew
Bill Prew

A couple of questions.

(1) Are there just one level of folders, or are there subfolders within those folders that need to be processed?

(2) Would it suffice to only process the folders that had a create day of "today" exactly? If so that might not be too bad.

~bp
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

Yes there are just C:\Downloaded\Reports and nothing below "reports", no other folders. And Yes if you can do "today" only that would be perfect--again thanks for all your time on this.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Lionel MM
Lionel MM
Flag of United States of America image

ASKER

Thanks for the help--appreciate it.
Avatar of Bill Prew
Bill Prew

Welcome, glad it was useful.

~bp
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo