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

How can I file transfer

Started by THC_Butterz, 02 March 2015 - 01:16 AM
THC_Butterz #1
Posted 02 March 2015 - 02:16 AM
Heres what I want to do and dont know where to start(I dont know anything about the modem api or the io api):

computer 1,
check to see if computer 2 has file xyz
if computer2 has file xyz end
else send file
end

computer 2
check to see if file xyz exists
if xyz does not exist recieve file
stay waiting till files recieved
else
end
Edited on 02 March 2015 - 01:19 AM
HPWebcamAble #2
Posted 03 March 2015 - 01:39 AM
Modem API:
http://computercraft.info/wiki/Modem_(API)

FS API (Very similar to the IO api, this is what I use):
http://computercraft.info/wiki/Fs_%28API%29

You obviously have a rough idea. It sounds a bit like a cloud server to me.

This is kinda long, but just stick with me for a bit.

Checking if a file exists:

fs.exists(path) --# Returns true or false if a file OR directory with that path exists.

Using Wireless Modems:

m = peripheral.wrap(side) --# Wrap it like any peripheral

m.transmit(channel,replyChannel,message) --# Sends the message on channel. Receiving computer gets the replyChannel, but it isn't really important, its just a number.

m.open(channel) --# Opens a channel. Use os.pullEvent() to wait for messages. Fires 'modem_message' when a message is received on an open channel. See below for the arguments

--# NOTE you don't need to open a channel to transmit on it.

m.close(channel) --# Closes the channel
m.closeAll() --# You'll never guess

'modem_message' arguments: Modem Name (It'll be the side if its touching the computer), channel it was received on, replyChannel, message, and distance to sender

You can always use 'help modems' or 'help fs' in a computer to refresh your memory


Finally, file interaction with FS api:

--# Opening a file to read it
file = fs.open(path,"r") --# path is the file path. "r" means read mode

file.readLine() --# Reads the next line in the file

--# Creating a file
file = fs.open(path,"w") --# path is file path, "w" means write mode

file.write(text) --# Writes the text to the file. Use \n to go to the next line in the file
--# For example
file.write("hi\n")
file.write("test")
--# Will be 
hi
test

--# Almost done! ALWAYS close your files. (It is what saves the changes when writing)
file.close()

If you don't do file.close(), java will keep the file from being modified by other programs.


There are other functions for both, but these are the basics. Check out the wikis for the others.

Good luck, I hope this helps :)/>
ElvishJerricco #3
Posted 03 March 2015 - 04:27 PM
Why use modems instead of the much simpler rednet?
HPWebcamAble #4
Posted 04 March 2015 - 12:46 AM
Why use modems instead of the much simpler rednet?

The main difference is that you can't use channels with rednet.

Other than that, they are pretty similar, but I still prefer the modem API
Edited on 03 March 2015 - 11:52 PM