Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Passing filename as parameter

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-27-2007   #1 (permalink)
anth
Guest


 

Passing filename as parameter

I have a script which tests if a file/path exists and moves an item if it
does. However whenever my file\path contains a bracket [] commas , etc the
test file path comes up that it doesn't exist even though it does.

How can I overcome this problem? Is there a way of passing the filename as a
parameter?

This is the code I'm working with:

$filename = $args[0]

$destination = "C:\powershell"
[string]$fileexist = Test-Path -Path $filename

If ($fileexist -eq "False")
{
#Sends E-mail to notify me that file doesn't exist
exit
}
Else
{
copy-item $filename -destination $destination
}

Command line I run:
..\test.ps1 "\C:\Done\a509054_MIC_0109[1].jpg"

Any help will be appreciated.
Thanks,
Anthony

My System SpecsSystem Spec
Old 09-27-2007   #2 (permalink)
Jeff
Guest


 

Re: Passing filename as parameter

On Sep 27, 2:15 pm, anth <a...@xxxxxx> wrote:
Quote:

> I have a script which tests if a file/path exists and moves an item if it
> does. However whenever my file\path contains a bracket [] commas , etc the
> test file path comes up that it doesn't exist even though it does.
>
> How can I overcome this problem? Is there a way of passing the filename as a
> parameter?
>
> This is the code I'm working with:
>
> $filename = $args[0]
>
> $destination = "C:\powershell"
> [string]$fileexist = Test-Path -Path $filename
>
> If ($fileexist -eq "False")
> {
> #Sends E-mail to notify me that file doesn't exist
> exit}
>
> Else
> {
> copy-item $filename -destination $destination
>
> }
>
> Command line I run:
> .\test.ps1 "\C:\Done\a509054_MIC_0109[1].jpg"
>
> Any help will be appreciated.
> Thanks,
> Anthony
There are several known issues having to do with wildcard characters
in path names. This is probably the closest to the situation you are
seeing:

https://connect.microsoft.com/feedba...1307&SiteID=99

You can vote to have it fixed in a later version. Until then, use the
LiteralPath parameter of the Test-Path Cmdlet:

[string]$fileexist = Test-Path -LiteralPath $filename

Jeff

My System SpecsSystem Spec
Old 09-28-2007   #3 (permalink)
Kiron
Guest


 

Re: Passing filename as parameter

You can double escape each bracket in the file's path:

..\test.ps1 "\C:\Done\a509054_MIC_0109``[1``].jpg"

--
Kiron
My System SpecsSystem Spec
Old 09-28-2007   #4 (permalink)
Kiron
Guest


 

Re: Passing filename as parameter

You could let the script double escape the brackets:

# -- test.ps1 --
$filename = $args[0] -replace '[\[\]]', '``$0'

$destination = "C:\powershell"
[string]$fileexist = Test-Path -Path $filename

If ($fileexist -eq "False")
{
#Sends E-mail to notify me that file doesn't exist
"absent"
exit
}
Else
{
copy-item $filename -destination $destination
}

--
Kiron
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoke-Expression parameter Passing James PowerShell 4 11-02-2007 03:44 AM
Passing a string array as a parameter from one script to another Brillig PowerShell 5 09-27-2007 04:16 AM
Function Parameter passing wrong or am I ? joergH PowerShell 4 01-23-2007 05:16 PM
Problem with parameter passing? =?Utf-8?B?TWF0dA==?= PowerShell 3 10-06-2006 04:35 PM
Passing parameter from one Page to another Johann MacDonagh Avalon 3 04-10-2006 05:01 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51