Windows批处理脚本*(明星)读为文本? [英] Windows Batch script * (star) read as text?

查看:192
本文介绍了Windows批处理脚本*(明星)读为文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个批处理脚本,将固定宽度的文本文件转换为.csv格式。这是我到目前为止写的:

  @echo off 
setlocal enabledelayedexpansion


for / Ftokens = *%% A in(HRV * .txt)do(
set var = %% A
set mer =!var:〜6,11!
set cr =!var:〜18,19!

set dt =!var:〜42,30!
set aa =!var:〜72,30!
set ab =!var:〜102,30!
set ac =!var:〜132,15!

set ad =!var:〜147,30!
set ae =!var:〜177,30!

set af =!var:〜283,36!

set ag =!var:〜318,3!
set ah =!var:〜329,7!
set ai =!var:〜337,17!
set aj =!var:〜442,2!
set ak =!var:〜460,15!
set al =!var:〜475,2!
set am =!var:〜482,15!

set y =!mer!!!cr !,!dt!,!aa!,!ab!,!ac!,!ad!,!ae!,=! ,!ag!,=ah,ai,aj,=ak,al,=am
echo!y! b $ b)

暂停

问题是, ,程序无法完成,并输出系统找不到文件HRV * .txt。然后我改为for循环到

  for / Ftokens = *%% A in(* .txt)do .. 

但是错误刚刚更改为系统找不到文件* .txt p>

看来,星号*正在读取为文本?这与tokens + *有什么关系?如何让脚本运行在任何文件以HRV开头?

解决方案

for / f 使用另一个简单替换

  for %% X in(HRV * .txt)do(
for / Ftokens = *%% A in(%% X)do(
... etc.


顺便说一下:你的重定向方式很慢。打开目标文件,写入一行文件并重新关闭文件打开和关闭文件是非常耗时的,你可以通过打开和关闭一次来避免它,而不是:

  for / l %% a in(1,1,10000)do(
echo something>> file.txt

这需要约27秒,do:

 
for / l %% a in(1,1,10000)do(
echo something

)> ; file.txt

这需要大约170 ms 。 (Ymmv - 次取决于您的计算机系统)


I'm writing a batch script to convert a fixed-width text file to .csv format. Here's what I've written so far:

@echo off
setlocal enabledelayedexpansion


    for /F "tokens=*" %%A in (HRV*.txt) do (
    set var=%%A
    set mer=!var:~6,11!
    set cr=!var:~18,19!

    set dt=!var:~42,30!
    set aa=!var:~72,30!
    set ab=!var:~102,30!
    set ac=!var:~132,15!

    set ad=!var:~147,30!
    set ae=!var:~177,30!

    set af=!var:~283,36!

    set ag=!var:~318,3!
    set ah=!var:~329,7!
    set ai=!var:~337,17!
    set aj=!var:~442,2!
    set ak=!var:~460,15!
    set al=!var:~475,2!
    set am=!var:~482,15!

    set y=!mer!.!cr!,"!dt!","!aa!","!ab!","!ac!","!ad!","!ae!",="!af!","!ag!",="ah","ai","aj",="ak","al",="am"
    echo !y!>> converted.csv
    )

pause

The problem is that when I run it, the program fails to complete, and outputs "The system cannot find the file HRV*.txt". I then changed for loop to

for /F "tokens=*" %%A in (*.txt) do ( ...

But the error just changed to "The system cannot find the file *.txt"

It seems that the star * is being read as text? does this have something to do with "tokens+*"? How can I make it so that the script will run on any file that starts with HRV?

解决方案

for /f expects a single file name and is not capable of using wildcards. Use another plain for around:

for %%X in (HRV*.txt) do (
  for /F "tokens=*" %%A in (%%X) do (
    ... etc. etc.
  )
)

by the way: your way of redirecting is very slow. The destination file is opened, one line is written and the file is closed again. Opening and closing a file is very time consuming. You can avoid it by opening and closing it just one time. Instead of:

for /l %%a in (1,1,10000) do (
  echo something>>file.txt
)

which needs about 27 seconds, do:

(
  for /l %%a in (1,1,10000) do (
    echo something
  )
)>file.txt

which needs about 170 ms. (Ymmv - times depends on your computer system)

这篇关于Windows批处理脚本*(明星)读为文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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