Source Code
Source Code

Reputation: 17

Getting info - if a folder exist ignoring varible

I am making a batch file which tells if a folder exist. \..\ - Consider is path is constant on all computers. \..\CompanyName\- Now i can check if this folder exists. \..\Companyname\Productname version x.x.x - Now I cannot check if "product" exits because if i use if exist "%constantpath%\Productname version 1.2.3" (code block) condition will only return true if Whole Name Matchs. If Productname version is 1.2.4 it will return false.so that batch file is good as dead.When i tried
set dest=Productname Version 1.2.3

set temp=%dest:~0,11% // which means "Productname"

if exist "%constantpath%\%temp% (code block) to see if only "Product name Exists it returns with error Syntax of command is incorrect. i think path is incorrect

Upvotes: 0

Views: 37

Answers (3)

npocmaka
npocmaka

Reputation: 57332

if exist accept wild cards so you can try:

if exist "%constantpath%\Productname version ?.?.?" (
  echo it exists
)

It also work with non normalized paths so you can use things like \..\

Upvotes: 1

Stephan
Stephan

Reputation: 56238

so that batch file is good as dead.

There's life in the old dog yet.

if exist can not work with wildcards, but dir can:

dir /s "%constantpath%\Productname version 1.*" >nul && (
  echo yes 
) || (
  echo no
)

This example will check for version 1.x.x

"%constantpath%\Productname*" will check for any version (with or without the string version .... Adapt to your needs.

In case you need the complete name:

for /f "tokens=*" %%i in ('dir /s /b "%constantpath%\Productname version 1.*"` do set "fullname=%%i"

Upvotes: 1

Dewi Jones
Dewi Jones

Reputation: 825

I'd highly recommend using powershell for this, i had to do something recently similar because i needed to run an installer that could be in a various location but the first portion would always remain the same. It would be as follows.

$BaseDir = "\..\CompanyName\"
$NameToFind = "ProductName"

$MyVariable = Get-ChildItem $BaseDir -Recurse | Where-Object { $_.PSIsContainer -and $_.Name -like ($NameToFind)}

$productVersion = $myvariable.FullName

Write-Host $productVersion

Upvotes: 0

Related Questions