Solved

Batch Script to Rename Specific files in Sub Folders

Posted on 2014-10-22
4
355 Views
Last Modified: 2014-10-24
I need help with a Batch Script that checks several folders and renames .vbk files that were created in the past to .vbk_old, EXCEPT the lastest .vbk file generated.

I have a parent folder called Daily. Within this parent folder there are several sub folders where the .vbk file resides in.

I need to change all the old and past .vbk files to .vbk_old EXCEPT the lastest .vbk file residing in each folder.

Possibly the script needs to check for the timestamp of the .vbk file before renaming it.
0
Comment
Question by:ben1211
[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
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40398454
Okay, here's a quick BAT script that should get you started.  It processes each of the folders under the base folder you specify in the script, and in each of those process all but the newest VBK file and renames it.

Currently the actual rename command has an ECHO in front of it.  This way you can test the BAT script from a command line and it will display the REN commands it would execute to the screen, but not do the renames.  This way you can see what it will rename before it does any harm if it isn't right.  If your testing goes well, just remove the work ECHO before the REN command.

@echo off
setlocal

set BaseDir=c:\temp\base

for /d %%A in ("%BaseDir%\*.*") do (
  for /f "skip=1 tokens=*" %%B in ('dir /ad /o-n /b "%%~A\*.vbk"') do (
    ECHO ren "%%~B" "*.vbk_old"
  )
)

Open in new window

~bp
0
 

Author Comment

by:ben1211
ID: 40398694
Hi Bill

The script doesn't work. It doesn't rename the old *.vbk files.

In fact, I'm getting a message - File not found

Does the latest vbk file have to be of today's date? Or can it be any date, but the latest date compared to all the other vbk files.
0
 
LVL 25

Expert Comment

by:Lionel MM
ID: 40399238
did you change set BaseDir=c:\temp\base to where your files are? And no the script above will take the newest file and not rename it, even if it is not today's date
0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40400869
Sorry, small typo, try this, changing the base directory of course...

@echo off
setlocal

set BaseDir=c:\temp\base

for /d %%A in ("%BaseDir%\*.*") do (
  for /f "skip=1 tokens=*" %%B in ('dir /a-d /o-n /b "%%~A\*.vbk"') do (
    ECHO ren "%%~B" "*.vbk_old"
  )
)

Open in new window

~bp
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

688 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