• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

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.
0
Lionel MM
Asked:
Lionel MM
  • 9
  • 8
  • 3
2 Solutions
 
sirbountyCommented:
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
0
 
sirbountyCommented:
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

0
 
sirbountyCommented:
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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.
0
 
Bill PrewCommented:
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
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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.
0
 
Bill PrewCommented:
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
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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)
0
 
Bill PrewCommented:
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
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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.
0
 
Bill PrewCommented:
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
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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.
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
Any further ideas PLEASE.
0
 
Bill PrewCommented:
Okay, give this a try.

@echo off

set BaseDir=C:\Downloaded\Reports

for /f "tokens=*" %%A in ('dir /a-d /b /s "%BaseDir%\*.*"') do (
  call :RenameFile "%%~A"
)

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
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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.
0
 
Bill PrewCommented:
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
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
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.
0
 
Bill PrewCommented:
Okay, give this a try, I didn't get chance to test it, but did add some comments to help you understand it better.  Let me know how it goes.

@echo off
setlocal EnableDelayedExpansion

REM Specify base folder to process
set BaseDir=C:\Downloaded\Reports

REM Get todays date and make sure it has a leading zero if needed
set Today=%DATE:~-10%
set Today=%Today: =0%

REM Look at each directory under the base directory
for /d %%A in ("%BaseDir%\*.*") do (
  REM Get the date of the current directory
  set DirDate=%%~tA
  set DirDate=!DirDate:~0,10!
  REM See if it was changed today, if so process it
  if "!DirDate!" EQU "%Today%" (
    REM Look at each file in this subfolder
    for %%B in ("%%~A\*.*") do (
      REM Call subroutine to rename file to it's parent folder name
      call :RenameFile "%%~B"
    )
  )
)

REM Exit script
exit /b

:RenameFile [file-path]
  REM Make sure the file exists
  if exist "%~1" (
    REM Get the full path in a FOR loop variable for parsing
    for %%A in ("%~1") do (
      REM Get just the parent folders path in another FOR variable for parsing
      for %%B in ("%%~dpA.") do (
        REM Rename the file (old full path to parent fole name and original file extension)
        ren "%~1" "%%~nB%%~xA"
      )
    )
  )
  exit /b

Open in new window

~bp
0
 
Lionel MMSmall Business IT ConsultantAuthor Commented:
Thanks for the help--appreciate it.
0
 
Bill PrewCommented:
Welcome, glad it was useful.

~bp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now