StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
adyy  
#1 Posted : Friday, August 30, 2019 9:00:30 PM(UTC)
adyy

Rank: Member

Reputation:

Groups: Approved
Joined: 8/25/2019(UTC)
Posts: 16
China

Hi, Rob.

How to detect the existence of a folder or file? I want to perform the action according to different results.
Could you help me?

For example:E:\Users\abc,E:\Users\cba.txt
Rob  
#2 Posted : Friday, August 30, 2019 9:26:23 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 401
United States

Thanks: 5 times
Was thanked: 78 time(s) in 68 post(s)
You can use the standard .NET calls for this. The only slight difference are the prefixes I have to expose them to the script engine; "clr." below.

Otherwise, it's just a method of the .NET File class and Directory class. Look at the left menu of those pages to see the other methods/properties that can also be used.

Most of the time, you can Google "c# how to check if file exists" and the example code will be nearly the same after converted to a script in S+; but of course there are a lot of variables and possibilities.

Here's a few example scripts:

Code:
//Escape backslashes with an extra backslash (\\)
if(clr.System.IO.Directory.Exists("C:\\Windows")) {
    sp.MessageBox("C:\\Windows Folder Exists", "Found");
}

//Note the ! before clr, means NOT
if(!clr.System.IO.Directory.Exists("C:\\DoesNotExist")) {
    sp.MessageBox("C:\\DoesNotExist Folder Does Not Exist", "Not Found");
}

//Can compare to true, but means the same
if(clr.System.IO.File.Exists("C:\\Windows\\win.ini") == true) {
    sp.MessageBox("C:\\Windows\\win.ini File Exists", "Found");
}

//Instead of ! can compare to false
if(clr.System.IO.File.Exists("C:\\Windows\\DoesNotExist.ini") == false) {
    sp.MessageBox("C:\\Windows\\DoesNotExist.ini File Does Not Exist", "Not Found");
}

Edited by user Friday, August 30, 2019 9:28:01 PM(UTC)  | Reason: Not specified

adyy  
#3 Posted : Friday, August 30, 2019 9:55:08 PM(UTC)
adyy

Rank: Member

Reputation:

Groups: Approved
Joined: 8/25/2019(UTC)
Posts: 16
China

My problem has been solved. Thank you for your timely reply.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.