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 : Saturday, August 31, 2019 3:00:30 AM(UTC)
adyy

Rank: Member

Reputation:

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

Was thanked: 1 time(s) in 1 post(s)
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 : Saturday, August 31, 2019 3:26:23 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 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 Saturday, August 31, 2019 3:28:01 AM(UTC)  | Reason: Not specified

adyy  
#3 Posted : Saturday, August 31, 2019 3:55:08 AM(UTC)
adyy

Rank: Member

Reputation:

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

Was thanked: 1 time(s) in 1 post(s)
My problem has been solved. Thank you for your timely reply.
Users browsing this topic
Guest
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.