Realbasic: How to create a file in a specific folder? - realbasic

I think my question is quite simple, but I can't find a solution.
I want to create a file in a folder that's located near the application file (MacOS).
To create a new folder:
dim fi as FolderItem = getFolderItem( "MyFolder" )
fi.CreateAsFolder
And now I need to get a FolderItem for the file inside this folder. How can I do it?

This will get you pretty close to what you want.
dim fi as FolderItem = getFolderItem( "MyFolder" )
if fi.exists = false then
fi.CreateAsFolder
end
dim fChild as folderitem = fi.child("someFile")
if fChild.exists = false then
//Do something like create it.
//Look at TextOutputStream or similar
else
//Already exists. Open it?
end

Related

How to write a function to combine folder path and file name?

I would like to pass the full path of a text file to one of the function.
i am placing the my script, and text file at same location
by using the below command i found the folder path where my script is
p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
p came as C:\test
my file name is xyz.txt
i want to pass the the argument to the function as C:\test\xyz.txt
how can i combine path and file name
i tried below code
path = p & "xyz.txt"
can any one help me how can join the path and file name.
You can use string concatenation to build a path. The correct way to do it, however, is to use the FileSystemObject's BuildPath() method, because this will do the right thing with the backslashes under all circumstances.
Set FSO = CreateObject("Scripting.FileSystemObject")
scriptPath = FSO.GetParentFolderName(WScript.ScriptFullName)
textFilePath = FSO.BuildPath(scriptPath, "xyz.txt")
MsgBox textFilePath
Try like this code :
Option Explicit
Msgbox GetFilePath("xyz.txt")
'******************************************************
Function GetFilePath(FileName)
Dim fso,scriptPath
Set fso = CreateObject("Scripting.FileSystemObject")
scriptPath = FSO.GetParentFolderName(WScript.ScriptFullName)
GetFilePath = FSO.BuildPath(scriptPath,FileName)
End Function
'******************************************************

unzip file silently vbscript

I found online script that basically unzip every .zip archive in a given path.
sub UnzipAll(path)
set folder = fso.GetFolder(path)
for each file in folder.files
if (fso.GetExtensionName(file.path)) = "zip" then
set objShell = CreateObject("Shell.Application")
objshell.NameSpace(path).CopyHere objshell.NameSpace(file.path).Items
file.delete
end if
next
end sub
This is actually working, but the problem is that I want to unzip "silently" (silently means that I don't want any kind of message from the system when unzipping, like "do you want to overwrite?" ect.).
I've searched a lot on google and I found that you just need to add a few flags on the "CopyHere" method, like this:
objshell.NameSpace(path).CopyHere objshell.NameSpace(file.path).Items, *FLAGHERE*
But the problem is right here. The flags would normally work, but they are completely ignored when unzipping a .zip archive.
So I searched for a workaround, but I didn't find anything helpful.
I managed to do it by myself. Basically you want to unzip 1 file per time and not everyone togheter, and before copying it you just check if it already exists, and evenutally delete it:
set fso = CreateObject("Scripting.FileSystemObject")
sub estrai(percorso)
set cartella = fso.GetFolder(percorso)
for each file in cartella.files
if fso.GetExtensionName(file.path) = "zip" then
set objShell = CreateObject("Shell.Application")
set destinazione = objShell.NameSpace(percorso)
set zip_content = objShell.NameSpace(file.path).Items
for i = 0 to zip_content.count-1
'msgbox fso.Buildpath(percorso,zip_content.item(i).name)+"."+fso.getExtensionName(zip_content.item(i).path)
if (fso.FileExists(fso.Buildpath(percorso,zip_content.item(i).name)+"."+fso.getExtensionName(zip_content.item(i).path))) then
'msgbox "il file esiste, ora lo cancello"
fso.DeleteFile(fso.Buildpath(percorso,zip_content.item(i).name)+"."+fso.getExtensionName(zip_content.item(i).path))
end if
destinazione.copyHere(zip_content.item(i))
next
file.Delete
end if
next
'for each sottocartella in cartella.subfolders
' call estrai(folder.path)
'next
end sub
call estrai("C:\Documents and Settings\Mattia\Desktop\prova")

If folder exist create shortcut this folder in MyDocuments VBS

I want to create a shortcut in my documents only if will be existed a network share.
I'm trying to solve for a long time, but i still have problem with this.
Any help or suggestions would be greatly appreciated.
Dim strSkanSou
Dim objMyDocuments
strSkanSou = "\\Network\Scan\%username%"
IF strSkanSou.FolderExists then
Set objShell = CreateObject("WScript.Shell")
objMyDocuments = objShell.SpecialFolders("MyDocuments")
Set objLink = objShell.CreateShortcut(objMyDocuments & "\Skaner.lnk")
objLink.Description = "Skaner"
objLink.TargetPath = strSkanSou
objLink.Save
End If
You've got most of it solved already. You just need to create a FileSystemObject to check for the existence of your folder. Replace:
IF strSkanSou.FolderExists then
With:
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strSkanSou) Then
Also, I see you're using the prefixes str for string and obj for object, which is great, but you may want to use strMyDocuments instead of objMyDocuments, since this is actually a string and not an object.

Replacing SubString values with the Replace function

The code below looks in the test folder for any files that have not been accessed in over 5 days, if it finds one it assigns mRoot the file path and then whats NOT WORKING is using the Replace method to look inside the mRoot string for the IP and replace it with the new one, I have it show me what mRoot looks like in a pop up just to make sure it changes(or doesn't). I can't seem to get the IP to change. Can anyone help out? I'm very new to VBS so I'm hoping this is obvious (whether it is doable or not). Thanks.
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = "\\192.168.1.104\test\"
today = Date
Set aFolder = oFileSys.GetFolder(sRoot)
Set aFiles = aFolder.Files
For Each file in aFiles
FileAccessed = FormatDateTime(file.DateLastAccessed, "2")
If DateDiff("d", FileAccessed, today) > 5 Then
Set objShell = Wscript.CreateObject("Wscript.Shell")
mRoot = file
Call Replace(mRoot,"\\192.168.1.104","\\192.168.1.105")
objShell.Popup mRoot,, "My Popup Dialogue box"
'oFileSys.MoveFile file, mRoot
End If
Next
Try mRoot = Replace(mRoot,"\\192.168.1.104","\\192.168.1.105")

create a folder in a grandparent dir vb.net

I want to create a dir (named with a varible Utilities._Name)located two levels from the exe file,
My exe file is in C:\SGEA\SGEA\bin
How can I do it so I get C:\SGEA\theNewDir without using full path, just relative paths?
If Not System.IO.Directory.Exists(Utilities._Name) Then
System.IO.Directory.CreateDirectory(Utilities._Name)
Else
MessageBox.Show("There is already a dir named: " & Utilities._Name, "SGEA", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
If you have the path to the exe file, you can use the System.IO.Path Class to navigate easily:
Dim folder = Path.GetDirectoryName(theExeFile)
Dim grandparent = Path.GetDirectoryName(Path.GetDirectoryName(folder)) ' Up two directories
Dim newFolder = Path.Combine(grandparent, "theNewDir") ' Use this to create the new folder name cleanly
Utilities._Name = newFolder

Resources