This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Brod8362's profile picture

Says a file dosent exist when it actually does

Started by Brod8362, 24 May 2015 - 03:19 PM
Brod8362 #1
Posted 24 May 2015 - 05:19 PM
I have been writing code for a program, and when I tell it to run another programs it claims it doesnt exist, even if it does. Any help?
Spoiler
  • shell.run("clear")
  • print [[
  • Floppy DOS |User
  • ————————————————–
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • –START(S)—————————————-
  • ]]
  • input = read()
  • if input == "S" or "s" then
  • shell.run("fd/smenu")
  • end
  • if input ~= "S" or "s" then
  • shell.run("clear")
  • shell.run("fd/main")
  • end
Edited on 24 May 2015 - 03:20 PM
TYKUHN2 #2
Posted 24 May 2015 - 05:36 PM
Replace the
 if input ~=
to
else
Also for long strings like "—————–" try
string.rep("-", 17)

Can your filesystem be explained? Where are your files and folders? What are the names of them? From the code itself nothing appears wrong.
KingofGamesYami #3
Posted 24 May 2015 - 05:52 PM
What does this program give you?


print( fs.isDir( "fd" ) )
print( fs.exists( "fd/smenu" ) )
print( fs.exists( "fd/main" ) )

Also, [code][/code] tags are appreciated if the code is short (under 50 lines or so), pastebin if it is long.
Brod8362 #4
Posted 24 May 2015 - 06:26 PM
A couple things:
fd/main is the file I was already running. smenu is a file that just prints a line of text and nothing else. I ran the program you put in, and all 3 entries claimed true. something I noticed is that now, after restarting MC, the program just restarts itself no matter what you put in. I couldnt terminate, so I just booted of a floppy that gave me access to the command line. I have no idea what is going on. (Keep in mind the program used to just say the program didnt exist)
KingofGamesYami #5
Posted 24 May 2015 - 06:42 PM
I don't know why it'd say the program doesn't exist - it obviously does, according to my script.

If the program is named "startup" it will run when the computer turns on, otherwise it will not.

You should be able to terminate out of the program you posted, there was nothing preventing it. At a guess, I'd wonder if you didn't hold down control+t long enough (the time varies).

There is a flaw in your logic, though this shouldn't cause what you are experiencing:


if input == "S" or "s" then

Will always be true, because 's' isn't nil or false. This is mainly due to your misuse of 'or', the correct way would be this:


if input == "S" or input == "s" then

If you want some advice, I'd use string.lower.


if input:lower() == "s" then
Brod8362 #6
Posted 24 May 2015 - 07:01 PM
I changed the

if input == "S" or "s" then
statement to

if input == "S" or input == "s" then
but it still says the files dont exist.

I recorded a video that shows me running the program so you can get a better feeling of what I mean.
EDIT: for some reason, the video wont upload. Im going to see if I can get it anywhere else.
EDIT 2: Here is a link to the video. https://vid.me/oQH5
EDIT 3: image of the dir command being run. http://imgur.com/sI665Gl
Edited on 24 May 2015 - 05:13 PM
KingofGamesYami #7
Posted 24 May 2015 - 07:09 PM
Sorry, but I cannot access that link. It's not your fault though, it's my schools' web filter. Google Drive / DropBox aren't blocked, you could try one of those.
Brod8362 #8
Posted 24 May 2015 - 07:15 PM
Image link: https://drive.google.com/file/d/0BzU241LId1_5M19Gb3VnX1ZQamM/view?usp=sharing
Video Link: https://drive.google.com/file/d/0BzU241LId1_5OWpEQ2dnTm40em8/view?usp=sharing
KingofGamesYami #9
Posted 24 May 2015 - 07:40 PM
Ok, I think I know what it's doing. cd sets the directory for the shell, therefor using shell.run in your program is going to try to run the file from inside that directory.

To clarify, because you used cd fd before running main, shell is trying to run fd/fd/smenu. Which doesn't exist.

You can solve this by running the program without using cd, ei:

>fd/main
Brod8362 #10
Posted 24 May 2015 - 07:48 PM
Ok. I will try this to see if it works.

