我有一个包含MANY文件的文件夹
Character 1 - object one.txt
Character 4 - Object two.txt
等等。
我想基于“ - ”之前的部分创建子文件夹,并将文件移动到该文件夹中。
我已经有一段时间没有触及过PS了,我很遗憾地说我不知道除了GCI之外从哪里开始..
非常感谢任何帮助!
答案 0 :(得分:3)
#Change Current Directory to the Folder with txt files
cd C:\path\to\folder
#List all the file contents and pipe to a foreach loop
Get-ChildItem -file | % {
#Split the file name at the dash, take the first half, and trim the spaces
#($_ is the pipeline variable so in this case it represents each file)
$folder = ($_ -split "-")[0].trim()
#if the folder doesn't exist, create it
if(-not (Test-path $folder)) {
New-item $folder -ItemType Directory
}
#Move file to the folder
Move-item $_ $folder
}