Solved

Batch Script to Rename Specific files in Sub Folders

Posted on 2014-10-22
4
340 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
  • 2
4 Comments
 
LVL 51

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 24

Expert Comment

by:lionelmm
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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Know what services you can and cannot, should and should not combine on your server.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

762 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

22 Experts available now in Live!

Get 1:1 Help Now