Windows Powershell-在目录中创建目录和文件
我试图通过创建子目录,然后将文件复制到新目录中,来清理和分组目录中的文件 例如:Windows Powershell-在目录中创建目录和文件,windows,powershell,file,directory,Windows,Powershell,File,Directory,我试图通过创建子目录,然后将文件复制到新目录中,来清理和分组目录中的文件 例如: test01.a.jpg test01.a.txt test01.b.bak test01.b.txt test02.a.txt test02.a.jpg test02.a.bak test03.a.txt test03.a.bak test03.b.txt 我希望创建子目录,如test01,test02,test03,并理想地复制相关文件。所有组都有一个txt文件,但其他组或多或少都有 要创建我所拥有的目
test01.a.jpg
test01.a.txt
test01.b.bak
test01.b.txt
test02.a.txt
test02.a.jpg
test02.a.bak
test03.a.txt
test03.a.bak
test03.b.txt
我希望创建子目录,如test01
,test02
,test03
,并理想地复制相关文件。所有组都有一个txt文件,但其他组或多或少都有
要创建我所拥有的目录,请执行以下操作:
gci -file *.txt | New-Item -ItemType directory $_.name
这不符合预期。如果您的文件有这样的名称,您可以简单地在点上拆分文件名,并仅将第一部分作为新文件夹名 然后测试这样的子文件夹是否已经存在(如果没有创建它),并移动该文件。 像这样的
$sourcePath = 'D:\Test' # put the real path to the files here
# if you want only the files with extension .txt, use filter '*.*.txt'
(Get-ChildItem -Path $sourcePath -File -Filter '*.*.*') | ForEach-Object {
# use the first part of the file name for subdirectory name
$destinationPath = Join-Path -Path $sourcePath -ChildPath $_.Name.Split(".")[0]
if (!(Test-Path -Path $destinationPath -PathType Container)) {
# if a subdirectory with that name does not already exist, create it
$null = New-Item -Path $destinationPath -ItemType Directory
}
# now move the file to that (new) destination folder
$_ | Move-Item -Destination $destinationPath
}
如果您的文件有这样的名称,您可以简单地在点上拆分文件名,并仅将第一部分作为新文件夹名称 然后测试这样的子文件夹是否已经存在(如果没有创建它),并移动该文件。 像这样的
$sourcePath = 'D:\Test' # put the real path to the files here
# if you want only the files with extension .txt, use filter '*.*.txt'
(Get-ChildItem -Path $sourcePath -File -Filter '*.*.*') | ForEach-Object {
# use the first part of the file name for subdirectory name
$destinationPath = Join-Path -Path $sourcePath -ChildPath $_.Name.Split(".")[0]
if (!(Test-Path -Path $destinationPath -PathType Container)) {
# if a subdirectory with that name does not already exist, create it
$null = New-Item -Path $destinationPath -ItemType Directory
}
# now move the file to that (new) destination folder
$_ | Move-Item -Destination $destinationPath
}
实际上,算法很简单(我们不需要事先比较文件名。我们只需要使用$\ux.BaseName属性)
gci*.txt |%{new item-ItemType Directory-Path($\.Directory.ToString()+“\”+$\.BaseName.ToString())}
gci-文件|%{Move item$\.Fullname($\.Directory.ToString()+“\”+$\\.BaseName.ToString())}
实际上,算法很简单(我们之前不需要比较文件名。我们只需要使用$BaseName属性)
gci*.txt |%{new item-ItemType Directory-Path($\.Directory.ToString()+“\”+$\.BaseName.ToString())}
gci-文件|%{Move item$\.Fullname($\.Directory.ToString()+“\”+$\\.BaseName.ToString())}
太棒了!这正是我需要的。非常感谢。您可以删除if
语句,只需编写$null=newitem-Path$destinationPath-ItemType Directory-Force
。这样,当路径已经存在时,它不会导致错误。@zett42您是对的。先测试是一个老习惯。太棒了!这正是我需要的。非常感谢。您可以删除if
语句,只需编写$null=newitem-Path$destinationPath-ItemType Directory-Force
。这样,当路径已经存在时,它不会导致错误。@zett42您是对的。先测试是一个老习惯。[1]显示从发布的示例文件名中得到的结果。//[2] 请看一看使用$\u0.BaseName.Split('.)[0]
获取基本名称的“嵌入点之前”部分。[1]显示从发布的示例文件名中获得的结果。//[2] 请看一下如何使用$\u.BaseName.Split('..)[0]
获取基本名称的“嵌入点之前”部分。