Board logo

标题: [文件操作] 批处理怎样根据文件名生成文件夹,并放入文件? [打印本页]

作者: 刘总    时间: 2023-2-21 10:35     标题: 批处理怎样根据文件名生成文件夹,并放入文件?

例如:a1.txt,a2.txt,a3.txt 新建文件夹a并放入这三个文件
b1.txt,b2.txt,b3.txt,b4.txt 新建文件夹b并放入这四个文件
c1.txt,c2.txt,c3.txt,c4.txt,c5.txt 新建文件夹c并放入这五个文件
作者: Batcher    时间: 2023-2-21 10:53

回复 1# 刘总


    ab1.txt这样的文件怎么处理?
作者: 23618342    时间: 2023-2-21 11:24

三步骤,1.获取文件名,2按照首字母建立文件夹,3执行copy命令
作者: 23618342    时间: 2023-2-21 11:27

最简单的就是md a,执行copy命令 copy a*.txt a
作者: Batcher    时间: 2023-2-21 14:29

回复 1# 刘总


请注意先做好原始文件夹或文件备份,再开始测试代码。
请参考Q-04把bat文件保存为ANSI编码:
https://mp.weixin.qq.com/s/Koje4FufWxWBs7ioDy_LJA

文件名首字符创建文件夹并移动文件.bat
  1. @echo off
  2. cd /d "%~dp0"
  3. setlocal enabledelayedexpansion
  4. for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
  5.     set "FileName=%%i"
  6.     set "FolderName=!FileName:~0,1!"
  7.     if not exist "!FolderName!" (
  8.         md "!FolderName!"
  9.     )
  10.     move /y "%%i" "!FolderName!"
  11. )
复制代码

作者: 刘总    时间: 2023-2-21 14:57

回复 5# Batcher


    这个可以,感谢大神
作者: 23618342    时间: 2023-2-21 15:09

回复 5# Batcher


    大神厉害了,我知道步骤,写出代码估计要好一会




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