Board logo

标题: [文件操作] 批处理怎样把第一个下划线前名字相同的文件自动合并并建立同名文件夹? [打印本页]

作者: yt_ljl    时间: 2019-10-10 12:58     标题: 批处理怎样把第一个下划线前名字相同的文件自动合并并建立同名文件夹?

求助:第一个下划线前名字相同的文件自动合并并建立同名文件夹
比如:abc_1   abc_2 自动建立并合并到叫abc的文件夹。
十分感谢!
作者: yt_ljl    时间: 2019-10-10 13:20

再现求助,,
作者: ivor    时间: 2019-10-10 13:36

本帖最后由 ivor 于 2019-10-10 13:41 编辑
  1. 2>nul md abc & move abc* .\abc
复制代码

作者: Batcher    时间: 2019-10-10 14:03

  1. @echo off
  2. for /f "tokens=1,* delims=_" %%i in ('dir /b /a-d *_*.txt') do (
  3.     if not exist "%%i" (
  4.         md "%%i"
  5.     )
  6.     move /y "%%i_%%j" "%%i"
  7. )
复制代码

作者: yt_ljl    时间: 2019-10-10 14:26

回复 4# Batcher

您好 试了下还是没反应
作者: Batcher    时间: 2019-10-10 15:45

回复 5# yt_ljl


    把你测试用的txt文件和bat文件打包压缩传上来我试试
作者: xczxczxcz    时间: 2019-10-10 17:28

假定为 ANSI 编码:
powershell "(ls *_*.txt|?{$_.BaseName -match '^[^_]+_'}|group {[regex]::Match($_,'^[^_]+').value}).Foreach{gc $_.Group|sc ($_.Name+'.txt')}"
作者: xczxczxcz    时间: 2019-10-10 17:33

powershell :
  1. (ls *_*.txt|?{$_.BaseName -match '^[^_]+_'}|group {[regex]::Match($_,'^[^_]+').value}).Foreach{sc ($_.Name+'.txt') -Value {gc $_.Group}}
复制代码

作者: ivor    时间: 2019-10-10 20:55

  1. ls *_*.txt | group {[regex]::Match($_.Name,"^[^_]*")}| %{ mkdir $_.Name -ErrorAction Ignore;Move-Item -Path ($_.Group) -Destination $_.name}
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2