reverse Filter String until FileDoesExist

I want to create loop that runs through a string until it finds the first DoesFileExist == true and then execute the script.

for example:

varString = C:\users\mike\donuts\bunnies\v01

(run backwards through string checking until true) varString
C:\users\mike\donuts\bunnies\v01 = FALSE
C:\users\mike\donuts\bunnies = FALSE
C:\users\mike\donuts = TRUE

stringn = "C:\Users\Joker\Desktop\cool\maybe\v01"
 
for i=1 to stringn.count do
(
	tempStream = (substring stringn 1 (stringn.count as integer - i))
	isValidDir = doesFileExist tempStream
 
	print tempStream
	print isValidDir
)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
JokerMartini's picture

Thanks

Thank you Anubis for helping out.

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Anubis's picture

my logical mistake :)

it happens often when i write a directly to the site without testing :) so, thanks for asking. Now the While loop not break due to the boolean test (pathParts.count > 0) that return true to the end. So, here is fixed function:

fn searchPath str = (
	local result = ""
	local isValidDir = false
	local pathParts = filterString str "\\"
	for n = pathParts.count to 1 by -1 while not isValidDir do
	(
		local tmpStr = ""
		for i = 1 to n do tmpStr += pathParts[i] + "\\"
		if (isValidDir = doesFileExist tmpStr) do result = tmpStr
	)
	result
)

my recent MAXScripts RSS (archive here)

JokerMartini's picture

Anubis

When I run the code below it returns = "C:\"
When in reality the folders go as deep as = "C:\Users\Joker\Desktop\cool\"

So it should be returning that = "C:\Users\Joker\Desktop\cool\

Why is that?

myPath= "C:\Users\Joker\Desktop\cool\maybe\v01"
fn searchPath str = (
	local result = ""
	local isValidDir = false
	local pathParts = filterString str "\\"
	while pathParts.count > 0 or not isValidDir do (
		local tmpStr = ""
		for p in pathParts do tmpStr += p + "\\"
		if (isValidDir = doesFileExist tmpStr) do result = tmpStr
		deleteItem pathParts pathParts.count
	)
	result
)
 
searchPath myPath

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Anubis's picture

I think you can optimize a

I think you can optimize a bit the function if you kick out the 'return' call. Something like ... (I write it on-the-fly, ie not tested):

fn searchPath str = (
	local result = ""
	local isValidDir = false
	local pathParts = filterString str "\\"
	while pathParts.count > 0 or not isValidDir do (
		local tmpStr = ""
		for p in pathParts do tmpStr += p + "\\"
		if (isValidDir = doesFileExist tmpStr) do result = tmpStr
		deleteItem pathParts pathParts.count
	)
	result
)

my recent MAXScripts RSS (archive here)

JokerMartini's picture

solution if anyone needs it!

stringn = "C:\Users\Joker\Desktop\cool\maybe\v01"
 
fn searchString = (
	pathParts = filterString stringn "\\"
	while pathParts.count > 0 do
	(
		testString = ""
		for p in pathParts do testString += p + "\\"
		print testString
		isValidDir = doesFileExist testString
		if isValidDir == true then
			return tempStream
		deleteItem pathParts pathParts.count
	)
)
dirThatExists = searchString()

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.