# deleting files with small basic (help)

• ### Question

• Thank you for attention.

I'm newbie with small basic (programming at general).

So i wanted to make a program that deletes and replaces some files.

For example: i was playing game where admin have rights to send you a virus and change value of one text document. So i wanted to make program that will delete that file( censuree.cfg)   and  replace (config.cfg)

I've tried to use file.deletefile(C:dir\dir\.censuree.cfg)  and so one with replace file command. but always something turns up to be wrong. also i got error for (c: . . .) ":" changes it color like it's used for other action, can someone explain me how can i use those commands right? code example would be nice.

Tuesday, September 18, 2012 8:15 AM

• TheGuxi,

Here is a code snippet that creates a file and then reads it back.  You need to have a temp folder on the c: drive for it to work.  Also, you can look at the intellisense to see how the various commands work.

'Code Snippet-to create a file and read it back
Text="This is my Text"
File.WriteContents("c:\temp\text.txt",text)
TextWindow.WriteLine(MyData)

JR

JR

• Marked as answer by Tuesday, September 18, 2012 5:49 PM
Tuesday, September 18, 2012 2:27 PM
• Now,

1) please don't write all in bold letters.

2) your path is wrong. It isn't (C:dir\dir\.censuree.cfg) it must be (C:\dir\dir\.censuree.cfg) .

3) This is a code to delete a file:

File.DeleteFile("C:\dir\dir\.censuree.cfg")

4) You must set your destination in "". Otherwise will the program think, that your destination is a variable.

Greetings

Timo

Ich bin Neu

• Proposed as answer by Tuesday, September 18, 2012 2:43 PM
• Marked as answer by Tuesday, September 18, 2012 5:49 PM
Tuesday, September 18, 2012 2:41 PM

### All replies

• TheGuxi,

Here is a code snippet that creates a file and then reads it back.  You need to have a temp folder on the c: drive for it to work.  Also, you can look at the intellisense to see how the various commands work.

'Code Snippet-to create a file and read it back
Text="This is my Text"
File.WriteContents("c:\temp\text.txt",text)
TextWindow.WriteLine(MyData)

JR

JR

• Marked as answer by Tuesday, September 18, 2012 5:49 PM
Tuesday, September 18, 2012 2:27 PM
• Now,

1) please don't write all in bold letters.

2) your path is wrong. It isn't (C:dir\dir\.censuree.cfg) it must be (C:\dir\dir\.censuree.cfg) .

3) This is a code to delete a file:

File.DeleteFile("C:\dir\dir\.censuree.cfg")

4) You must set your destination in "". Otherwise will the program think, that your destination is a variable.

Greetings

Timo

Ich bin Neu

• Proposed as answer by Tuesday, September 18, 2012 2:43 PM
• Marked as answer by Tuesday, September 18, 2012 5:49 PM
Tuesday, September 18, 2012 2:41 PM

By the way, if you have some time to answer me an other question that i forgot to mention.

Since that file (censuree.cfg) is placed at configuration files of that game, directory is not same. it depends on user where he creates a directory for that game. So can you help me with code for searching that file "censuree.cfg" and then terminating it?.

Tuesday, September 18, 2012 5:55 PM
• Now, you can locate the program directory with  "Program.Directory".

Now here are two examples.

1) This one show you the location of the program in a text window.

A = Program.Directory
TextWindow.WriteLine(A)

2) If you want to copy a file to the location of the program should u do the follow:

A = Program.Directory File.CopyFile("File Path of the file which has to copy", A) 'If you want to copy the file in a folder at the program directory: B = Program.Directory + "\YourFolder" File.CopyFile("File Path of the file which has to copy", B)

I hope this is helpful for you.

Greetings

Timo

Ich bin Neu

Tuesday, September 18, 2012 6:25 PM
• Hi there, TheGuxi!

I always use something like -> dir = Program.Directory + "\" when I need to locally save & read files for my programs. Files like images, sounds, etc.

But since you only want , I guess, a "config" file to store your program settings, there's an even easier way!

Use -> confFile = File.GetSettingsFilePath()

What it does is create a full path to a file which has the same name as your program, and also in the same folder, but w/ extension .settings.

Study this code to get what I mean:

dir = Program.Directory + "\"
confFile = File.GetSettingsFilePath()

TextWindow.WriteLine(dir)
TextWindow.WriteLine(confFile)
TextWindow.WriteLine("")

config["Player"]     = "TheGuxi"
config["MaxLives"]   = 5
config["Difficulty"] = "Hard"

File.WriteContents(confFile  config)  '  Save game config to a .settings file

config = ""                           ' Delete config

confCount = Array.GetItemCount (config)
confIndex = Array.GetAllIndices(config)

For index = 1 To confCount
TextWindow.Write    ( confIndex[index] + " = ")
TextWindow.WriteLine( config[ confIndex[index] ] )
EndFor

TextWindow.WriteLine("")

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Tuesday, September 18, 2012 9:04 PM
• Oops!

I think I haven't taken the time to read your request fully, TheGuxi.

If you believe that "censuree.cfg" resides in the same folder as the .exe, then you just need the code below to delete it:

dir  = Program.Directory + "\"
file = "censuree.cfg"
path = dir + file

File.DeleteFile(path)

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Tuesday, September 18, 2012 9:30 PM