标题: [文本处理] 求助批处理如何按文件固定字符前字符命名压缩包名? [打印本页]
作者: jieyuan_1981 时间: 2022-6-29 15:31 标题: 求助批处理如何按文件固定字符前字符命名压缩包名?
我有一段代码是按照文件前10位数字来命名压缩包名,如果要是这样取 "_" 前几位相同字符来命名怎么改,如:
a_disk1.txt
a_disk2.txt
abc_disk1.txt
abc_disk2.txt
efgh_disk1.txt
efgh_disk2.txt
efgh_disk3.txt
想实现效果 a.zip abc.zip efgh.zip (里面文件要是a_disk1.txt a_disk2.txt ....)- @echo off
- cd /d %~dp0
- setlocal enabledelayedexpansion
- ::设置WinRAR的安装目录路径
- set WinRarDir=%ProgramFiles%\WinRAR
- set Path=%Path%;%WinRarDir%
- for %%a in (*.txt) do (
- set "Fn=%%~na"
- echo ,!Fs!,|findstr ",!Fn:~,10!," >nul || set "Fs=!Fn:~,10!,!Fs!"
- )
- for %%a in (!Fs!) do RAR a "%%~a" "%%~a*.txt"
复制代码
作者: Batcher 时间: 2022-6-29 18:15
回复 1# jieyuan_1981 - @echo off
- cd /d %~dp0
- setlocal enabledelayedexpansion
- REM 设置WinRAR的安装目录路径
- set WinRarDir=%ProgramFiles%\WinRAR
- set Path=%Path%;%WinRarDir%
- for %%a in (*.txt) do (
- for /f "delims=_" %%b in ("%%~na") do (
- set "Fn=%%b"
- echo ,!Fs!,|findstr ",!Fn!," >nul || set "Fs=!Fn!,!Fs!"
- )
- )
- for %%a in (!Fs!) do (
- WinRAR a "%%a.zip" "%%a_*.txt"
- )
复制代码
作者: jieyuan_1981 时间: 2022-6-29 19:56
回复 2# Batcher
winrar报错:没有文件被添加
作者: Batcher 时间: 2022-6-30 11:00
回复 3# jieyuan_1981
1. 请参考Q-04把bat文件保存为ANSI编码:
https://mp.weixin.qq.com/s/Koje4FufWxWBs7ioDy_LJA
2. 代码里面加几个echo看看是哪个环节出了问题,例如:- @echo off
- cd /d %~dp0
- setlocal enabledelayedexpansion
- REM 设置WinRAR的安装目录路径
- set WinRarDir=%ProgramFiles%\WinRAR
- set Path=%Path%;%WinRarDir%
- for %%a in (*.txt) do (
- echo 正在处理文件 %%a
- for /f "delims=_" %%b in ("%%~na") do (
- set "Fn=%%b"
- echo ---!Fn!
- echo ,!Fs!,|findstr ",!Fn!," >nul || set "Fs=!Fn!,!Fs!"
- echo +++!Fs!
- )
- )
- echo ===!Fs!
- for %%a in (!Fs!) do (
- WinRAR a "%%a.zip" "%%a_*.txt"
- )
复制代码
作者: jieyuan_1981 时间: 2022-6-30 11:53
回复 4# Batcher
感谢大神编译已通过。
作者: jieyuan_1981 时间: 2022-7-3 12:00
回复 5# jieyuan_1981
我找到出错的原因了,可能是空格的问题。导致有的过不去。会把空格前后字符单独处理。请帮改一下。
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |