Basic Batch File If Exists Else Woes

I know this is very simple, but can't 'see' the error.  

I want a batch file to look for an MSI "install123.msi" and truncate the version to "install.msi" so I can point SCCM at "install.msi" and not have to keep up on version numbers as they evolve.  

This batch file works fine the first go-round in that is truncates the name.  My problem is that I can't get it to not delete the truncated file if a new "install124.msi" does NOT exist.

Thanks so much, script follows...

rem see if new version exists
if not exist f:\path\setupnx?????.msi (goto end)

if exist f:\path\setupnx?????.msi (

echo "Exists"
rem delete the existing/old msi
del f:\path\setupnx.msi
echo 'Delete'

rem rename the new MSI so that SCCM can use it.
move f:\path\setupnx*.msi f:\path\SetupNX.msi
echo 'rename'

) 

:end

Open in new window

whoamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
I think this should work for you, let me know if this doesn't make sense, or if you have questions.  Basically it looks for all setupnx*.msi files, and them if it's not just the base file name then does the rename.

set BaseDir=f:\path
set BaseName=setupnx
for %%A in ("%BaseDir%\%BaseName%*.msi") do (
  if /I "%%~nA" NEQ "%BaseName%" (
    if exist "%BaseDir%\%BaseName%.msi" del "%BaseDir%\%BaseName%.msi"
    ren "%%~A" "%BaseName%.msi"
  )
)

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
whoamAuthor Commented:
BP,

Works great. Just looking for some clarification.

%%A - what is this, what does it do?

if /I "%%~nA"  What does this mean & what is this doing?

Thanks.
0
Bill PrewCommented:
The FOR statement below:

for %%A in ("%BaseDir%\%BaseName%*.msi") do (

will look for all matching files (much like a DIR command), and then one at a time process them inside the "body" of the FOR statement, the part in parens after the DO.  Inside that block, the %%A "loop variable" will have the value of the file currently being processed.  So if there were 3 matching files, the loop body would execute 3 times, and %%A would have the file path of the current file each time.

if /I "%%~nA"

This is an IF, and the /I makes it case insensitive.  The first thing used for the compare will be the current file being processed in the loop, but the ~n "modifier" says to just look at the base name of the file, no path, no extension.  The purpose of this check is to determine if this is the default base file name, or a new file with the extra characters.  If it's just the base file name we don't do the file moving.

Do the following command at a prompt for some additional info on the FOR command, and then let me know if you want additional reference links, or have other questions.

FOR /?

~bp
0
whoamAuthor Commented:
BP,

I'd like more info, but not fair to milk this ticket when you've solved it.  I'll open another by the same name Part 2.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.