Solved

renaming multiple files in separate folder in same directory

Posted on 2012-03-20
5
329 Views
Last Modified: 2012-08-14
Hi,
I have 2 files in several directories

directory is C:\Data
folders are

Data_DM_UN_20120312-1654
in folder are 2 csv files
            current.csv
           active.csv
Data_DM_UN_20120501-2114
in folder are 2 csv files
            current.csv
           active.csv
I want to rename the csv files in each folder in directory is C:\Data to part of the folders name, when done the csv files should be

Data_DM_UN_20120312-1654
            current_20120312-1654.csv
           active_20120312-1654.csv
Data_DM_UN_20120501-2114
in folder are 2 csv files
            current_20120501-2114.csv
           active_20120501-2114.csv

how do I do this in dos or bat file?  thanks
0
Comment
Question by:elucero
  • 4
5 Comments
 
LVL 12

Expert Comment

by:ibrahim52
Comment Utility
Its free but check if it helps. Good luck.

http://www.bulkrenameutility.co.uk/Main_Intro.php
0
 
LVL 10

Expert Comment

by:ReneGe
Comment Utility
I am a big fan of http://www.bulkrenameutility.co.uk/Main_Intro.php.

However, this application may require to many manual intervention.

I just scripted a batch file that, under your parameters, will do exactly what you asked for.

Make sure you test it before doing it for real.

Enjoy!


@ECHO OFF

SET Directory=C:\Data

REM RENAMING FILES
   FOR /D %%A in ("%Directory%\*") do (
      REM DEFINING NEW NAME SUFFIX
      FOR /f "tokens=4 delims=_" %%B in ("%%~nA") do (
         REM FINDING FILES TO BE RENAMED
         FOR %%C in ("%%~fA\*.*") do (
            ECHO RENAMING "%%~C"
            ECHO TO: "%%~nC_%%B%%~xC"
            REN "%%~C" "%%~nC_%%B%%~xC"
            ECHO.
         )
      )
   )

PAUSE
EXIT
0
 
LVL 10

Expert Comment

by:ReneGe
Comment Utility
And it's short version would be:

@ECHO OFF
SET Directory=C:\Data
FOR /D %%A in ("%Directory%\*") do FOR /f "tokens=4 delims=_" %%B in ("%%~nA") do (
   FOR %%C in ("%%~fA\*.*") do REN "%%~C" "%%~nC_%%B%%~xC"
)
0
 
LVL 10

Accepted Solution

by:
ReneGe earned 500 total points
Comment Utility
For last, here is what I did to test my script:

@ECHO OFF

REM WILL CREATE A SUBFOLDER NAMED DATA IN THE SAME FOLDER AS THE BATCH FILE
SET Directory=%~dp0Data

IF EXIST "%Directory%" RD /s /q "%Directory%"
MD "%Directory%"

REM CREATING TEST FOLDERS
   SET Folder=%Directory%\Data_DM_UN_20120312-1654
   IF NOT EXIST "%Folder%" MD "%Folder%"
   ECHO TEST>"%Folder%\current.csv"
   ECHO TEST>"%Folder%\active.csv"

   SET Folder=%Directory%\Data_DM_UN_20120501-2114
   IF NOT EXIST "%Folder%" MD "%Folder%"
   ECHO TEST>"%Folder%\current.csv"
   ECHO TEST>"%Folder%\active.csv"

REM RENAMING FILES
   FOR /D %%A in ("%Directory%\*") do FOR /f "tokens=4 delims=_" %%B in ("%%~nA") do (
         FOR %%C in ("%%~fA\*.*") do REN "%%~C" "%%~nC_%%B%%~xC"
   )

EXIT
0
 
LVL 10

Expert Comment

by:ReneGe
Comment Utility
Glad I could help and thanks for the points.

Cheers,
Rene
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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

5 Experts available now in Live!

Get 1:1 Help Now