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

8/22/2022 - Mon
sirbounty

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)
Cannot create
sirbounty

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

sirbounty

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
Lionel MM

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.
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
ASKER
Lionel MM

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
ASKER
Lionel MM

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)
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
Lionel MM

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.
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
ASKER
Lionel MM

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Lionel MM

Any further ideas PLEASE.
SOLUTION
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Lionel MM

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.
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
Lionel MM

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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Lionel MM

Thanks for the help--appreciate it.
Bill Prew

Welcome, glad it was useful.

~bp
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.