Instead of changing where I run it from, I change what it runs, so it runs without /fd so it just runs /fd/smenu instead of /fd/fd/smenu. This works, however I now have an unrelated problem that causes it to always go with option 2, which is to rerun the program. Ill figure that out later. Thanks for the help!
KingofGamesYami #11
Posted 24 May 2015 - 07:57 PM
The second if statement has the same problem as the first - misuse of 'or'. I'd suggest using an else, so that it runs if the first statement wasn't true.
Brod8362 #12
Posted 24 May 2015 - 08:21 PM
I fixed that. Now, smenu is the problem. I have a if and 2 elseifs. here is code:


shell.run("clear")
print [[
Start Menu								   |User
--------------------------------------------------
|Games	|  G		  
|Options  |  O
|Programs |  P
|		 |
|		 |
|		 |
|		 |
|		 |
|		 |
|		 |
|		 |
|		 |
|Terminal |  T
|Restart  |  R
--START(S)----------------------------------------
]]
input = read()
if input == "S" or input == "s" then
shell.run("main")
elseif input ~= "S" or input ~= "s" or input ~= "G" or input ~= "g" or input ~= "O" or input ~= "o" or input ~= to "P" or input ~= to "p" or input ~= "T" or input ~= "t" or input ~= "R" or input ~= "r" then
shell.run("clear")
shell.run("smenu")
elseif input == "T" or input == "t" then
shell.run("clear")
shell.run("shell")
end
problem is, it always restarts smenu unless S is selected. T is defined, but does not work.
Edited on 24 May 2015 - 06:21 PM
KingofGamesYami #13
Posted 24 May 2015 - 08:46 PM
Well, I'd do this:


if input == "S" or input == 's' then
  shell.run("main")
elsief input == "T" or input == "t" then
  shell.run("clear")
  shell.run("shell") --#why are you running the shell?  Just wondering
else --#notice there are no arguments
  shell.run("smenu")
end

This will run smenu if the input was not recognized in any if / elseif statements above.
Brod8362 #14
Posted 24 May 2015 - 08:48 PM
Thank you. Also, i'm running the shell because T is assigned to go to terminal.
KingofGamesYami #15
Posted 24 May 2015 - 09:04 PM
Why not just exit the program? That'd also go back to the shell.
Brod8362 #16
Posted 24 May 2015 - 09:14 PM
One final question: How do you have shell.run use parameters? Ex: shell.run("PROGRAM" PARAMETERfromFILE) except it works. Basically, how do you have shell.run use a parameter that you define in the program? See code below for details.

shell.run("clear")
term.setTextColor(16384)
print [[
Floppy DOS > Start Menu > Options            |User
--------------------------------------------------
|CPU ID   |  I          
|Move File|  MV
|Copy File|  CP
|Del File |  D
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |  
|         |  
--BACK(B)/>-----------------------------------------
]]
input = read()
if input == "i" or input == "I" then
shell.run("id")
sleep(3)
shell.run("omenu")
elseif input == "mv" or input == "MV" then
print("What File?")
filem = read()
print("Move to what location?")
move = read()
shell.run("mv" filem move)
shell.run("omenu")
elseif input == "cp" or input == "CP" then 
print("Copy What file?")
filec = read()
print("To Where?")
loc = read()
print("What Name?")
cpnane = read()
shell.run("cp" filec loc/cpname)
elseif input == "d" or input == "D" then
print("What File? (Include Directory)")
dfile = read()
shell.run("delete" dfile)
elseif input == "B" or input == "b" then
shell.run("clear")
shell.run("smenu")
else
shell.run("clear")
shell.run("omenu")
end
KingofGamesYami #17
Posted 24 May 2015 - 09:19 PM
If you have a newer version of CC, you can pass them as multiple arguments, eg.


shell.run("program", arg1, arg2 )

Otherwise, you'll need to concat them into a single string with spaces, eg.

shell.run( "program " .. arg1 .. " " .. arg2 )
Brod8362 #18
Posted 24 May 2015 - 09:21 PM
Thank you kind sir for all of your help! And also to what you said earlier, exiting the program is overrated.
MKlegoman357 #19
Posted 25 May 2015 - 06:03 AM
What do you mean it's overrated? If you want to exit your program - you exit, right? Not run the shell again. Running programs in that manner is not good either. Why? Well, imagine if in Windows everytime you exit a game it would not return to the desktop but run a new one. Any programs that were running inside the first desktop would still be running, eating processor power and RAM. Not to mention that the new desktop would also eat more RAM and CPU power. Evantually you'd run out of memory and your computer would crash.

In short, it's not good to be running shell each time you exit your program.