soMuch2Learn
soMuch2Learn

Reputation: 187

Trouble escaping my own string, date variable in file name

Im having a bit of a bump in trying to grasp quotes, single and double within powershell.

I have a file that is auto generated each day, named filecsv20140306.csv, where 20140306 is the date. I want to run a script that takes this file each day, and processes it.

I can do the following..

$filename = 'filecsv' + (get-date -uformat %Y%m%d) + '.csv'
$file = import-csv \\fs-01\daily\$filename

Then when I read that file, it is as it should be.

How would I get this into one line? I tried..

$file = import-csv \\fs-01\daily\filecsv + (get-date -uformat %Y%m%d) + csv ..

whether I put single quotes, no quotes i seem to get an error stating cannot bind parameter, cannot convert valie "20140306" to type "System.Char"...

Upvotes: 0

Views: 41

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36342

You can always just use a ; instead of a new line like such:

$filename = 'filecsv' + (get-date -uformat %Y%m%d) + '.csv';$file = import-csv \\fs-01\daily\$filename

But I think what you wantis going to be:

$file = import-csv "\\fs-01\daily\filecsv$(get-date -uformat %Y%m%d).csv"

When in the double quotes anything in a $(<code>) enclosure will be processed before returned as a part of the string, so as far as Import-CSV is concerned this:

"\\fs-01\daily\filecsv$(get-date -uformat %Y%m%d).csv"

looks like this:

"\\fs-01\daily\filecsv20140306.csv"

Upvotes: 1

Related Questions