Solved

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

Posted on 2013-11-21
20
328 Views
Last Modified: 2013-12-05
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
Comment
Question by:lionelmm
  • 9
  • 8
  • 3
20 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 39666586
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 39666615
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 39666657
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39666820
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39666834
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39667916
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39667924
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39667925
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39668037
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39668782
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 39668887
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39669677
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39691005
Any further ideas PLEASE.
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 39694495
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39695244
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39695281
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
 
LVL 24

Author Comment

by:lionelmm
ID: 39695315
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
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39697336
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
 
LVL 24

Author Closing Comment

by:lionelmm
ID: 39698211
Thanks for the help--appreciate it.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39698418
Welcome, glad it was useful.

~bp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now