Programing

존재하지 않는 경우 폴더 만들기- "항목이 이미 있습니다."

lottogame 2020. 11. 10. 07:42
반응형

존재하지 않는 경우 폴더 만들기- "항목이 이미 있습니다."


이 질문에 이미 답변이 있습니다.

PowerShell을 사용하여 폴더가없는 경우 폴더를 만들려고합니다.

$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\MatchedLog"
if(!(Test-Path -Path MatchedLog )){
   New-Item -ItemType directory -Path $DOCDIR\MatchedLog
}

이것은 폴더가 이미 존재한다는 오류를 제공하지만 폴더를 만들려고해서는 안됩니다.

여기서 무엇이 잘못되었는지 잘 모르겠습니다.

New-Item : 지정된 이름이 C : \ Users \ l \ Documents \ MatchedLog 인 항목이 이미 있습니다. C : \ Users \ l \ Documents \ Powershell \ email.ps1 : 4 char : 13 + New-Item <<<< -ItemType 디렉터리 -Path $ DOCDIR \ MatchedLog + CategoryInfo : ResourceExists : (C : \ Users \ l. ... ents \ MatchedLog : String) [New-Item], IOException + FullyQualifiedErrorId : DirectoryExist, Microsoft.PowerShell.Commands.NewItemCommand`


집중도 안했는데 방법은 여기

$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = '$DOCDIR\MatchedLog'
if(!(Test-Path -Path $TARGETDIR )){
    New-Item -ItemType directory -Path $TARGETDIR
}

New-Item을 사용하면 Force 매개 변수를 추가 할 수 있습니다.

New-Item -Force -ItemType directory -Path foo

또는 ErrorAction 매개 변수

New-Item -ErrorAction Ignore -ItemType directory -Path foo

-Not연산자를 사용하고 가독성에 대한 선호도에 따라 대체 구문 :

if( -Not (Test-Path -Path $TARGETDIR ) )
{
    New-Item -ItemType directory -Path $TARGETDIR
}

참고 URL : https://stackoverflow.com/questions/17329443/creating-a-folder-if-it-does-not-exists-item-already-exists

반응형