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

Copying files from the same location as the script

Closed Thread
 
Thread Tools Display Modes
Old 01-10-2008   #1 (permalink)
greatbarrier86
Guest
 
Posts: n/a

Copying files from the same location as the script

Hi,
I've got a script that i'm executing from C:\MININT. Part of that script
copies files using the copy-item command. To execute it, i run powershell
C:\Minint\Script.ps1. However, it errors out when it tries to reference any
certain file because it is still trying to look in the path i executed it
from instead of the path in which the script is located. Is there something i
forgot to type in to tell it to look in the script path?
 
Old 01-10-2008   #2 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: Copying files from the same location as the script



You can cd into that dir and perform the copy:

# Adds the current location to the top of a list of locations ("stack"),
*remember* the current location
pushd $pwd

cd C:\MININT
copy-item file.ext ...

# changes the current location to the location most recently pushed onto
the stack (pushd)
popd



Or append each file name to the script's current directory :


$workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
copy-file $workingDirectory\file.ext ...



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hi,
> I've got a script that i'm executing from C:\MININT. Part of that
> script
> copies files using the copy-item command. To execute it, i run
> powershell
> C:\Minint\Script.ps1. However, it errors out when it tries to
> reference any
> certain file because it is still trying to look in the path i executed
> it
> from instead of the path in which the script is located. Is there
> something i
> forgot to type in to tell it to look in the script path?

 
Old 01-10-2008   #3 (permalink)
greatbarrier86
Guest
 
Posts: n/a

Re: Copying files from the same location as the script

Thanks Shay. The problem with those is if those files are moved to C:\Temp
for instance, the ps file will have to be adjusted to accomodate that. Is
there a way to just have it see those files as the same place as the script?

"Shay Levi" wrote:
Quote:

>
>
> You can cd into that dir and perform the copy:
>
> # Adds the current location to the top of a list of locations ("stack"),
> *remember* the current location
> pushd $pwd
>
> cd C:\MININT
> copy-item file.ext ...
>
> # changes the current location to the location most recently pushed onto
> the stack (pushd)
> popd
>
>
>
> Or append each file name to the script's current directory :
>
>
> $workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
> copy-file $workingDirectory\file.ext ...
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Hi,
> > I've got a script that i'm executing from C:\MININT. Part of that
> > script
> > copies files using the copy-item command. To execute it, i run
> > powershell
> > C:\Minint\Script.ps1. However, it errors out when it tries to
> > reference any
> > certain file because it is still trying to look in the path i executed
> > it
> > from instead of the path in which the script is located. Is there
> > something i
> > forgot to type in to tell it to look in the script path?
>
>
>
 
Old 01-10-2008   #4 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: Copying files from the same location as the script


You can cd into temp and perform the copy.


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thanks Shay. The problem with those is if those files are moved to
> C:\Temp for instance, the ps file will have to be adjusted to
> accomodate that. Is there a way to just have it see those files as the
> same place as the script?
>
> "Shay Levi" wrote:
>
Quote:

>> You can cd into that dir and perform the copy:
>>
>> # Adds the current location to the top of a list of locations
>> ("stack"),
>> *remember* the current location
>> pushd $pwd
>> cd C:\MININT
>> copy-item file.ext ...
>> # changes the current location to the location most recently pushed
>> onto
>> the stack (pushd)
>> popd
>> Or append each file name to the script's current directory :
>>
>> $workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
>> copy-file $workingDirectory\file.ext ...
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hi,
>>> I've got a script that i'm executing from C:\MININT. Part of that
>>> script
>>> copies files using the copy-item command. To execute it, i run
>>> powershell
>>> C:\Minint\Script.ps1. However, it errors out when it tries to
>>> reference any
>>> certain file because it is still trying to look in the path i
>>> executed
>>> it
>>> from instead of the path in which the script is located. Is there
>>> something i
>>> forgot to type in to tell it to look in the script path?

 
Old 01-13-2008   #5 (permalink)
Mark E. Schill
Guest
 
Posts: n/a

Re: Copying files from the same location as the script

You can use this to get the path of the script:

$MyLocation = (Split-Path $myinvocation.MyCommand.path -Parent)

greatbarrier86 wrote:
Quote:

> Thanks Shay. The problem with those is if those files are moved to C:\Temp
> for instance, the ps file will have to be adjusted to accomodate that. Is
> there a way to just have it see those files as the same place as the script?
>
> "Shay Levi" wrote:
>
Quote:

>>
>> You can cd into that dir and perform the copy:
>>
>> # Adds the current location to the top of a list of locations ("stack"),
>> *remember* the current location
>> pushd $pwd
>>
>> cd C:\MININT
>> copy-item file.ext ...
>>
>> # changes the current location to the location most recently pushed onto
>> the stack (pushd)
>> popd
>>
>>
>>
>> Or append each file name to the script's current directory :
>>
>>
>> $workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
>> copy-file $workingDirectory\file.ext ...
>>
>>
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>>
>>
>>
Quote:

>>> Hi,
>>> I've got a script that i'm executing from C:\MININT. Part of that
>>> script
>>> copies files using the copy-item command. To execute it, i run
>>> powershell
>>> C:\Minint\Script.ps1. However, it errors out when it tries to
>>> reference any
>>> certain file because it is still trying to look in the path i executed
>>> it
>>> from instead of the path in which the script is located. Is there
>>> something i
>>> forgot to type in to tell it to look in the script path?
>>
>>
 
Old 01-13-2008   #6 (permalink)
Shay Levi
Guest
 
Posts: n/a

Re: Copying files from the same location as the script


There is no need to include -Parent, it's the default split location parameter




-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> You can use this to get the path of the script:
>
> $MyLocation = (Split-Path $myinvocation.MyCommand.path -Parent)
>
> greatbarrier86 wrote:
>
Quote:

>> Thanks Shay. The problem with those is if those files are moved to
>> C:\Temp for instance, the ps file will have to be adjusted to
>> accomodate that. Is there a way to just have it see those files as
>> the same place as the script?
>>
>> "Shay Levi" wrote:
>>
Quote:

>>> You can cd into that dir and perform the copy:
>>>
>>> # Adds the current location to the top of a list of locations
>>> ("stack"),
>>> *remember* the current location
>>> pushd $pwd
>>> cd C:\MININT
>>> copy-item file.ext ...
>>> # changes the current location to the location most recently pushed
>>> onto
>>> the stack (pushd)
>>> popd
>>> Or append each file name to the script's current directory :
>>>
>>> $workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
>>> copy-file $workingDirectory\file.ext ...
>>>
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>>>> Hi,
>>>> I've got a script that i'm executing from C:\MININT. Part of that
>>>> script
>>>> copies files using the copy-item command. To execute it, i run
>>>> powershell
>>>> C:\Minint\Script.ps1. However, it errors out when it tries to
>>>> reference any
>>>> certain file because it is still trying to look in the path i
>>>> executed
>>>> it
>>>> from instead of the path in which the script is located. Is there
>>>> something i
>>>> forgot to type in to tell it to look in the script path?

 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
script location? Justin Rich PowerShell 5 05-15-2008 11:55 AM
Windows Script Host has no script engine for ".js" files Stephan G. Vista General 9 04-24-2008 03:04 PM
Copying files to Program Files directory Parrot Vista file management 13 08-23-2007 01:14 AM
how to determine a script location? Jeff Jarrell PowerShell 6 06-01-2007 02:56 PM
script location and $pwd Doug PowerShell 4 12-11-2006 02:47 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