by antonela at 2013-03-20 06:06:30
I have a file c:\temp\myfilename.ps1by Typeo at 2013-03-20 06:26:19
How can I put the path and the name of file in 2 variables ?
a= "c:\temp"
b="myfilename"
Thanks
You basically had it. You just need to add a $ at the beginning.by antonela at 2013-03-21 01:49:13
$Path = ‘C:\temp’
$FName = ‘Myfilename.ps1’
sorry, I don’t want to set the 2 variables.by jonhtyler at 2013-03-21 03:56:32
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?
$filename = split-path -path c:\temp\myfilename.ps1 -leafby antonela at 2013-03-21 04:39:46
$newFileName = "$($filename.substring(0, $filename.LastIndexOf(‘.’)))-($(get-date -format yy.MM.dd)
).log"
Thanks, but I don’t want to write c:\temp\myfilename.ps1 in any way in the split-pathby jonhtyler at 2013-03-21 04:45:13
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
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:by antonela at 2013-03-21 07:24:45
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.
Thank you very much
$path = $MyInvocation.MyCommand.Path
is what I needed.
Thanks again!