Solved

renaming multiple files in separate folder in same directory

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
BAT FILE ERROR LOG Question 17 71
Running Multiple Access Macros from batch file or vba 19 501
Xcopy / Robocopy Question 7 75
Recursively Delete Files 5 111
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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