Board logo

标题: [文本处理] 批处理如何以指定文本里的每一行内容为文件名生成/创建多个txt文本文件 [打印本页]

作者: hepeisheng    时间: 2016-11-1 21:09     标题: 批处理如何以指定文本里的每一行内容为文件名生成/创建多个txt文本文件

本帖最后由 pcl_test 于 2016-11-1 21:18 编辑

有重复就在名字后加重复1字样,有很多重复,


如txt
12345
12345
12345

12345.txt
12345重复1.txt
12345重复2.txt


谢谢
作者: pcl_test    时间: 2016-11-1 21:55

1、逐行读取文本
2、生成/创建指定名称的文本
3、判断指定名称的文件是否存在
4、重复的累加数字
以上几点哪里不会,文件名不合法的字符又有哪些
作者: hepeisheng    时间: 2016-11-1 22:15

以上几点全部不会,每行的文本字符已经修改为可建文件名的文字,*?:"<>/\|/||是没有的
作者: hepeisheng    时间: 2016-11-1 22:27

*星号
?问号
:冒号
,豆号
=等号
空格
<左括号
>右括号
/右斜号

我把文本里不能建文件名字符都改成中文了
作者: hepeisheng    时间: 2016-11-1 22:42

把那些非法字符改成中文字也可以的
作者: pcl_test    时间: 2016-11-1 22:43

本帖最后由 pcl_test 于 2016-11-3 14:12 编辑

回复 3# hepeisheng

http://www.bathome.net/viewthread.php?tid=35774
http://www.bathome.net/viewthread.php?tid=517
http://bbs.bathome.net/viewthread.php?tid=9355
  1. rem win7及以上系统运行
  2. powershell -c "$a=@{};gc '文本.txt'|?{$_ -notmatch '[\\/:*?""<>|\t]'}|%%{$n=$_+'.txt';$a[$_]=1;if(Test-Path $n){while(Test-Path ($n=$_+'重复'+$a[$_]+'.txt')){$a[$_]+=1}};ni $n -type file}"
  3. pause
复制代码

作者: hepeisheng    时间: 2016-11-1 22:50

我只会做饭吃,这些不懂呀
作者: pcl_test    时间: 2016-11-1 23:03

回复 7# hepeisheng

说得好像生来就会做饭吃似的
作者: hepeisheng    时间: 2016-11-1 23:06

都老年痴呆了。好难学的
作者: pcl_test    时间: 2016-11-1 23:18

回复 9# hepeisheng


    老年痴呆还能做饭吃也是蛮厉害的,不要忘了关煤气,多动脑对老年痴呆有益,老了闲着就该多学习,传播正能量,不要倚老卖老,没事摔倒
作者: Bella    时间: 2016-11-1 23:47

本帖最后由 Bella 于 2016-11-5 01:31 编辑

回复 9# hepeisheng


即使 0 编程基础, 1 个月也能学的不错了
作者: aa77dd@163.com    时间: 2016-11-2 00:30

回复 11# Bella

好学的孩子是可爱滴孩子
作者: hepeisheng    时间: 2016-11-2 04:32

以前我也学编程。那是1991年。那年还在用Dos.视窗是95的事。但现在全荒废了。不能会东西。一研究就头痛掉头发
作者: GNU    时间: 2016-11-2 09:25

第一步,逐行读取文本内容:
  1. @echo off
  2. for /f "delims=" %%i in ('type "1.txt"') do (
  3.     echo,%%i
  4. )
  5. pause
复制代码

作者: hepeisheng    时间: 2016-11-2 09:39

然后呢 ,等待中
作者: GNU    时间: 2016-11-2 09:56

第二步,创建文件:
  1. @echo off
  2. for /f "delims=" %%i in ('type "1.txt"') do (
  3.     echo 正在处理 %%i
  4.     type nul > "%%i.txt"
  5. )
  6. pause
复制代码

作者: hepeisheng    时间: 2016-11-2 10:40

接近成功的一半了
作者: 523066680    时间: 2016-11-2 11:30

本帖最后由 523066680 于 2016-11-2 11:33 编辑

估计 Powershell 会有一种黑魔法:
Create-File("filename.ext").IfThisFileExists.AddSuffixtoFileName("重复%d")

字母一定要多
作者: hepeisheng    时间: 2016-11-2 14:14

未下班,放工再试
作者: GNU    时间: 2016-11-2 17:17

回复 18# 523066680


你用的哪个版本?

PS C:\Users\GNU> Create-File("filename.ext").IfThisFileExists.AddSuffixtoFileName("重复%d")
You cannot call a method on a null-valued expression.
At line:1 char:1
+ Create-File("filename.ext").IfThisFileExists.AddSuffixtoFileName("重复%d")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PS C:\Users\GNU> Get-Host | Select-Object Version

Version
-------
4.0
作者: 523066680    时间: 2016-11-2 17:37

回复 20# GNU


    我在黑powershell啊
作者: hepeisheng    时间: 2016-11-2 19:45

未解决555555555555
作者: hepeisheng    时间: 2016-11-3 10:35

求各位大学仕帮帮我,
作者: GNU    时间: 2016-11-3 10:44

回复 21# 523066680


    希望 pcl_test 版主严惩你这种灌水行为
作者: hepeisheng    时间: 2016-11-3 17:44

不要灌水呀
作者: hepeisheng    时间: 2016-11-4 08:44

未解决,求帮帮我




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