IF EXIST Statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dukdpk
Posts: 13
Joined: 10 Mar 2011 03:27

IF EXIST Statement

#1 Post by dukdpk » 21 Mar 2011 23:07

Hi All

I want to modify following bat file based on number of requirements. My requirments are,

When user enter database name, have to check entered database name in the C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data. If it is exist then do,
for %%a in (R0001.bat R0002.bat R0003.bat R0004.bat R0005.bat R0006-R0017.bat R0018-R0029.bat) do (
>new%%a type nul
for /f "tokens=* delims=" %%b in (%%a) do (
set "str=%%b"
>>new%%a echo !str:TESTDB=%new%!
)

and create defined folders as well as copy created bat files to defined path.

if it is not exist then must display database name does not exist. And also if it is not exist give chance to enter database name again.

My bat file is as follows,

@echo off &setLocal enableDelayedExpansion
set old = TESTDB
set /p "new=Database name ? "
cd C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data

IF EXIST %new% (

for %%a in (r001.bat r002.bat r003.bat r004.bat ) do (
>new%%a type nul
for /f "tokens=* delims=" %%b in (%%a) do (
set "str=%%b"
>>new%%a echo !str:TESTDB=%new%!
)
)

) ELSE (
echo Database name %new% does not exist
)


md E:\Reports
md E:\Reports\Current


copy "C:\ReportsSource\BatfilesSource\*newr001.bat*" E:\Reports\Current
copy "C:\ReportsSource\BatfilesSource\*newr002.bat*" E:\Reports\Current
copy "C:\ReportsSource\BatfilesSource\*newr003.bat*" E:\Reports\Current
copy "C:\ReportsSource\BatfilesSource\*newr004.bat*" E:\Reports\Current

PAUSE



Please advise me how to achive my requirements.

Thank You

Post Reply