[英]Powershell to move text files from one folder to another
I have a folder that is having multiple subfolders, each folder have around 50 subfolders containing Text Files I want to move file from one folder to another without opening each and every subfolder. 我有一个包含多个子文件夹的文件夹,每个文件夹有大约50个包含文本文件的子文件夹,我想将文件从一个文件夹移动到另一个文件夹而无需打开每个子文件夹。 there are two kind of files 1. BSEG and 2. BKPF
有两种文件:1. BSEG和2. BKPF
I want to move files that are starting with BSEG in folder called BSEG and files starting from BKPF in a folder called BKPF 我想将以BSEG开头的文件移动到名为BSEG的文件夹中,并将以BKPF开头的文件移动到名为BKPF的文件夹中
the code I am using for BSEG files is 我用于BSEG文件的代码是
Move-Item D:\Company\SAP\JAN\"BSEG"*.txt D:\Company\BSEG
this code works but I have to write update code for each subfolder , I want help to modify this code so it looks for all subfolders 此代码有效,但我必须为每个子文件夹编写更新代码,我需要帮助来修改此代码,以便它查找所有子文件夹
Powershell uses -recurse to accomplish this. Powershell使用-recurse完成此操作。 Give this a shot.
试一下。 The -recurse piece tells it to hit each sub folder then pipe the output to the move item.
-recurse片段告诉它命中每个子文件夹,然后将输出通过管道传递到移动项。 It is worth noting that this will not grab the folder structure, just the files, and it will also fail if you try to move files with identical names.
值得注意的是,这将无法抓住文件夹结构,而不会抓住文件,如果尝试移动具有相同名称的文件,也会失败。
Get-ChildItem D:\Company\SAP\JAN\"BSEG"*.txt -recurse | Move-Item -Destination D:\Company\BSEG
dfundako has the half of it. dfundako拥有一半。
Move-Item
will not go into sub folders. Move-Item
不会进入子文件夹。 Using Get-ChildItem
with the -Recurse
switch will help get you the files you want to move. 将
Get-ChildItem
与-Recurse
开关一起使用将帮助您获取要移动的文件。
You also have two different sets of files to move. 您还可以移动两组不同的文件。 Never repeat code that you need to reuse.
永远不要重复您需要重用的代码。 So lets have a simple
foreach
loop with both of your file sets. 因此,让这两个文件集都有一个简单的
foreach
循环。 This works well since you are moving files of the same prefix into their own folders. 由于您要将相同前缀的文件移动到其自己的文件夹中,因此效果很好。
foreach($fileType in $fileTypes){
Get-ChildItem -Path D:\Company\SAP\JAN\ -Filter "$fileType*.txt" -Recurse |
Move-Item -Destination D:\Company\$fileType
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.