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
72 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 84

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 84

Accepted Solution

by:
oBdA earned 500 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

791 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