TOPIC: TYPE
Windows commands for setting default applications for opening certain file types
18th August 2015On Friday, I was working on a system where a session is instantiated from a stored virtual machine that produces a fresh session every time, meaning that all previous changes get lost. What I have is a batch script that I run to reinstate what I need, and I encountered another task that I wanted it to do.
Part of my work involves the creation of plain text files with the extension lst
and this is getting associated with SAS instead of Notepad. While you can reassign such associations using the GUI, it would be a bonus to do it via the command line too, so the assoc
and ftype
commands caught my interest. The first of these associates a file with a given extension with a desired file type, while the second shows the available file types together with the associated applications that open them. The assoc
command also shows all the associations that are in place when it is executed with no parameters, and the ftype
command does the same for file types.
Once you have picked out a file type with the ftype
command, then the assoc
can be used like the following:
assoc .lst=txtfile
The above associates an extension with a file type. In this, .lst
files are going to get opened by Notepad because of the txtfile
association. Though it did not do what I wanted on Friday due to system lockdown, it is good to know that this is possible and that even the Windows command line supports goodies like these.
Creating empty text files and changing file timestamps using Windows Command Prompt & Powershell
17th May 2013Linux and UNIX have the touch command for changing the creation dates and times for files. However, it also creates empty text files for you as well. In fact, there are times when I feel the need to do this sort of thing on Windows too and the following command accomplishes the deed when run in a Command Prompt window:
type nul > command.bat
Essentially, null output is sent to a file that is created anew, command.bat in this case. Then, you can edit it in Notepad (or whatever is your choice of text editor) and add in what you need. This will not work in PowerShell, so you need another command for that:
New-Item command.bat -type file1
This uses the New-Item command, which also can be used to create folders as well if you so desire. Then, the command becomes the following:
New-Item c:\commands -type directory1
Note that file1 in the previous example has become directory1
and there is the -force
option should you need to overwrite what already exists for some reason...
That other use of the UNIX/Linux touch command can be performed from the Command Prompt too, and here is an example command:
copy /b file.txt +,,
The /b switch switches on binary behaviour for the copy command, though that appears to be the default action anyway. The +
operator triggers concatenation and ,,
gets around not having a defined destination because you cannot copy a file over itself. If that were possible, then there would no need for special syntax for changing the date and time for a file.
For doing the same thing with PowerShell, try the following:
(GetChildItem test.txt).LastWriteTime=Get-Date
The GetChildItem
command has aliases of gci
, dir
and ls
and the last two of these give away its essential purpose. Here, it is used to pick out the test.txt file so that its timestamp can be replaced with the current date and time returned by the Get-Date command. The syntax looks a little more complex, even if it achieves the same end. Somehow, that touch command is easier to explain. Are Linux and UNIX that complicated, after all?