Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I don't like Microsoft's built in Notepad at all. I prefer to use notepad++, but since it's not built in it's not easy to use from a command line. I am writing a powershell script that can help out, here's what I have so far:

Function Read ([string] $file)
{
$currentPath = (get-location).Path
cd "C:\program Files (x86)\Notepad++\"
.\notepad++.exe $file
cd $currentPath
}

The problem is, I have to type the complete path every time. Most of the time I am at the location I want to read the file, so it would be nice to be able to call the function like:

read .\docu.txt

instead of: read C:\users\user\desktop\docu.txt I was thinking something like:

$fullPath = '$currentPath\$file'
.\notepad++.exe $fullPath

but then if I ever do type out the whole path it won't work anymore, and that doesn't work anyway because that would look like "C:\users\user\documents.\docu.txt" Also that doesn't handle UNC paths. When I select get-location from there it looks like

Microsoft.Powershell.Core\FileSystem::\\server\drive

and I can't get it to work.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

This would be a great time to make use of Regex. There's a few issues here though, so let's work through them top to bottom. First, you need to be able to handle network paths. Do this by adding the following to the beginning of your script to eliminate all that extra crap in the path

$UNCPath = $pwd.ProviderPath

That will give you just the \\network\drive.

Now, to handle the different ways you want to be able to send information in the $file parameter, use regular expressions to inspect what you've sent in.

$first = $file -match "\.\\" #does file contain .\ ?
$second = $file -match "\\"  #does file contain \ ?
if($first -eq $true)
{ #for instances when you sent in .\filename.txt
$subFile = $file.Substring(2) #Eliminate the .\ from the path
$completeFile = "$UNCPath\$subFile"
.\notepad++.exe $completedFile
}
else if($second -eq $true) #Contains \ but not .\ so you've sent the complete path
{
.\notepad++.exe $file   #easy when the full path is already sent
}
else
{  #You sent just the name of the document
$pathfile = $UNCPath + "\" + $file   #we assume if you just sent the name, the file is 
.\notepad++.exe $pathfile            #located at your current location
}
cd $currentPath
}
share|improve this answer

I think you're making this way harder than it needs to be. :)

1) Notepad++ will load a file based on the argument whether it has a path or not, based on the standard Windows path system.

So Notepad++ file.txt will open file.txt if it's in the local folder, and Notepad++ c:\path\file.txt will open C:\path\file.txt, regardless of what folder you are in.

2) Just add your Notepad++ folder to the Path of the system, and then you can just type "notepad++ c:\path\file.txt` at the command prompt.

If you want that shorter, create a batch file in the Notepad++ folder, perhaps named read.bat, and its contents would simply be something like:

Notepad++.exe %1 %2 %3 %4 %5

Doing that should allow you to run read file.txt and/or read c:\pathto\file.txt.

share|improve this answer
    
Your answer is good knowledge and it does solve the problem in the question, so +1 to you. However, I specifically wanted to do this task with a script, if for no other reason than it's the most fun solution, even if not the easiest –  Jacob Smart Aug 14 '13 at 15:51
    
A batch file is a script. ;) –  Ƭᴇcʜιᴇ007 Aug 14 '13 at 16:17
    
Yes of course; I was referring to adding the notepad++ folder to the path when I spoke of a script solution. I learned scripts writing batch files but I really want to learn Powershell scripting so I'm trying to do everything with it until I am a master. Like I said, your way is more efficient, but I was looking for a way to do it all from within Powershell. Guess I could have made that more clear :D –  Jacob Smart Aug 16 '13 at 17:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.