powershell에서`pushd`dd 스택의 마지막 디렉토리에 복사하는 방법이 있습니까?


3

Windows PowerShell (및 CMD 및 bash) pushd에서는 마지막 디렉토리에 복사하면 좋을 것 입니다. 예 :

> pwd
Path
----
D:\Some insanely long\path I really\ don/'t want to type\because it's hard\vimstuff\
> pushd ..\..\..\..\thing that\lives in the swamp
> cp *.pu $popd

여기서 $ popd는 마지막으로 밀린 디렉토리입니다. 이 기능이 존재합니까, 아니면 스크립트를 작성해야합니까?


편집 : 모두가 솔루션에 가까워 지지만 아직 거기에 도달하지 못하는 데 도움이되는 팁에 답변하는 것처럼 보입니다. powershell에서는 불가능할 수 있습니다. cmd를 위해 작성했지만 powershell에서는 작동하지 않는 다음과 같은 것을 찾고있었습니다.

CPP.BAT :

echo off
if "%olddirp%"=="" (
  echo olddirp not defined, use cdp to push directory before using cpp
) else (
  for %%A in ("" "help" "-help" "/help" "-h" "/h") do (
    if "%1"==%%A goto help
  )
)
copy %1 %olddirp%
echo .\%1 copied to %olddirp%\%1
goto end
:help
echo "cdp / cpp usage: cdp to directory 'cpp c:\newdir' then cpp files to previous directory 'cpp somefile'"
:end

CDP.BAT :

set olddirp=%cd%
cd %1

그것들을 쉽게 번역 할 수 있습니까? 분명히 더가 없기 때문에 나는 문제가 있었다 %cd%거나 %path%또는 PowerShell의 다른 간단한 변수.

답변:


1

오래된 박쥐 파일이 한 일을해야합니다. your.name.ps1로 저장하고 powershell을 관리자로 시작하고 "Set-ExecutionPolicy RemoteSigned"를 실행하여 PowerShell 스크립트 실행을 활성화하십시오.

<#
.SYNOPSIS
Push a folder to stack with "pushd" then call this script with files/filepattern as arguments

.EXAMPLE
script.ps1 *.pu,*.txt
Copies pu files and txt files to last folder pushed to stack.
#>

Param(
  [Parameter(Mandatory=$False, Position=0)]
   [String[]]$files,

   [alias("h")]
   [switch]$help
)

if($help -or !$files)
{
    (Get-Help $MyInvocation.MyCommand.Definition -full)
    exit(0);
}

$CopyToFolder = $null;

try
{
    $CopyToFolder = (get-location -stack).peek();
}
catch
{
    write-host "Stack is empty, use pushd before $($MyInvocation.MyCommand)";
    exit(1);
}


foreach($f in $files)
{
    (copy $f $CopyToFolder);
    write-host ".\$files copied to $CopyToFolder\$files";
}

3

powershell에서는 push-location(예를 들어 pushd) 사용할 때 나중에 검색 할 수있는 스택에 위치를 저장합니다 get-location -Stack. 따라서 귀하의 예는 다음과 같습니다

> pushd ..\..\..\..\thing that\lives in the swamp
> cp *.pu (get-location -stack)

스택에 여러 위치가있을 수 있습니다.이 경우 사용해야합니다cp *.pu (get-location -stack).peek()
Rynant

질문 편집에서 위에 게시 한 것처럼 배치 파일로 어떻게 만들 수 있습니까? 배치 파일 및 변수와 동등한 Powershell은 무엇입니까? 나는 %cd%일하는 것처럼 간단한 것을 얻을 수 없습니다 .
Still.Tony

1

경로를 변수에 저장하려고 시도 할 수 있습니다 (예에서는 스크립트가 실행 된 경로를 사용한다고 가정합니다). 필요에 따라 사용하십시오.

$popd = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

Set-Location -Path "C:\windows\system32"

Write-Host "Your current location: $(Get-Location)"

Write-Host "Your previous location: $popd"

Set-Location -Path $popd

Write-Host "We're back to: $(Get-Location)"

먼저 스크립트가 호출 된 위치부터 $popd변수 까지의 경로를 저장합니다 . 그런 다음 디렉토리를 변경 c:\windows\system32하고 화면에 해당 경로와 원래 경로 (에 저장된 경로 $popd)를 표시 한 다음 해당 변수를 사용하여 시작 폴더로 다시 변경합니다.

이 TechNet 기사 에서 $ MyInvocation과 같은 자동 변수에 대해 자세히 알아볼 수 있습니다 .

또한 Andy Arismendi는 PowerShell에서 스택에 액세스하는 방법을 설명 하는 답변제공했습니다 .


1

명백한 것을 피하십시오 :

cd "D:\Some insanely long\path I really\don't want to type again…"
…
copy "..\..\..\..\thing that\lives in the swamp\*.pu" .

다음을 사용할 수도 있습니다 subst(Windows).

cd "D:\Some insanely long\path I really\don't want to type again…"
…
subst Z: .
cd "..\..\..\..\thing that\lives in the swamp"
…
copy *.pu Z:\
…
subst Z: /d
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.