If statement and getting file name

Shanan212 used Ask the Experts™
set aday = %date:~3,2%
set bday = %date:~3,2%
IF %aday% GTR %bday% echo "bigger"

Open in new window


I am somewhat good at batch-scripting but not to this advanced level :/

What I want to do is, check if file exists in a folder, get portion of the file name and extract a number (date) and check if the date is bigger than 20 then rename the file, if not copy it.

This being renaming the file. I have the code to rename it.

code to rename - working
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "C:\Users\%USERNAME%\Desktop\Database.accdb" "Database BACKUP "%%d-%%e-%%f.accdb

Open in new window

code to copy - working
copy "c:\Users\aravinds\Desktop\Database.accdb" "\\Fileserver\Everyone\ Database\Database\Database\BackUp"

Open in new window

Could anyone please help me with the following

- Check if file exisits with current month and year embded in its name

database - 05/23/2012.accdb

if then (above case yes since it has May & 2012), then check if the database name has date bigger than 20? (in this case, 23, so yes)

If then rename - I have code

if not copy - I have code

Thanks in advance!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Now first of all I don't like using dates based on %date% (see my article below for why) but if you are happy with that then will try and help.


You may be better off using

%userprofile% rather than c:\users\%Username% too btw as the profile could, for instance, be under "c:\documents and settings" if an upgraded older machine or any other path possibly.

This will go through all the databases for 05-2012 in filename order, i.e. 01 will come first and 31 last.  

for /f "tokens=1,2,3,4,5* delims=-. " %A in ('dir /b /a-d "Database BACKUP 05-??-2012.accdb"') do echo %A,%B,%C,%D,%E

will give you:


so you could do:

@echo off
REM GET DD, MM, YYYY .  Very unreliable with different date formats so be aware and use
REM different methods such as VBScript ones above if possible.

for /f "tokens=1-5 delims=/ " %%d in ("%date%") do (
  set MM=%%d
  set DD=%%e
  SET YYYY=%%f

REM Get listing of files for the current year and month

SET LastFile=
SET LastDay=0

for /f "tokens=1,2,3,4,5* delims=-. " %%A in ('dir /b /a-d "Database BACKUP %MM%-??-%YYYY%.accdb" 2^>NUL') do (
  echo File found for %%B - %%C - %%D
  Set LastFile=%%A %%B %%C-%%D-%%E.%%F
  Set LastDay=%%D

if NOT "%LastDay%"=="0" (
  echo The last file was %LastFile% which was from %LastDay%
IF %LastDay% GTR 20 (
    echo Do stuff for day over 20
    echo rename "%LastFile%" etc.
) ELSE (
  echo No file found that matches

Open in new window



No problem.  Did it work for Ok for you then?


Yes, I used the concept and adjusted here and there.

No problem, just checking you had all you needed.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial