?
Solved

Batch file stumper

Posted on 2007-03-27
14
Medium Priority
?
167 Views
Last Modified: 2010-04-16
I am trying to write a command in a batch file that looks in a folder for the highest sequencial number, and renames it as the date.

IE, my backup program saves a file called BACKUP_000436.xml in a folder, and tomorrows will be BACKUP_000437.xml

Is there a command, that will look for the highest number, and when found, changes its name to the current date? such as BACKUP_032707, or even just the date.

Thanks
0
Comment
Question by:centurian102
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 1500 total points
ID: 18802753
Try this from the directory where the files exist:

@echo off

setlocal

call :GETDATEPARTS "%date%"

set filePrefix=BACKUP_
set fileExt=.xml

set newFile=%filePrefix%%mm%%dd%%yy:~2%%fileExt%

if exist "%newFile%" echo File %newFile% has already been created for today.&goto :EOF

set fileMask=%filePrefix%*%fileExt%

for /f "tokens=*" %%a in ('dir /o-n /b /a-d "%fileMask%" 2^>NUL') do (
  echo Renaming %%a %newFile%
  ren "%%a" "%newFile%"
  goto :EOF
)

echo Could not locate any files matching %fileMask%

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

Good Luck,
Steve
0
 

Author Comment

by:centurian102
ID: 18802830
That didn't work, I inserted several pauses, but I still couldn't see where it wasn't working???
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18802882
I tested it out on my XP/Pro machine and it worked fine.

What version of Windows do you have?

Did it issue any messages?

More information is required to help you.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:centurian102
ID: 18802977
I am running this on my server 2003

0
 

Author Comment

by:centurian102
ID: 18802991




Here is what I'm getting




@echo off

setlocal

call :GETDATEPARTS "%date%"

set filePrefix=BACKUP_
set fileExt=.xml

set newFile=%filePrefix%%mm%%dd%%yy:~2%%fileExt%

if exist "%newFile%" echo File %newFile% has already been created for today.&goto :EOF

set fileMask=%filePrefix%*%fileExt%

for /f "tokens=*" %%a in ('dir /o-n /b /a-d "%fileMask%" 2^>NUL') do (
  echo Renaming %%a %newFile%
  ren "%%a" "%newFile%"
  goto :EOF
)

echo Could not locate any files matching %fileMask%

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 

Author Comment

by:centurian102
ID: 18802999

C:\Program Files\VERITAS\Backup Exec\NT\Data>setlocal

C:\Program Files\VERITAS\Backup Exec\NT\Data>call :GETDATEPARTS "Tue 03/27/2007"


C:\Program Files\VERITAS\Backup Exec\NT\Data>set dt=Tue 03/27/2007

C:\Program Files\VERITAS\Backup Exec\NT\Data>set tok=1-3

C:\Program Files\VERITAS\Backup Exec\NT\Data>if "T" GTR "9" set tok=2-4

C:\Program Files\VERITAS\Backup Exec\NT\Data>set yyyy=

C:\Program Files\VERITAS\Backup Exec\NT\Data>pause
Press any key to continue . . .

C:\Program Files\VERITAS\Backup Exec\NT\Data>for /F "tokens=2-4 delims=.:/-, " %
a in ('echo Tue 03/27/2007') do (for /F "skip=1 tokens=2-4 delims=/-,()." %x in
('echo.|date') do set %x=%a  & set %y=%b  & set %z=%c )

C:\Program Files\VERITAS\Backup Exec\NT\Data>(for /F "skip=1 tokens=2-4 delims=/
-,()." %x in ('echo.|date') do set %x=03  & set %y=27  & set %z=2007 )

C:\Program Files\VERITAS\Backup Exec\NT\Data>set mm=03  & set dd=27  & set yy=20
07

C:\Program Files\VERITAS\Backup Exec\NT\Data>if not "" == "" set yy=

C:\Program Files\VERITAS\Backup Exec\NT\Data>pause
Press any key to continue . . .

C:\Program Files\VERITAS\Backup Exec\NT\Data>if 12007 LSS 1000 (if 2007 LSS 70 (
set yy=202007 )  else (set yy=192007 ) )

C:\Program Files\VERITAS\Backup Exec\NT\Data>if 103 LSS 100 set mm=003

C:\Program Files\VERITAS\Backup Exec\NT\Data>if 127 LSS 100 set dd=027

C:\Program Files\VERITAS\Backup Exec\NT\Data>pause
Press any key to continue . . .

C:\Program Files\VERITAS\Backup Exec\NT\Data>goto :EOF

C:\Program Files\VERITAS\Backup Exec\NT\Data>set filePrefix=BEX_CNTYBACKUP_

C:\Program Files\VERITAS\Backup Exec\NT\Data>set fileExt=.xml

C:\Program Files\VERITAS\Backup Exec\NT\Data>pause
Press any key to continue . . .
0
 

Author Comment

by:centurian102
ID: 18803009


C:\Program Files\VERITAS\Backup Exec\NT\Data>set newFile=BEX_CNTYBACKUP_032707.x
ml

C:\Program Files\VERITAS\Backup Exec\NT\Data>if exist "BEX_CNTYBACKUP_032707.xml
" echo File BEX_CNTYBACKUP_032707.xml has already been created for today.  & got
o :EOF

C:\Program Files\VERITAS\Backup Exec\NT\Data>set fileMask=BEX_CNTYBACKUP_*.xml

C:\Program Files\VERITAS\Backup Exec\NT\Data>pause
Press any key to continue . . .
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18803140
Please remove all debugging information you have placed in the batch processing.

I see you have changed certain aspects of the file, specifically filePrefix.

Please post the batch file as you are running it without the debugging information.

Also, please post any output without, if any.

We'll start from there.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18803148
The last request should read as:

Also, please post any output, if any.
0
 

Author Comment

by:centurian102
ID: 18803186
Here it is, I changed file prefix,

The file names are as follows BEX_CNTYBACKUP_0043

there is no output



@echo off

setlocal

call :GETDATEPARTS "%date%"

set filePrefix=BEX_CNTYBACKUP_
set fileExt=.xml

set newFile=%filePrefix%%mm%%dd%%yy:~2%%fileExt%

if exist "%newFile%" echo File %newFile% has already been created for today.&goto :EOF

set fileMask=%filePrefix%*%fileExt%

for /f "tokens=*" %%a in ('dir /o-n /b /a-d "%fileMask%" 2^>NUL') do (
  echo Renaming %%a %newFile%
  ren "%%a" "%newFile%"
  goto :EOF
)

echo Could not locate any files matching %fileMask%

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18803334
Do you run the batch file from the directory where the files exist?
0
 

Author Comment

by:centurian102
ID: 18803440
yes
0
 

Author Comment

by:centurian102
ID: 18803469
I found a work around, Thanks for all the help

echo %date%
if %date:~0,1% GTR 9 echo %date% appears to have the 'day' && goto :Daydate
echo %date% doesn't have the dayname...
SET FDATE=%DATE:~0,2%-%DATE:~3,2%-%DATE:~6%
goto :setup
:Daydate
SET FDATE=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10%
:setup

REN "BEX_CNTYBACKUP_*.xml" DailyBackup%FDATE%.xml

copy DailyBackup%FDATE%.xml Z:\

pause
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18803563
good luck
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

801 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