简体   繁体   English

Powershell将文本文件从一个文件夹移动到另一个文件夹

[英]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.

相关问题 在Powershell中将X文件从一个文件夹移动到另一个文件夹 - Move X files from one folder to another in Powershell 从文本文件的文件名列表将文件从一个文件夹移动到另一个文件夹 - Move files from one folder to another from a list of filenames from a text file Powershell 无法仅将文件从一个目录移动到另一个目录? - Powershell Unable to move only files from a one dire to another directory? 将某些类型的文件从一个文件夹移动到另一个文件夹 - Move files of certain type from one folder to another 不使用-Recurse将文件从一个文件夹移动到另一个文件夹 - Move Files from one folder to another without using -Recurse 尝试将旧的日志文件从一个文件夹移动到另一个文件夹 - Trying to move the old log files from one folder to another 如何使用PowerShell仅将新文件从一个文件夹复制到另一个文件夹 - How To copy only new files from one folder to another with PowerShell Powershell 替换一个文件夹中多个文件中的文本 - Powershell to replace text in multiple files in one folder 读取文件,然后根据文件内容名称,使用 Powershell 脚本将文件从一个文件夹移动到另一个文件夹 - Read a file and then based on file content names, move the file from one folder to another using Powershell script Powershell 脚本将文件从一个文件夹移动到同一文档库中的另一个文件夹 - Powershell script to Move files from one folder to other in same document library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM