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
61 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
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
Hi oBdA,

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

Vicki
0
 

Author Closing Comment

by:Vicki05
Comment Utility
Thanks Mate,

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

Featured Post

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
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…
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…

763 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

8 Experts available now in Live!

Get 1:1 Help Now