Bat文件可将文本文件中的行拆分为多行 [英] Bat file to split row from text file into multiple rows

查看:759
本文介绍了Bat文件可将文本文件中的行拆分为多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每行我都有应用程序编号以及该应用程序的关联图像.

I have application number along with associated images for that application on every row.

58:58123 image.jpg,image2.jpg
58:58456 image4.jpg

我想拥有一个csv,它将在每行中包含唯一的数字和一个关联的图像,例如

I want to have csv which will contain unique number and one associated image on every row like

123 image.jpg
123 image2.jpg
456 image4.jpg

我已经创建了bat文件,但是显示为

I have already created bat file but it is showing me output as

58:58123 image.jpg
58:58123 image2.jpg
58:58456 image4.jpg

我想从每一行中删除58:58.

I want to remove 58:58 from every row.

@Echo off&(for /f "usebackq tokens=1* delims=:" %%A in ("transact.txt") do For %%C in (%%B) do Echo(%%A,%%C)>Output.csv

实际输出:

58:58123 image.jpg
58:58123 image2.jpg
58:58456 image4.jpg

预期输出:

123 image.jpg
123 image2.jpg
456 image4.jpg

推荐答案

将所有代码都放在一行中是一种不好的做法,因为它很难维护.

It is a bad practice to put all code into a single line as it is hardly maintainable.

无论如何,您的代码似乎试图删除前缀(例如58),并且确实不能返回您描述的实际输出.

Anyway, your code seem to make an attempt to remove the prefix (like 58), and it does for sure not return the actual output you describe.

但这是一种可能的方法(最有可能):

But here is a possible way to do what you want (most probably):

@echo off
rem // Write to the output file:
> "Output.csv" (
    rem // Read from the input file and split at the first `:`:
    for /F "usebackq tokens=1-2* delims=: " %%A in ("transact.txt") do (
        rem // Assign parts behind the first `:` to variables:
        set "NUM=%%B" & set "LIST=%%C"
        rem /* Enable delayed expansion to be able to read a variable
        rem    that is has been written in the same block of code: */
        setlocal EnableDelayedExpansion
        rem /* Remove prefix from number before the file names and then
        rem    provide it in a `for` variable (one loop iteration): */
        for %%D in ("!NUM:*%%A=!") do (
            rem /* Modify list so that spaces in file names are preserved,
            rem    loop through the file names (none must contain `,`!): */
            for %%E in ("!LIST:,=" "!") do (
                rem /* Return one file name after another preceded by the
                rem    pruned number; toggle delayed expansion in order to
                rem    avoid trouble with `!` or `^` in the file names: */
                endlocal
                echo(%%~D %%~E
                setlocal EnableDelayedExpansion
            )
        )
        endlocal
    )
)

此脚本利用了子字符串替换以及延迟扩展.

这篇关于Bat文件可将文本文件中的行拆分为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