PowerShellでは、「プッシュ」スタックの最後のディレクトリにコピーする方法はありますか?


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

それらは簡単に翻訳できますか?どうやらpowershellには単純な変数がない%cd%か、%path%または他の単純な変数がないため、問題がありました。

回答:


1

これを試してください、あなたの古いbatファイルがしたことをするべきです。yourName.ps1として保存し、powershellをadminとして起動し、「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 14

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.