mypath and myfilename

by antonela at 2013-03-20 06:06:30

I have a file c:\temp\myfilename.ps1
How can I put the path and the name of file in 2 variables ?
a= "c:\temp"
b="myfilename"

Thanks
by Typeo at 2013-03-20 06:26:19
You basically had it. You just need to add a $ at the beginning.

$Path = ‘C:\temp’
$FName = ‘Myfilename.ps1’
by antonela at 2013-03-21 01:49:13
sorry, I don’t want to set the 2 variables.
I want something that returns the value of file name and the path.

I want to launch this command:
powershell.exe c:\temp\myfilename.ps1
and I want to write a file log c:\temp\myfilename-(yy.mm.dd).log

Is it a way to do this?To put in a variable the name of file I have as an argument?
by jonhtyler at 2013-03-21 03:56:32
$filename = split-path -path c:\temp\myfilename.ps1 -leaf
$newFileName = "$($filename.substring(0, $filename.LastIndexOf(‘.’)))-($(get-date -format yy.MM.dd)).log"
by antonela at 2013-03-21 04:39:46
Thanks, but I don’t want to write c:\temp\myfilename.ps1 in any way in the split-path
I want to have $filename without writing the name of file, but using a function that returns the name of my file.

It’s important for me because I want to create more cmd:
powershell myfilename.ps1
powershell myfilename2.ps1
powershell myfilename3.ps1

and have automatically myfilename-(yy.mm.dd).log , myfilename2-(yy.mm.dd).log , myfilename3-(yy.mm.dd).log and so on…
I thought that if I was able to put in a variable the name of my file, I would came out…

In every file .ps1 I have an sqlcmd -s …>>$filelog
by jonhtyler at 2013-03-21 04:45:13
So replace "c:\temp\myfilename.ps1" in the split-path cmdlet with a variable of your choice that has the fully-qualified path that you want to process.
by poshoholic at 2013-03-21 04:57:00
Sounds like you are trying to get the path of the file from within the script file itself. Here’s an example showing how to do that:

Assume you have a file with the following contents:
$path = $MyInvocation.MyCommand.Path
$logFileName = $(Split-Path -Path $Path -Leaf) -replace '.ps1$',"-$(Get-Date -Format 'yy.MM.dd').log"
Write-Host "Now we can create a log with a filename of $logFileName"

Whenever you invoke this, whether dot-sourced, called with the call operator, or invoked using the full path, you’ll see that no matter where the file is or what it is named, you’ll get a log file using the same name of the file in the format you requested.
by antonela at 2013-03-21 07:24:45
Thank you very much
$path = $MyInvocation.MyCommand.Path
is what I needed.
Thanks again!