Solved

renaming multiple files in separate folder in same directory

Posted on 2012-03-20
5
335 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 500 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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