check if file exists?

Is it possible to arrange this scenario:
script checks if some_file.rsc exists in /
if yes → do some job
else → exit

Thanks!

:if ([:len [/file find name=someconfig.rsc]] > 0) do={
:put “FOUND IT”
}

Does this help?

Sam

Yes! Thanks ALOT!

Thanks also from me.
Helps me very much after a lot of wasted time.

I need this script to run only if a file with the extension .ncfg exits I already found a way to find the file by extension as I have it here, just need to do that if the file exists.

/tool e-mail send to=“myemail@domain.com” subject=“just for you” file=[/file find where name~“.ncfg$”]

The command send all file with filename at least of 5 chars, ending with “ncfg” literally.
Examples:

xncfg
thisisnotdotncfg
micio.miaoncfg
noncfg
any.name.noextncfg

Instead to send only the files with name that end with .ncfg

Try something along the lines of:

:while ([:len [/file/find where type=ncfg]]=0) do={:delay 1}; :put "File with .ncfg is present"

*edited to remove unnecessary CPU usage by adding delay 1

I don’t think an “infinite” loop is better with CPU consume, than doing something on a scheduled basis …

and also thins:

[/file/find where type=ncfg]

never works.

Before post something, better test it, not?

If it’s not necessary to be delivered asap, then sure

I write something about for all…

Pardon, had to adjust to the exact file type name to type=“.ncfg file” but you probably guessed it already
https://ibb.co/JznpKGQ

If a file exist, do something.
2 general cases: a) by exact name, b) by file extension

Consideration about type of files:
RouterOS, for what I remember, know only this type of “file”:
disk, directory, backup (.backup), script (.rsc), userman backup (.umb)
the others are type “.extension file”, for example “.txt file”

Files without extension are simply identified as “file” on type, and must be searched by filename.

Consideration about name of files:
If the RB have Flash memory, the file can be inside /flash/ directory instead on root.
If exact file name is searched, must be specified also the internal path, like “flash/something.rsc”
This problem persist also if other paths are presents, Flash or NAND not mind.
The file name do not start with /

For not mess with { } I use global for examples.

a) check by exact name, if the exact name exist, do something, else nothing.

# if the file iphistory.txt can be on root
:global filename "iphistory.txt"
:global findresult [/file find where name="$filename"]
:if ([:len $findresult] > 0) do={
    :put "file $filename exist"
}
# if the file iphistory.txt can be on flash or another subdirectory
:global filename "flash/myfiles/iphistory.txt"
:global findresult [/file find where name="$filename"]
:if ([:len $findresult] > 0) do={
    :put "file $filename exist"
}

If the file must be searched everywhere ignoring path:

:global filename "iphistory.txt"
:global findresult [/file find where name~"(^|/)$filename\$"]
:if ([:len $findresult] > 0) do={
    :put "file $filename exist"
}

b) check by file extension, if at least one file wit that extension exist, do something, else nothing.

# if the extension is not recognized from RouterOS
:global fileext ".txt"
:global findresult [/file find where type="$fileext file"]
:if ([:len $findresult] > 0) do={
    :put "found $[:len $findresult] file(s) with $fileext extension"
}
# if the extension is recognized, like backup, from RouterOS
:global filetype "backup"
:global findresult [/file find where type=$filetype]
:if ([:len $findresult] > 0) do={
    :put "found $[:len $findresult] $filetype file(s)"
}

searching file extension not everywhere, but only on particular subdirectory

# if the extension is not recognized from RouterOS
:global fileext ".txt"
:global subdir "flash/myfiles/"
:global findresult [/file find where name~"^$subdir" and type="$fileext file"]
:if ([:len $findresult] > 0) do={
    :put "found $[:len $findresult] file(s) with $fileext extension inside the folder $subdir"
}
# if the extension is recognized, like backup, from RouterOS
:global filetype "backup"
:global subdir "flash/myfiles/"
:global findresult [/file find where name~"^$subdir" and type=$filetype]
:if ([:len $findresult] > 0) do={
    :put "found $[:len $findresult] $filetype file(s) inside the folder $subdir"
}

On all models, for invert “do if file(s) exist” to “do if file(s) NOT exist” simply replace “> 0” with “= 0”

Similarly, is it possible to check whether a folder exists?
I want to check if /flash/ exists or not before doing my stuff (some cards have it, other don’t).

The solution is in the previous post (thanks to @rextended), try to adapt it to your needs.


:global subdir "flash"
:global findresult [/file find where name~"^$subdir"]
:if ([:len $findresult] > 0) do={
    :put "found $[:len $findresult] the folder $subdir exist!"
}

BR.

How can I search for a USB in the file menu and extract the path name?

:if ($LastRun = $date) do={
    /file
    :foreach item in=[find where name~"usb" and type=disk] do={
        :local usbName [get $item name]
        :local usbState 
        :if ([:len $usbName] > 0) do={
            :set usbState "true"
            } else={
            :set usbState "false"
            :put $usbState
            }  
 
        }   
}