?
Solved

Check if a folder exists in a batch file, If it does to rename it with date and time stamp on the current drive.

Posted on 2016-07-19
5
Medium Priority
?
83 Views
Last Modified: 2016-07-19
Hi,

I was hoping someone can help me with a quick solution. I need to use this on a Windows 7 X64bit PC.

I need to verify if a folder exists on the current drive. If it does to rename it with date and time stamp. I need to do all this in a batch file. I want to be able to run it from a USB Drive.

Thanks,
Vicki.
0
Comment
Question by:Vicki05
[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
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41719445
Try this; it's in test mode and will only display the "ren" command it would normally run.
Remove the uppercase ECHO in line 10 to run it for real.
The script uses WMI to get the date, so it won't mind different OS versions and localizations.
@echo off
setlocal enabledelayedexpansion
set Folder=C:\Temp\Foo
if not exist "%Folder%" goto :eof
REM The next line sets the following DateTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%DT_Year%%DT_Month%%DT_Day%-%DT_Hour%%DT_Minute%%DT_Second%
for %%a in ("%Folder%") do set FolderName=%%~nxa
ECHO ren "%Folder%" "%FolderName%_%TimeStamp%"
if errorlevel 1 (
	echo ERROR: could not rename "%Folder%" to "%FolderName%_%TimeStamp%"
	pause
	exit /b 1
)

Open in new window

0
 

Author Comment

by:Vicki05
ID: 41719478
Hi oBdA,

The batch file works but the only issue I am running into now is that if the file is not there. I want it to continue with the rest of the batch file,  I don't want it to exit. Can you please help me with that?

Thanks,
Vicki
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41719484
You just need to add a label and change the goto:
@echo off
setlocal enabledelayedexpansion
set Folder=C:\Temp\Foo
if not exist "%Folder%" goto Continue
REM The next line sets the following DateTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%DT_Year%%DT_Month%%DT_Day%-%DT_Hour%%DT_Minute%%DT_Second%
for %%a in ("%Folder%") do set FolderName=%%~nxa
ECHO ren "%Folder%" "%FolderName%_%TimeStamp%"
if errorlevel 1 (
	echo ERROR: could not rename "%Folder%" to "%FolderName%_%TimeStamp%"
	pause
	exit /b 1
)
:Continue

Open in new window

0
 

Author Comment

by:Vicki05
ID: 41719503
Hi oBdA,

Thanks for all your help. You are a life saver.

Vicki
0
 

Author Closing Comment

by:Vicki05
ID: 41719515
Thanks Mate,

You have helped me so many times. I appreciate all your help.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

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.
Introduction to Processes
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ā€¦

765 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