
일부 파일의 이름을 바꾸려고 합니다. 경로와 파일 이름에는 [
, ]
공백과 같은 기호가 있습니다. 이름 바꾸기 명령은 파일을 찾을 수 없다고 말합니다.
기호가 포함된 기본 파일과 "임시" 파일이 있습니다. 메인 파일은 삭제하고 임시 파일 이름은 원본에서 따와야 합니다.
내 코드는 다음과 같습니다
$onlypath = Split-Path -Path $_.FullName
Rename-Item $onlypath + "\TEMP" $_.Name
다음과 같은 다른 많은 구문을 테스트해 보세요.
Rename-Item -Path "$onlypath\TEMP" -NewName $_.Name
or
Rename-Item -Path $onlypath"\TEMP" -NewName $_.Name
or
Rename-Item -Path $onlypath + "\TEMP" -NewName $_.Name
오류는 파일이 이 위치에 없을 때마다 발생합니다. 쉘에서 명령을 시도했는데 동일한 오류가 발생했지만 기호 앞에 이스케이프 문자로 `를 추가하면 작동합니다.
인사말
답변1
코드를 보면 파일 이름을 바꾸고 싶지 않은 것 같지만이동하다새로운 길로 가는 것입니다. Rename-Item은 실제로 파일이 위치한 곳에서 파일 이름을 바꾸는 것입니다. 그런 다음 새 파일 이름을 지정하면 경로를 변경하기 위한 것이 아닙니다.
또한 파일에 와 같은 기호가 있는 경우 대신 [,]
다음을 지정해야 합니다 .-LiteralPath
-Path
노력하다
$sourcePath = 'X:\Somewhere\Here[123]'
$destination = Join-Path -Path $sourcePath -ChildPath 'Temp'
# make sure the destination folder exists
$null = New-Item -Path $destination -ItemType Directory -Force
# get the files and move them to the destination
Get-ChildItem -LiteralPath $sourcePath -File | ForEach-Object {
Move-Item -LiteralPath $_.FullName -Destination $destination
}