在powershell中移动与子字符串匹配的pdf文件
我需要根据从位置3的文本文件名获取的子字符串(在最后一个和最后一个“.”之间),将pdf文件从位置1移动到位置2。我无法在运行时重命名location1处的pdf文件,因为它有数十万个pdf文件&我只需要几个匹配子字符串模式的文件 位置3:在powershell中移动与子字符串匹配的pdf文件,powershell,pdf,split,substring,Powershell,Pdf,Split,Substring,我需要根据从位置3的文本文件名获取的子字符串(在最后一个和最后一个“.”之间),将pdf文件从位置1移动到位置2。我无法在运行时重命名location1处的pdf文件,因为它有数十万个pdf文件&我只需要几个匹配子字符串模式的文件 位置3: A_b_c_d_e_f_1.2.3.4.5.txt G_h_i_j_k_6.7.8.9.txt l_m_n_o_p_2.7.8.4.txt 5_rha_thye_lej.pdf 9_tyoe_hslel_hlssls.pdf 4_shl_heoe_keie
A_b_c_d_e_f_1.2.3.4.5.txt
G_h_i_j_k_6.7.8.9.txt
l_m_n_o_p_2.7.8.4.txt
5_rha_thye_lej.pdf
9_tyoe_hslel_hlssls.pdf
4_shl_heoe_keie_ekye.pdf
位置1:
A_b_c_d_e_f_1.2.3.4.5.txt
G_h_i_j_k_6.7.8.9.txt
l_m_n_o_p_2.7.8.4.txt
5_rha_thye_lej.pdf
9_tyoe_hslel_hlssls.pdf
4_shl_heoe_keie_ekye.pdf
我实现了从txt文件名获取子字符串,但移动与模式匹配的pdf会导致问题
$files = Get-ChildItem "location3" -Filter *.txt
forEach ($n in $files) {
$substring = $n.Name.split(".")[-2]
write-host $substring }
Move-Item (Join-Path location1\$substring) -Destination location2
这个问题我读了好几遍,我希望我明白你想要什么:
- 在location3中,有些.txt文件的文件名在扩展名前面的一个数字结尾,在
- 在位置1中查找文件名以任何数字开头,后跟下划线的pdf文件
- 将这些文件移动到位置2
$location1 = 'D:\Test\sourcefiles' # the source folder where the .pdf files are
$location2 = 'D:\Test\destination' # the destination to move the .pdf files to
$location3 = 'D:\Test' # where the .txt files are
# first test if the destinationfolder $location2 exists. If not create it
if (!(Test-Path -Path $location2 -PathType Container)) {
$null = New-Item -Path $location2 -ItemType Directory
}
# get an array of numbers taken from the textfile names in location3
$numbers = Get-ChildItem -Path $location3 -Filter '*.txt' -File |
Where-Object {$_.BaseName -match '\.(\d+)$'} | ForEach-Object { $matches[1] }
# next, loop through the sourcefolder $location1
Get-ChildItem -Path $location1 -Filter '*_*.pdf' -File | # find .pdf files that contain an underscore in the name
Where-Object { $numbers -contains ($_.Name -split '_')[0] } | # that have a name starting with one of the numbers we got above, followed by an underscore
Move-Item -Destination $location2
谢谢你,西奥。它解决了移动pdf文件的问题。