홈>
D:\TestARC_Source
라는 디렉토리에 다음과 같은 수백 개의 Excel 파일이 있습니다.
5020190429.dat
5120190429.dat
602019111121.dat
702019050926.dat
etc.
파일 이름의 처음 두 문자를 기준으로 각 폴더로 이동하려면 스크립트가 필요합니다. 대상 디렉토리에 폴더가 이미 생성되어 있습니다.
D:\TestARC_Destination\file_50
D:\TestARC_Destination\file_51
D:\TestARC_Destination\file_60
D:\TestARC_Destination\file_70
파일 이름의 처음 두 문자를 기준으로 파일이 각 폴더로 이동해야합니다. 예를 들어
5020190429.dat
50_file
로 이동
폴더.
아래 스크립트를 편집하려고하는데 작동하지 않습니다. PS 스크립트에 대한 지식이 거의 없으며 도움을 주시면 감사하겠습니다.
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"
# Find all files matching *.sql in the folder specified
Get-ChildItem -Path $SourceFolder -Filter *.dat | ForEach-Object {
# Combine the source filename and target directory
# The source filename has all instances of _ replaced with \
# Cast the resulting string to a FileInfo object to take advantage of extra methods
[System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_","\"))
# Create the directory if it doesn't already exits
if (!(Test-Path) $destination.Directory.FullName)
{
New-item -Path $destination.Directory.FullName -ItemType Directory
}
# Copy the source to the target directory
copy-item -Path $_.FullName -Destination $Destination.FullName
}
- 답변 # 1
- 답변 # 2
파일을 첫 번째 두 자리로 그룹화하는 효율적인 접근 방법입니다.
폴더가 존재하는지 확인하고 (필요한 경우 생성) 파일을 한 번에 이동하십시오.## Q:\Test\2019\05\31\SO_56397901.ps1 $SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\" $targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\" Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat | Group-Object {"file_"+$_.BaseName.Substring(0,2)}| ForEach-Object { $TargetDir = Join-Path $targetFolder $_.Name if(!(Test-Path $TargetDir)){MD $TargetDir | Out-Null} $_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf }
위의 테스트 데이터로 실행 한 후 트리 :
> TREE /f ├───FILE_50 │ 5020190429.DAT │ ├───FILE_51 │ 5120190429.DAT │ ├───FILE_60 │ 602019111121.DAT │ └───FILE_70 702019050926.DAT
수정 : 요구 사항 변경은 일반적으로 새로운 질문을 할 수 있지만 변경은 최소입니다
## Q:\Test\2019\05\31\SO_56397901.ps1 $SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\" $targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\" Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat | Group-Object {$_.BaseName.Substring(0,2)+"_file"}| ForEach-Object { $TargetDir = Join-Path $targetFolder $_.Name if(Test-Path $TargetDir){ $_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf } else { "{0} doesn't exist, files not moved: {1}" -f $TargetDir,($_.Group.FullName -join ';') } }
관련 자료
- powershell - 이름을 기준으로 하위 디렉터리에 파일 복사
- 열의 범주를 기반으로 R에서 Excel 파일 내보내기
- PowerShell을 사용하여 anonfiles에 파일을 업로드하는 방법
- python - 다른 파일 이름 폴더를 기반으로 폴더의 모든 파일 이름을 바꾸는 방법은 무엇입니까?
- python 3.x - Pandas는 여러 디렉터리의 Excel 파일을 세 번째 디렉터리로 결합합니다
- 이전 일치 기준을 기반으로 한 Excel 수식 계산
- vba - 텍스트 파일을 만들기 위해 Excel 매크로를 트리거하는 Powershell이 작동하지 않습니다
- asp.net - Powershell을 사용하여 Servicenow 티켓에서 첨부 된 Excel 파일 다운로드
- 두 열의 일치를 기반으로 두 파일을 병합하지 않는 awk
- Powershell 기능 복사 파일
- PowerShell 이메일에 여러 파일을 첨부 파일로 추가하는 방법
- Excel에서 다른 셀 값을 기반으로 셀 서식을 지정하는 방법은 무엇입니까?
- loops - R에서 Excel 파일 폴더를 조작하는 방법은 무엇입니까?
- c# - 이전 appsettings 파일을 기반으로 사용자 지정 appsettings 파일로드
- ansible을 사용하여 vars 플래그를 기반으로 파일을 머신에 복사하려면 어떻게해야합니까?
- PowerShell을 사용하여 이름에 따라 파일 목록을 폴더에 복사
- PowerShell에 파일이 있는지 확인
- PowerShell을 사용하여 파일의 메타 데이터 편집 (Windows 파일 탐색기에서 파일의 세부 정보 탭)
- Powershell에서 1GB가 넘는 파일로 작업 할 때 "OutOfMemoryException"이 발생합니다
- 파일을 반복하여 PowerShell 스크립트 실행
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- JavaScript 변수를 HTML div에 '출력'하는 방법
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- python - 화면에서 찾은 요소를 찾을 수없는 경우 셀레늄
다음은 기준에 따라 파일을 이동해야합니다.
-WhatIf
를 제거하십시오 이동할 내용에 만족하면 전환합니다.