Very basic thing I am trying to accomplish.
A have a source of a file (image) stored as string (simple path).
I want to copy that file to custom destination. to be more precise to a folder name image which is located in the application root. I have checked documentation, and all of them refer to FolderItem class, which unfortunately, I cant figure out.
Any Ideas?
The FolderItem class has a built-in FileCopy method and I'd recommend learn FolderItem because it makes file handling so much easier in the long run because it's really the only way to do it in Xojo/Real Studio.
Generally the folderitem class is initialized by using the GetFolderItem method:
dim f as folderitem = GetFolderItem("somefile.pdf")
This basic function looks for the pdf file in the same directory as the executable. If the file is somewhere else you can use the Absolute Path like "C:/SomeFolder/somefile.pdf".
There are some proscribed locations that are meant to be accessed a lot (Application Data, Preferences, etc) and the easiest way to get to them is use the SpecialFolders object. If your files was in the Application Data folder you would access it:
dim f as folderitem = SpecialFolder.ApplicationData.child("somefile.pdf")
SpecialFolder.ApplicationData returns a folderitem and child looks for the file. Folderitem child and parent methods are very important to learn.
There are many examples of how to use GetFolderItem at https://docs.xojo.com/index.php/GetFolderItem
SpecialFolder explained at https://docs.xojo.com/index.php/SpecialFolder
If you want video training, subscribers can get over 40 hours of Real Studio and Xojo training at http://www.bkeeney.com/XojoTraining/xojotraining.cgi
If you are not used to object-oriented syntax, just think of FolderItem as the thing where the copy command is. So below, we make two FolderItem objects: one for the source file, and one for the destination folder. Once that is done, we can use the CopyFileTo() method of FolderItem to copy the file:
dim s as String
dim source as FolderItem
dim dest as FolderItem
s="C:\test.jpg"
source=GetFolderItem(s)
dest=GetFolderItem("C:\image")
source.CopyFileTo(dest)
The FolderItem class can represent any file or folder on the machine. To create a FolderItem instance for a particular absolute path, pass the path to the GetFolderItem method and store the result:
Dim SourceFile As FolderItem
SourceFile = GetFolderItem("C:\ExampleFolder\ExampleFile.txt", PathTypeAbsolute)
Once you've constructed the FolderItem you can modify its properties and call its methods to affect changes to the underlying file or directory.
To copy or move a file to another directory, you need to acquire a FolderItem representing the destination directory. Depending on the destination, you can use one of several methods to acquire the destination FolderItem.
For example,
Dim destination As FolderItem
destination = GetFolderItem("C:\DestinationExample\", PathTypeAbsolute)
or, using the SpecialFolder module:
destination = SpecialFolder.Desktop 'the user's desktop directory
or, using the parent folder of the ExecutableFile property of the App class:
destination = App.ExecutableFile.Parent 'your app's directory
Once you have both the source and destination FolderItems set up, simply call the CopyFileTo or MoveFileTo methods of the source FolderItem:
Dim SourceFile As FolderItem
SourceFile = GetFolderItem("C:\ExampleFolder\ExampleFile.txt", PathTypeAbsolute)
Dim destination As FolderItem
destination = GetFolderItem("C:\DestinationExample\", PathTypeAbsolute)
SourceFile.MoveFileTo(destination)
' or
SourceFile.CopyFileTo(destination)
Note that the CopyFileTo and MoveFileTo methods can't be used to move or copy directories, only files.
Folderitems are a way to represent a path, independently of the OS particulars. It is important if you plan on creating apps for Windows and Mac or Linux, for instance.
In Windows, a typical path is expressed as
C:\Users\MitchMatch\Desktop\myPicture.png
In Mac OS X or Linux, the same path will be :
C:/Users/MitchMatch/Desktop/myPicture.png
FolderItem also provides ways to directly access the desktop :
Dim f as folderItem = SpecialFolder.Desktop.Child("myPicture.png")
To copy a file, you can use Xojo built in FolderItem.CopyFileTo method, or shell to the system, and use a command line.
On Windows for instance, you can use
Dim s as new shell
s.execute("Copy c:\Users\MitchMatch\Desktop\myPicture.png c:\Users\MitchMatch\Pictures")
On Mac OS X and Linux, the command is CP. Note that contrary to the Xojo CopyFileTo command, the system function is able to copy an entire directory.
Related
fso.GetSpecialFolder(2) correctly returns the temp folder for me, when run from a VBS file. However, when run from within an Autodesk Revit journal file, which has historically been VBS compliant, I get back the proper path and a GUID after Temp. I had never seen this before, and I am unsure if this is perhaps a known issue in newer builds of Windows 10 (it's been about three years since I tested this), or is this more likely an issue with Autodesk's implementation of VBScript support? I suspect the latter.
Which then raises the question, is there another good way to get the full temp path in VBScript?
I could use
Dim strUser : strUser = CreateObject("WScript.Network").UserName
"C:\Users\" & strUser & "\AppData\Local\Temp"
It's just been so long since I played with VBS, I'm not remembering if there is a better answer, or this is the consistently workable way to go. And, more than anything I want to know if .GetSpecialFolder(2) is broken in some way in Windows, or just by Autodesk.
Isn't it logical to have different values on different environments / scripting hosts?
GetSpecialFolder(2) simply returns the process environment variable named TMP. Any change on that variable -which is quite legal-, affects the value that GetSpecialFolder(2) returns.
GetSpecialFolder method
Constant: TemporaryFolder
Value: 2
The Temp folder is used to store temporary files. Its path is found in the TMP environment variable.
Since GetSpecialFolder(2) will always return an existing directory path I probably would use it with the thought that this is intended by the environment; the Autodesk Revit.
Other than that, I'd use something like below if I want the usual temporary path because even they are rare, there are installations where system drive is not C:. Relying on %localappdata% makes more sense in that manner.
Set WshShell = CreateObject("Wscript.Shell")
TempPath = WshShell.ExpandEnvironmentStrings("%localappdata%\Temp")
Could someone please help me create a path for an image that I am displaying in the image box? This tool is intended for use of others. Therefore, they will have to download it in their computers and the path to the image will change. I have tried several ways but nothing seems to work. Here is the code:
Me.cmdImage2.Picture = LoadPicture("G:\Pedestrian Bridges\New folder with organization\Visual Basic Tool\Tool\New folder\TRB paper Version\Imagenes Finales\Alternative 2.JPG")
Thanks a lot!!!!!!!!!!
Try thisworkbook.path or activeworkbook.path it will be saved the image in same folder
This is a good question and something I've been recently working on. I will show you a few lines of my code that would be beneficial for you to use as well.
The first potential method you could use would be to implement the IO class's ability to return the path your application is being run from. You can do that by following this example:
MessageBox.Show(IO.Path.Combine(IO.Directory.GetParent(Application.ExecutablePath).FullName, "myimage.jpg"))
Another potential solution would be to use environmental variables.
In order to do this you will need to import the IO class:
using System.IO;
Then you can reference environmental variables like this:
DirectoryInfo dst = new DirectoryInfo(Environment.GetEnvironmentVariable("UserProfile") + #"\Desktop\!#!Desktop_Cleanup\" + s + #"_Desktop\" + category);
Sorters.CopyFiles(src, dst, true, "*."+ extension);
In my example, I'm using the UserProfile enviromental variable that is stored in Windows. Then, knowing where that path leads I am able to add a path to a file system on my desktop that I use for cleaning the desktop. Keep in mind you will need to use the # symbol before strings containing backslashes so they will be read properly and not as escape characters.
Sorters is a class that I made and won't be applicable in your application, but I included that line so you could see a possible implementation of the DirectoryInfo you create.
Hope this helps!
Using this link I was able to write a program in vba that reads extended file properties. Now, I'd like to make a program that can edit extended file properties - specifically property 22, the "subject" of a file. So, given a file path, how could you edit the subject associated with that file?
It can't be done using the method you are using now. You can install and use the Microsoft ActiveX dsofile.dll to both get and set extended properties using VBScript.
Set objFile = CreateObject("DSOFile.OleDocumentProperties")
objFile.Open("C:\My Path\MyFile.doc")
objFile.SummaryProperties.Subject = "My Subject"
objFile.Save
set objFile = Nothing
This is really more of a comment to jac above. The .dll file referenced won't work on 64 bit machines, and I feel most machines today are 64 bit. Click Here for an open source 64 bit equivalent to the referenced dsofile.dll.
' Make the file Read-Only
SetAttr "c:\temp\Sample.txt", vbReadOnly
' Make the file Hidden
SetAttr "c:\temp\Sample.txt", vbHidden
' Please note that if you change one attribute, the existing attribute is overwritten. For making a file as both readonly and hidden use both attributes in the function
SetAttr "c:\temp\Sample.txt", vbHidden + vbReadOnly
' Remove all atributes - convert a read-only file to read-write file, unhide the file etc
SetAttr "c:\temp\Sample.txt", vbNormal
Please help me to acheive the following using VBScript
1.Messagebox with three tabs Copy,Update,Cancel and displaying "Welcome to the AVG
definition fies copier/updater module.Click on Copy to copy files or Update to update
definition files.
2.If copy selected,the drive letter from where the script is run(usb drive) stored as
variable,directory "(usb drive)Update" created if not exist,new and files not existing
in update folder copied to(eg=xcopy /d), from
"%allusersprofile%\applic~1\avg8\update\download"
3.If possible display message 'copying files, while copying.After completion of
copying display 'Files copied successfully'.
4.If update selected,tdirectory "c:\Update" created if not exist,new and files not
existing in "c:\Update" copied to, from (usb drive) update folder
5.If possible display message 'Updating files' while copying.After completion of
updating, display 'Files Updated successfully'.After clicking OK exit and start
"C:\progra~1\avg\avg8\avgui.exe"
Well, the way that I would do it is to make stand alone functions for each of the functional tasks that you have then wrap those functions inside an HTA to give you the interface layer that you want.
As I understand from your other question, you managed to find solutions to most of these tasks yourself. Here's a tip for your #2, which I haven't noticed implemented in that your script.
2.If copy selected,the drive letter from where the script is run(usb drive) stored as variable
You can retrieve the full path of the current script file using the WScript.ScriptFullName property and then use the FileSystemObject.GetDriveName method to extract the drive letter:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strUSBDrive = objFSO.GetDriveName(WScript.ScriptFullName)
This will give you the drive letter followed by a colon (e.g. J:). You can then concatenate this value with the target folder name to get the full path, e.g.:
MsgBox strUSBDrive & "\Update"
I am busy writing an application that runs under windows
Where is the correct place to save temporary files ?
If you are using .NET, please use Path.GetTempPath(). This will guarantee that you use the temporary directory assigned to the user that runs your application, regardless of where it is stored.
If you browse the file system, you will notice that there are many "temp" directories:
~\Temp
~\Windows\Temp
~\Users\userName\AppData\Local\Temp
... and many more. Some of these paths are OS-dependent, and won't be present on certain windows flavors. So, save yourself some time and hassle, and let the .NET framework figure out where the "temp" path is located.
Use GetTempPath and and possibly GetTempFileName to determine where to put your temporary files. This is the most reliable, enduser-friendly, and future proof way to get a temporary location for files.
In the temp directory?
Use GetTempPath or in a batch file %TEMP%
C:\Temp is NOT a good choice.
If you are using .Net use code like this:
string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string cfgFolder = Path.Combine(baseFolder, "MyAppName");
try
{
if (!Directory.Exists(cfgFolder))
{
Directory.CreateDirectory(cfgFolder);
}
}
catch { } // If no access, not much we can do.
to get a place for medium-term storage of app data, or Path.GetTempPath() for transient storage of data.
Use the GetTempPath API, or the equivalent for your programming environment.
It depends on the language you are using:
string tempFolder = System.IO.Path.GetTempPath();
will return you the appropriate folder in C# for instance.
or, the environment variables TEMP or TMP if you must.
C:\Documents and Settings\username\Application Data\IsolatedStorage