• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PowerShell: Concatenation and \

oynaz

Platinum Member
SOLVED - see #3
Hi guys,

I am writing a PS script, and I want it to create a log file. The user selects a source folder during the script, and I want to get the date, call the log file date.txt and put it in the source folder.
I can get PS to concatenate my variables, but that results in a c:\sourcefolderdate.txt file.
I want a c:\sourcefolde\date.txt file instead.
I have tried adding a +="\" to my concatenation, but that does not work.

Any ideas?

Code:
#This function asks for the location of the build folder
function buildfolderloc {
	$app = new-object -com Shell.Application
	$script:buildfolder = $app.BrowseForFolder(0, "Select Folder", 0, "C")
	$script:buildfolder = $buildfolder.Self.Path.ToString()
}

#This function creates a log file form a parsed date, and stores the name in a variable.
function createlogfile {
#Get date
$script:date=Get-Date
$script:date=$date.ToString("M-d-yyyy")
#Create log file based on date and build folder location
$logfilename = $buildfolder +="\" +=$date +=".txt"
New-Item $logfilename -type file


}

buildfolderloc
createlogfile
 
Last edited:
I don't think you need the +=

Code:
PS I: \> $blah = "C:" + "\"
PS I: \> echo $blah
C: \
PS I: \>

--edit--

Also it is much easier to use variable subbed strings to do what you are doing:

Code:
PS I: \> $buildfolder="C:"
PS I: \> $date="2013-04-23"
PS I: \> $blah='$buildfolder\$date'
PS I: \> echo $blah
$buildfolder\$date
PS I: \> $blah="$buildfolder\$date"
PS I: \> echo $blah
C: \2013-04-23                             [anti smily space c: \ here]
PS I: \>

Note the difference there. Powershell will sub variables in strings using double quotes. Singles are "absolute."
 
Last edited:
Thanks. You let me on the right track. This works:

Code:
#This function asks for the location of the build folder
function buildfolderloc {
	$app = new-object -com Shell.Application
	$script:buildfolder = $app.BrowseForFolder(0, "Select Folder", 0, "C")
	$script:buildfolder = $buildfolder.Self.Path.ToString()
}

#This function creates a log file form a parsed date, and stores the name in a variable.
function createlogfile {
#Get date
$script:date=Get-Date
$script:date=$date.ToString("M-d-yyyy")
$logfilename = "$buildfolder" +"\" +"$date" +".txt"
Write-Host $logfilename
New-Item $logfilename -type file
#Create log file based on date and build folder location

}

buildfolderloc
createlogfile
 
Thanks. You let me on the right track. This works:

Code:
#This function asks for the location of the build folder
function buildfolderloc {
	$app = new-object -com Shell.Application
	$script:buildfolder = $app.BrowseForFolder(0, "Select Folder", 0, "C")
	$script:buildfolder = $buildfolder.Self.Path.ToString()
}

#This function creates a log file form a parsed date, and stores the name in a variable.
function createlogfile {
#Get date
$script:date=Get-Date
$script:date=$date.ToString("M-d-yyyy")
$logfilename = "$buildfolder" +"\" +"$date" +".txt"
Write-Host $logfilename
New-Item $logfilename -type file
#Create log file based on date and build folder location

}

buildfolderloc
createlogfile

Also:

$logfilename = "$buildfolder\$date.txt"

Should work also.
 
Back
Top