如何在dir中的多个文本文件中搜索jpg的文件名,然后将它们移动到相应的文件夹
How to search in multiple text files in dir for file names of jpg and then move them to corresponding folders
我有这个文件夹,里面有文本文件和 jpg 文件。每个文本文件都包含类似
的信息
2021-01-12T07:22:14;0;R0010313.JPG
2021-01-12T07:23:03;0;R0010314.JPG
其中 R 编号对应于该名称的 jpg 文件。现在,我想读取每个文本文件,获取 jpgs 的名称,然后将 jpgs 移动到与文本文件同名的新文件夹,然后转到下一个文本文件。我设法创建了具有正确名称的新文件夹,所以现在我只需要将正确的 jpg 文件移动到正确的文件夹中。这是我目前所拥有的:
$path = "C:\Users\Yo\Desktop\Brunnar" #Path where all the TXT files are
$TxtFiles = Get-ChildItem -Path $path -filter "*.txt"
$JpgFiles = Get-ChildItem -Path $path -filter "*.JPG"
$files = Get-Childitem $path | Where-Object { ! $_.PSIsContainer} #Get all files in the folder
#For each file in this folder
foreach ($file in $files){
## If it is a txt file
if ([io.path]::GetExtension($file.Name) -eq ".txt"){ # If it is a .txt file
$foldername = [io.path]::GetFileNameWithoutExtension($file) #Remove the fileextension in the foldername
if (!(Test-Path "$path$foldername")){ #If the folder doesn't exist
New-Item "$path$foldername" -ItemType "directory" #Create a new folder for the file
}
## Move-Item $file.FullName "$path$foldername" #Move file into the created folder
Get-ChildItem -LiteralPath C:\Users\Yo\Desktop\Brunnar -Filter *.txt -File -Recurse | foreach {
#$fileData = @{
# "File"=$_.FullName;
$content =(Get-Content -LiteralPath $_.FullName -Raw)
#}
if($content -match $JpgFiles.Name){
#move jpg to the new folder
}
}
}
}
看看下面的代码片段(未经测试)。
它显示了执行任务所需的所有步骤,但可能需要进行一些调整。
$Path = 'C:\Users\Yo\Desktop\Brunnar'
# Process only the txt files in the direcotry
foreach ($TextFile in (Get-ChildItem -Path "$($Path)\*.txt" -File)) {
# Get the content of the text file
$Content = Get-Content -Path $TextFile.FullName
# Get the r number from content
$JPGFileName = $Content.Split(';')[2]
# Create folder if not exists
if (-not (Test-Path -Path "$($Path)$($TextFile.BaseName)")) {
New-Item -Path "$($Path)$($TextFile.BaseName)" -ItemType Directory
}
# Move the jpg file to the new path
if (Test-Path -Path "$($Path)$($JPGFileName)") {
Move-Item -Path "$($Path)$($JPGFileName)" -Destination "$($Path)$($TextFile.BaseName)$($JPGFileName)"
}
}
我有这个文件夹,里面有文本文件和 jpg 文件。每个文本文件都包含类似
的信息2021-01-12T07:22:14;0;R0010313.JPG
2021-01-12T07:23:03;0;R0010314.JPG
其中 R 编号对应于该名称的 jpg 文件。现在,我想读取每个文本文件,获取 jpgs 的名称,然后将 jpgs 移动到与文本文件同名的新文件夹,然后转到下一个文本文件。我设法创建了具有正确名称的新文件夹,所以现在我只需要将正确的 jpg 文件移动到正确的文件夹中。这是我目前所拥有的:
$path = "C:\Users\Yo\Desktop\Brunnar" #Path where all the TXT files are
$TxtFiles = Get-ChildItem -Path $path -filter "*.txt"
$JpgFiles = Get-ChildItem -Path $path -filter "*.JPG"
$files = Get-Childitem $path | Where-Object { ! $_.PSIsContainer} #Get all files in the folder
#For each file in this folder
foreach ($file in $files){
## If it is a txt file
if ([io.path]::GetExtension($file.Name) -eq ".txt"){ # If it is a .txt file
$foldername = [io.path]::GetFileNameWithoutExtension($file) #Remove the fileextension in the foldername
if (!(Test-Path "$path$foldername")){ #If the folder doesn't exist
New-Item "$path$foldername" -ItemType "directory" #Create a new folder for the file
}
## Move-Item $file.FullName "$path$foldername" #Move file into the created folder
Get-ChildItem -LiteralPath C:\Users\Yo\Desktop\Brunnar -Filter *.txt -File -Recurse | foreach {
#$fileData = @{
# "File"=$_.FullName;
$content =(Get-Content -LiteralPath $_.FullName -Raw)
#}
if($content -match $JpgFiles.Name){
#move jpg to the new folder
}
}
}
}
看看下面的代码片段(未经测试)。
它显示了执行任务所需的所有步骤,但可能需要进行一些调整。
$Path = 'C:\Users\Yo\Desktop\Brunnar'
# Process only the txt files in the direcotry
foreach ($TextFile in (Get-ChildItem -Path "$($Path)\*.txt" -File)) {
# Get the content of the text file
$Content = Get-Content -Path $TextFile.FullName
# Get the r number from content
$JPGFileName = $Content.Split(';')[2]
# Create folder if not exists
if (-not (Test-Path -Path "$($Path)$($TextFile.BaseName)")) {
New-Item -Path "$($Path)$($TextFile.BaseName)" -ItemType Directory
}
# Move the jpg file to the new path
if (Test-Path -Path "$($Path)$($JPGFileName)") {
Move-Item -Path "$($Path)$($JPGFileName)" -Destination "$($Path)$($TextFile.BaseName)$($JPGFileName)"
}
}