Windows批处理文件执行错误 [英] Windows Batch file execution error

查看:549
本文介绍了Windows批处理文件执行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个批处理文件来连接具有在工作folder.They模式 ContractEligibility _ * 所有CSV文件都是相同的格式,我需要没有串联重复的标题。

I am trying to run a batch file to concatenate all csv files with the pattern ContractEligibility_* in the working folder.They are all of the same format and i need to concatenate without the repeating header.

在code是如下:

@echo off>Combined.csv
cls
setlocal enabledelayedexpansion

if exist C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv del C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv

dir /a-d /b C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv>C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt

cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\

for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
    set /p header=<%%A
    echo !header!>Combined.csv
)

for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
        more +1 %%A>>Combined.csv
   )

del dirfiles.txt

这并置很好,但我得到一个回声已关闭,在文件的第一行的开始。
我知道,第一个for循环(一个在黑体)负责,因为它是通过null值的变量头。所有文件都在所设置的路径。
有人可以帮我解决这个问题。回声配售后下降为批处理文件之后,它利用创建的文件的自动负载是不能接受的 combined.csv

推荐答案

相当于的code以下解决问题,运行速度更快,外观更清洁的...

The equivalent code below solve the problem, run faster, and looks cleaner...

@echo off
setlocal enabledelayedexpansion

cls

cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\

set "header="
(for %%A in (ContractEligibility_*.csv) do (

   if not defined header (
      set /p header=<%%A
      echo !header!
   )

   more +1 %%A

)) > Combined.csv

这篇关于Windows批处理文件执行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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