Get File when given Server File Location - vb.net

Is it possible to get the actual file, or the file that gets copied from version control to a location?
This sounds confusing. Basically I have the file path of the version controlled file, but I need an actual path to the file because I need to make a cconsole command using powershell.exe. The file will look something like this
$/MyTeamProject/MyProject/Development/MyPowershellScript.ps1
Now, I am looking for a vb expression to see if I can get the actual file and make call the powershell.exe command from console. Any thoughts?

You may use VersionControlServer.GetItem(String path) to obtain a reference to the Item. Then use Item.DownloadFile() or Item.DownloadFile(String localPath) to copy the file locally. I have a variation of this that creates a shipment based on multiple changesets.

Related

How to use the latest file in a folder for source

I have an SSIS package which pulls in a CSV file for processing which pulls one file for the source \\server\dash\LABORDERS.CSV and is working fine.
We wanted to keep older files for historic purposes so everyday there will be new files instead of just overwriting the old one and it looks like this:
I know I am suppose to add a script task but I am not sure where to add it and how to invoke it so that the source file is always looking in the folder for the latest file and using that file to transfer data to it's sql destination.
How can I achieve it?
What have you tried? You can could create a script task at the start of your control flow that uses the .NET framework filesystem objects to search a directory and get the file with the most recent timestamp. You could then assign that file name to a SSIS Variable, then use that variable in your file connection manager.

Method to inspect first 4 bytes and rename file extension

I have a large batch of assorted files, all missing their file extension.
I'm currently using Windows 7 Pro. I am able to "open with" and experiment to determine what application opens these files, and rename manually to suit.
However I would like some method to identify the correct file type (typically PDF, others include JPG, HTML, DOC, XLS and PPT), and batch rename to add the appropriate file extension.
I am able to open some files with notepad and review the first four bytes, which in some cases shows "%PDF".
I figure a small script would be able to inspect these bytes, and rename as appropriate. However not all files give such an easy method. HTML, JPG, DOC etc do not appear to give such an easy identifier.
This Powershell method appears to be close: https://superuser.com/questions/186942/renaming-multiple-file-extensions-based-on-a-condition
Difficulty here is focusing the method to work on file types with no extension; and then what to do with the files that don't have the first four bytes identifier?
Appreciate any help!!
EDIT: Solution using TriD seen here: http://mark0.net/soft-trid-e.html
And recursive method using Powershell to execute TriD here: http://mark0.net/forum/index.php?topic=550.0
You could probably save some time by getting a file utility for Windows (see What is the equivalent to the Linux File command for windows?) and then writing a simple script that maps from file type to extension.
EDIT: Looks like the TriD utility that's mentioned on that page can do what you want out of the box; see the -ae and -ce options)
Use python3.
import os,re
fldrPth = "path/to/folder" # relative to My Documents
os.chdir(fldrPth)
for i in os.listdir():
with open(i,'r') as doc:
st = doc.read(4)
os.rename(i,i+'.'+re.search(r'\w+',st).group())
Hopefully this would work.
I don't have test files to check the code. Take a backup and then run it and let me know if it works.

Vb.net - FileMovement Lock on destination folder

Source code: vb.net
We are using File.Move() method to move the file from source to destination locaion.
But the destination location is being monitored by one tool, whenever we are moving files to the destination location, it will pick up the file and process it. The issue here when we try to move huge volume file like around 5GB file, the tool is immediately picking up the file and try to process it before the move operation is complete and send failure notice to all the users.. After again successfully moving the file completely, it picks up the same and process it sucessfully this time and send successful notice this time.
We can't have control over the tool which is monitoring the destination folder because it is a third party tool. However we want to find out the alternative option to place a lock over the destination foler like ReadWrite access till the move operation completes so that 3rd party will not be able to pick up or try to access that file.
Pls help us.
Not sure if it works, but you might be able to make the following work with directories as well:
FileOpen(1, "c:\file.ext", OpenMode.Binary)
Lock(1)
'Do something with file here
Unlock(1)
FileClose(1)
Reference and example here
I hope it helps.
First, I agree with #hometoast, sometimes tools like this just look for specific file extensions so you can copy in with a different file extension and then rename.
But barring that, download the file to a temp location and then Move the file into the dir getting watched. A Move does not recopy the file contents but just updates its pointers in the filesystem. Should be atomic.

LINQPad script directory?

Does anyone know how to get hold of the path to the directory where the LINQPad script file (.linq) resides?
Or to the script itself for that matter.
Note that I'm not talking about the location of the "My Queries" folder, the one shown inside LINQPad.
I can save a .linq file anywhere on disk, and open it by double-clicking on it.
For instance, if I save the file to C:\Temp\Test.linq, and execute the program, I'd like to have either C:\Temp or C:\Temp\Test.linq.
Basically I'd like something akin to Environment.CurrentDirectory or Assembly.GetEntryAssembly().Location, just for the .linq file.
Things I've tried:
Looking through environment variables
Looking through the LINQPad assembly that is given to my script
Throwing an exception and looking at the stacktrace (contains a link to a temporary copy of my script somewhere else)
Environment.CommandLine.Dump(); - gives LINQPad executable
Environment.CurrentDirectory.Dump(); - gives C:\windows\system32
Assembly.GetExecutingAssembly().Location.Dump(); - gives temp directory
I've just added a feature to address this. You can test it now by downloading the 2.27.1 or 4.27.1 beta build.
To get the current query's folder, use the following expression:
Path.GetDirectoryName (Util.CurrentQueryPath)
I presume you mean programatically and not through the UI itself.
The path to the linq file directory is held in
%APPDATA%\LINQPad\querypath.txt
The next question after "How do I get LinqPad script directory" is "How do I set it?" below is how you do it and link to where I found the answer
Directory.SetCurrentDirectory (Path.GetDirectoryName (Util.CurrentQueryPath));

How do I get the path to the associated file used to launch a program?

I have a program with an associated file type. However, when I double-click a file to launch the program, I can't figure out where that path was passed to the program.
Initially I thought the file path would be in Environment.CommandLine, but the command line only contained the program path.
I specified the file association using the Publish Options in visual studio like so: extension: .ext, Description: Some File, ProgId: ProjectName, Icon: SomeIcon.ico.
You are right, it should indeed be passed as a command line argument and be available through Environment.CommandLine.
My guess is that something is wrong with the file association itself. Here is a dated but still useful TechNet article on how these things work. In particular, the "%1" in the screenshot below is essential.
I'm not sure about the DDE thing, so if you're not using it, turn it off.
As a guess, your file type association may not be configured properly. If you look in the registry at your "Open" command for your file type, the command is probably configured as "c:\your-company\your-app.exe", instead of as "c:\your-company\your-app.exe" "%1", which is what it would need to be if you wanted the file path to show up on the command line.