Solved

renaming multiple files in separate folder in same directory

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to print and extire folder structure 6 45
hosts.ini- can it be made to block all websites? 14 80
CMD shell elevation.js 4 62
Run MS Access Module from Macro / Bat File 9 75
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

23 Experts available now in Live!

Get 1:1 Help Now