파일 이름의 일부를 파일 이름 앞으로 이동

파일 이름의 일부를 파일 이름 앞으로 이동

파일 이름의 일부를 파일 이름 앞으로 옮기고 싶습니다. 내 모든 파일 #에는 다음과 같은 구분 기호가 있습니다 2585#xxxxx#4157.pdf.

#이제 마지막 부분 뒤의 부분을 파일 이름의 두 번째 부분으로 이동하고 싶습니다 . 예:2585#4157#xxxxx.pdf

Powershell로 어떻게 할 수 있나요? 무엇을 검색해야 할지 모르기 때문에 아직 어떤 방법도 직접 살펴보지 않았습니다.

답변1

아래 스크립트는 당신이 찾고 있는 것을 수행할 것입니다. 모든 단계가 명확하도록 간결하지 않은 루프로 작성되었습니다. 이를 스크립팅하는 더 최적의 방법이 있습니다.

# Assumes this script is run in the directory with the pdf files
# First get all pdf files in the current directory
$all_files = Get-ChildItem -Path . -Filter '*.pdf'

# Loop over all files
foreach ($file in $all_files) {
    # Get the current (old) filename
    $old_name = $file.Name
    # Define a regex pattern (see below)
    $regex_pattern = '(.+)#(.+)#(.+)(\.pdf)'
    # Define a replacement pattern (see below)
    $replace_pattern = '$1#$3#$2$4'
    # Construct the new name
    $new_name = $old_name -replace $regex_pattern, $replace_pattern
    # Actually rename the file
    Rename-Item -Path $file.FullName -NewName $new_name
}

정규식

정규식텍스트를 검색하고 바꾸는 고급 방법입니다.

검색 패턴은 다음 부분으로 나눌 수 있습니다.

(.+)      Match any character 1 or more times, store in the first group
#         Match the # symbol literally
(.+)      Match any character 1 or more times, store in the second group
#         Match the # symbol literally
(.+)      Match any character 1 or more times, store in the third group
(\.pdf)   Match a literal dot followed by the letters "pdf" and store in the fourth group

교체 패턴은 검색 패턴에서 저장된 부품의 순서를 다시 지정합니다.

$1  Take the content from the first group
#   Write a literal # symbol
$3  Take the content from the third group
#   Write a literal # symbol
$2  Take the content from the second group
$4  Take the content from the fourth group

이 스크립트를 실행하면 다음 파일의 이름이 변경됩니다.

2585#xxxxx#4157.pdf
2d23#ab23-421d#40++057.pdf
2d23#abd#400057.pdf

~ 안으로

2585#4157#xxxxx.pdf
2d23#40++057#ab23-421d.pdf
2d23#400057#abd.pdf

답변2

정규식 없이 다음을 수행할 수 있습니다.

(Get-ChildItem -Path 'X:\where\the\files\are' -Filter '*#*#*.pdf' -File) | 
Rename-Item -NewName { 
    $first, $second, $third = $_.BaseName.Split("#")
    '{0}#{1}#{2}{3}' -f $first, $third, $second, $_.Extension
}

관련 정보