Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

renaming multiple files in separate folder in same directory

Posted on 2012-03-20
5
Medium Priority
?
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 12

Expert Comment

by:ibrahim52
ID: 37744269
Its free but check if it helps. Good luck.

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

Expert Comment

by:ReneGe
ID: 37745161
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
ID: 37745186
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 2000 total points
ID: 37745202
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
ID: 37844388
Glad I could help and thanks for the points.

Cheers,
Rene
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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