LoginSignup
0
0

More than 1 year has passed since last update.

POWERSHELL リストの名前を含むファイルをコピー

Last updated at Posted at 2021-06-07

ファイルの構成
1.jpg

$getlist = (Get-Content \searchlist.txt) -as [string[]]

foreach ($word in $getlist) {
    $list = "*" + $word +"*"
    $link = "\findfolder\"
    $file = $link + $list
    if(Test-Path $file)
    {
        Write-Host "見つかったワード:" $word
        Write-Host "コピーして移動しました:" $word
    }
    else
    {
        Write-Host "見つからなかったワード:" $word
        Write-Output $word | Out-File -Append "\folderreadcopy\out.txt"
    }
}



# 処理対象のフォルダ
$targetFolder = "\findfolder"

# コピー先のフォルダ
$destinationPath = "\copyfolder"

# $targetFolder内のファイル・フォルダのリストを取得する。
$itemList = Get-ChildItem $targetFolder;
foreach($item in $itemList)
{

    if($item.PSIsContainer)
    {
        # フォルダの場合の処理
        Write-Host ($item.Name + 'はフォルダです。'); 
    }
    else
    {
        # ファイルの場合の処理
        # 拡張子を除いたファイル名を$str_file_name_without_extここにいれる
        $str_file_name_without_ext = [System.IO.Path]::GetFileNameWithoutExtension($item);
        Write-Host ($str_file_name_without_ext + 'はファイルです。'); 
        # $fileにin.txtにあるリストをすべて代入
        $file = (Get-Content \searchlist.txt) -as [string[]]
        # $str_file_name_without_extに$fileのリストの文字が含まれていればtrue含まれていない場合はfalse
        foreach ($l in $file) 
        {
            if($str_file_name_without_ext.Contains($l))
            {
                $copymoto = "\findfolder\" + $item
                Copy-Item -Path $copymoto -Destination $destinationPath
            }


        }

    }
} 


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0