Solved

Batch Script to Rename Specific files in Sub Folders

Posted on 2014-10-22
4
347 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 53

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 53

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

838 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