Community discussions

MikroTik App
 
xxcat
just joined
Topic Author
Posts: 7
Joined: Tue Apr 05, 2005 2:04 pm

check if file exists?

Thu Aug 17, 2006 2:16 pm

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

Thanks!
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Thu Aug 17, 2006 6:44 pm

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

Does this help?

Sam
 
xxcat
just joined
Topic Author
Posts: 7
Joined: Tue Apr 05, 2005 2:04 pm

Thu Aug 17, 2006 9:19 pm

Yes! Thanks ALOT!
 
jvolkhausen
just joined
Posts: 5
Joined: Fri Apr 26, 2019 8:44 am

Re: check if file exists?

Wed Apr 22, 2020 11:19 am

Thanks also from me.
Helps me very much after a lot of wasted time.
 
User avatar
ariosvelez
newbie
Posts: 29
Joined: Mon Mar 11, 2013 5:39 pm
Location: Ocala, FL
Contact:

Re: check if file exists?

Tue Apr 12, 2022 12:25 am

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\$"]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: check if file exists?

Tue Apr 12, 2022 12:35 am

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
 
Rugx
Member Candidate
Member Candidate
Posts: 123
Joined: Thu Jan 02, 2020 1:44 pm

Re: check if file exists?

Tue Apr 12, 2022 1:20 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: check if file exists?

Tue Apr 12, 2022 1:41 pm

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?
 
Rugx
Member Candidate
Member Candidate
Posts: 123
Joined: Thu Jan 02, 2020 1:44 pm

Re: check if file exists?

Tue Apr 12, 2022 1:55 pm

If it's not necessary to be delivered asap, then sure
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: check if file exists?

Tue Apr 12, 2022 2:21 pm

I write something about for all...
Last edited by rextended on Tue Apr 12, 2022 2:30 pm, edited 3 times in total.
 
Rugx
Member Candidate
Member Candidate
Posts: 123
Joined: Thu Jan 02, 2020 1:44 pm

Re: check if file exists?

Tue Apr 12, 2022 2:26 pm

Before post something, better test it, not?
Pardon, had to adjust to the exact file type name to type=".ncfg file" but you probably guessed it already
https://ibb.co/JznpKGQ
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: check if file exists?

Tue Apr 12, 2022 2:30 pm

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"
 
User avatar
Albirew
just joined
Posts: 8
Joined: Wed Oct 14, 2020 7:16 pm

Re: check if file exists?

Fri Jun 30, 2023 12:44 am

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).
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: check if file exists?

Fri Jun 30, 2023 1:14 am

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.

Who is online

Users browsing this forum: Bing [Bot], JDF, stefanau and 19 guests