Filecopy vs name function? Differences? - vba

I have found two different ways to copy single files in Excel using VBA. One is file copy:
FileCopy (originalPath), (pathToCopyTo)
The other is name:
Name (originalPath) As (pathToCopyTo)
Any differences in performance/intended use/functionality? I haven't been able to notice anything, but interested to know!

They actually do very different things.
FileCopy will actually make a new copy of the file at the location of pathToCopyTo, leaving the original file intact.
Name renames the original file. The effectively moves the file if you supply a different file path. (A copy/paste action.) So, the original file won't be where you found it, it will be in the new location.
For more information see the following MSDN documentation:
Name Statement
FileCopy Statement
As for performance, Name will preform better because it's not necessary for the OS to physically copy the data on disk. It will just update the file's location in the lookup tables. Incidentally, this is also the reason Cut/Paste file operations are faster than Copy/Paste file operations.

Related

pentaho spoon/pid: how to move files to folders with different name everytime?

I have new text files every month from where I extract the data and do some transformations. In the end of every month, I need to move these files to a folder with current date in name. Which means, the destination folder's name is different everytime.
I made a step before move files that creates a folder and its name is current date (exp: 2019-06-01, 2019-07-01), but then on move files step, I don't know how to specify the destination folder. Guess "wildcard" is only used for source...
Otherwise, on move files - Destination file - Move to folder, there is create folder option, thought it could be a solution but as on the screenshot, this section can't be modified. What is the reason?
Does anyone have an idea ?
Thanks for reading :))
You can utilize this approach for this task.
Basically you'll have to extract the date you want from each file before doing the transformations, and after all the transformations are done, you'll use the JOB step 'Move files', but instead of passing a fixed destination, you'll use variables in the path, much like this answer, but you don't need to create the folder before, you can just use the option on the 'Move files' step, so the folders are created at run time.
I have attached an example of this approach with some commentary in the KTR/KJB.
You can download here
Let me know if the link is working, this is my first time posting an external file in StackOverflow

Preserve settings in the file

I am creating my excel add-in that saves the current file as csv into user-specified folder. I would like my program to ask for the folder path the first time the program is launched and to remember that folder in the future. I am wondering is there any way to preserve the data within the program? I figured that I could write the path into .txt-file but that feels a little hack-like solution and would clutter the addin folder.
I use the GetSetting and SaveSetting functions in my VB 6 apps. Rather than cover them in detail, take a look at this excellent web page that illustrates how to use in with Excel
Excel Tips From John Walkenbach
Create a Worksheet, and store the values in cells. Then in the VBA Editor find the Worksheet in the Project Explorer (Ctrl + R) and set "Visible" to "2 - xlSheetVeryHidden" in the Properties Pane (F4) so that it is not readily visible to users.
You can then set/retrieve the data in with code in the format SheetName.Cells(row,column).Value, e.g.
MyPath = Sheet1.Cells(1,2).Value 'Get data from cell B1
Sheet1.Cells(2,2).Value = NewPath 'Set data in cell B2
There are multiple ways to approach this. Besides the hidden sheet approach, already described, you can
Write the information to a CustomXMLPart. This is a xml file stored in the workbook's ZIP file where information can be stored.
For something as short and simple as a file path, you could also use a CustomDocumentProperty

Searching for an excel file using VBA

I want to search and open an excel file named remote.xls
I want to store that excel file into a variable, so i can use it further
But the problem is that I dont know the exact path because for every user, the location is different.
But I know for certain that it is in C:\ drive.
How can I find it using VBA?
I know 2 methods :
1) using dir
2) using application.FileSearch
But the 2nd one is closed now. I am left with the first one..but I am not sure..how to use it
there are several other threads. I have tried those method but have not been successful.
Why different? I want to search with the exact file name and not using wildcards

Change Path to Picture Links in Excel

I have manually pasted a large number of linked pictures into a 2010 Excel spreadsheet using insert picture -> select picture location --> link to file. These pictures are part of a report. I update the pictures using R each quarter, and my report automatically updates. Perfect.
I now need to change the directory where the plots are kept, and need to update the links. As there are around 200 of them (its a big report), I want to do this in VBA. Whilst I can loop through the pictures ok (ActiveSheet.Pictures), I can't seem to find the links/address. Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere (note - these don't seem to be stored as hyperlinks).
Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere
Create a new folder
Paste a copy of the .xlsx or .xlsm excel file
Uncompress the file with a zip tool (i'm using 7-Zip)
Delete the .xlsx or .xlsm file (optional)
Now we have all the component parts of the original file as plain text xml files and folders
Inside the folder xl\drawings\ _rels there are files named as drawing2.xml.rels, drawing3.xml.rels, ...
It seems that each file corresponds to a sheet and stores the paths to images in this format:
Target="file:///C:\Users\myusername\Documents\MyImageFolder\My%20Image%20Name.png"
Change the paths with a text editor
Compress all the contents of the folder to a .zip
Change the extension to the original .xlsx or .xlsm
These steps could be automated with VBA, AutoIt, etc., here some references:
An example with AutoIt and 7-zip
http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp
http://www.jkp-ads.com/Articles/Excel2007FileFormat02.asp
Ron de Bruin zip examples with VBA
Read and change multiple XML files in Excel (2007) VBA
Excel uses the Formula Bar as the link in this case, just the same as it would link between ranges in two different worksheets. When I select a linked picture, the formula below populates in the formula bar:
=[TrialWB.xlsm]Sheet1!$C$3:$E$6
You can access the Shape's formula using the code below and inserting your picture's specific name:
ActiveSheet.Pictures("Picture Name").Formula = "=[TrialWB.xlsm]Sheet1!$C$4:$E$6"
In updating the links, you'll have to change the file path in the formula. This might look like:
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2015\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
changing to
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2016\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
This question may be of some further assistance for accessing formulas:
Excel: create image from cell range
And here's a useful Microsoft page for formula file path editing: https://support.office.com/en-us/article/Create-an-external-reference-link-to-a-cell-range-in-another-workbook-c98d1803-dd75-4668-ac6a-d7cca2a9b95f

assignging text file contents to variable

Stack!
I'm currently trying to code a neat little function in Visual Basic that deletes my entire file system for my program depending on the contents of one text file.
If my text file contains the number 0 by itself, my file system will be deleted. If it contains a 1 by itself, it will not delete the file system and carry on with its pathway of execution.
the problem I'm having is reading the file, then assigning the text file's contents to a variable (string), which then is used in an if statement to decide whether it deletes (then immediately recreates) the file system, or keeps it. Ideally I'd be using this primarily for when the user installs. By default, it is set to 0 so that it clears any stored content, then after creating my directory, the number turns to 1 so that it does not reset my programs content directory.
Here's my code. The first line is giving me difficulties -
Could not find file 'C:\Users\110fa_000\documents\visual studio 2013\Projects\Novation_Launchpad_Emulator\Novation_Launchpad_Emulator\bin\Debug\0'.
fileReader = File.ReadAllText(My.Resources.FirstTimeUse)
If fileReader = "0" Then
FirstTimeUse = True
End If
If you can help me, that'd be absolutely brilliant. I don't think there's any more information I can give sadly, but everything regarding the function, purpose, and error is there.