Solved

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

Posted on 2007-11-29
4
672 Views
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.

D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T

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

For example:

name=DOE__JOHN
name=DOE__JANE
name=55-J058-872

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

Like so:  

D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T\name=DOE__JOHN\doc.zip
D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T\name=DOE__JANE\doc.zip
D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T\name=55-J058-872\doc.zip

 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:

D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T\name=DOE__JOHN\doc.zip

would be renamed to:

D:\Data\XactRestored\Data\Xactware\Xm8\data\estimates\instance=0\profile=2T\name=DOE__JOHN\DOE__JOHN.ESX

then moved to:

D:\DATA\XactRestored\Done\DOE__JOHN.ESX

The final result would look like this:

D:\DATA\XactRestored\Done\DOE__JOHN.ESX
D:\DATA\XactRestored\Done\DOE__JANE.ESX
D:\DATA\XactRestored\Done\55-J058-872.ESX




0
Comment
Question by:zreisman
  • 2
4 Comments
 
LVL 38

Expert Comment

by:Shift-3
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

setlocal
 

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\doc.zip" "%%G.ESX"

 move "%root%\%%G\%%G.ESX" "%target%"

)
 

endlocal

Open in new window

0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 20376297
Well, here's my version. It strips out the "name=" as requested:
@echo off
 

setlocal
 

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\doc.zip" call :PROCESS "%%a"
 

popd
 

goto :EOF
 

:PROCESS
 

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\doc.zip" "%destDir%\%newName%"
 

goto :EOF

Open in new window

0
 
LVL 1

Author Closing Comment

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

Expert Comment

by:SteveGTR
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...).
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

12 Experts available now in Live!

Get 1:1 Help Now