VBScript or Batch to rename files based on the name of the folder they are in.

Posted on 2007-11-29
Last Modified: 2010-04-21
Please keep in mind I am a complete scripting n00b.    This could be a batch file or vbs script.  I am open to other suggestions as well.

We are restoring an old database manually.   There are 500 or so folders in the directory listed below.


Each folder is named based on the name of the database entry.  

For example:


Each of these folders has a file in it called "".

Like so:  


 Please note that while these zip files are all named the same thing, they contain very different data.  

Each zip file needs to be named based on the folder it is in and moved to another directory (D:\Data\XactRestored\Done\)

For example:


would be renamed to:


then moved to:


The final result would look like this:


Question by:zreisman
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
  • 2
LVL 38

Expert Comment

ID: 20376185
Here is a batch script which should do what you want.

Paste the script below into a new file with a .cmd extension.  Double-clicking it should rename and move the files.

@echo off
set root=D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T
set target=D:\DATA\XactRestored\Done
for /F "tokens=* usebackq" %%G in (`dir "%root%" /A:D /B`) do (
 ren "%root%\%%G\" "%%G.ESX"
 move "%root%\%%G\%%G.ESX" "%target%"

Open in new window

LVL 30

Accepted Solution

SteveGTR earned 500 total points
ID: 20376297
Well, here's my version. It strips out the "name=" as requested:
@echo off
set baseDir=D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T
set destDir=D:\DATA\XactRestored\Done
pushd "%baseDir%"
for /f "tokens=*" %%a in ('dir /b /ad "name=*" 2^>NUL') do if exist "%%a\" call :PROCESS "%%a"
goto :EOF
set newName=
for /f "tokens=1,*" %%a in ('echo %~1') do set newName=%%b
if "%newName%"=="" echo Couldn't strip out "name=" from %~1&goto :EOF
set newName=%newName%.ESX
if exist "%destDir%\%newName%" del "%destDir%\%newName%"
move "%~1\" "%destDir%\%newName%"
goto :EOF

Open in new window


Author Closing Comment

ID: 31411715
Thanks.   Any recommendations where to go to achieve at least semi uber batching skills?
LVL 30

Expert Comment

ID: 20377335
Right where you are. EE is where I have learned my current batch skills. I'll try to figure out another person's problem and in the process enhance me batch skills. In this instance you could look at each line of the processing. Many people don't know that DOS commands have help.

for /?
pushd /?
if /?
set /?

Sometimes the help is hard to understand, but I try to solidify my understand by trying out the command and echoing out the results. I don't have to do this as much now, but I'll still consult the for help to look at the extensions at the bottom of the help (%~n, etc...).

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

749 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