?
Solved

Batch Script to Rename Specific files in Sub Folders

Posted on 2014-10-22
4
Medium Priority
?
358 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 56

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 56

Accepted Solution

by:
Bill Prew earned 2000 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
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.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Suggested Courses

771 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