Avatar of Vicki05
Vicki05
Flag for United States of America asked on

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

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.
Windows BatchWindows OSProgramming

Avatar of undefined
Last Comment
Vicki05

8/22/2022 - Mon
oBdA

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

Vicki05

ASKER
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
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vicki05

ASKER
Hi oBdA,

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

Vicki
Your help has saved me hundreds of hours of internet surfing.
fblack61
Vicki05

ASKER
Thanks Mate,

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