• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • Last Modified:

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.
0
Vicki05
Asked:
Vicki05
  • 3
  • 2
1 Solution
 
oBdACommented:
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
 
Vicki05Author Commented:
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
 
oBdACommented:
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
 
Vicki05Author Commented:
Hi oBdA,

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

Vicki
0
 
Vicki05Author Commented:
Thanks Mate,

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now