Solved

Batch Script to Rename Specific files in Sub Folders

Posted on 2014-10-22
4
354 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 54

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 54

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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