Windows批处理根据条件将文件拆分为两个 [英] windows batch splitting file in to two based on conditions

查看:63
本文介绍了Windows批处理根据条件将文件拆分为两个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中开头单词是大小写"add","changeprop"或"anyother"我正在尝试读取此类文件,并基于是否有条件将特定行推送到add.txt,changeprop.txt和errorrecords.txt

I have a file where the starting words are any case "add" "changeprop" OR "anyother" I am trying to read such file and based on if conditions pushing that specific line to add.txt , changeprop.txt and errorrecords.txt

在替换位置处比较困难,因为变量没有保持行值,并且我无法完成此工作:(

Having hard time at the substing place as the variable is not holding the line value and i am unable to finish this job :(

set "File=.\WorkFlow_Action_Script_Merged_Folder\All_WorkFlows_Merged_File.txt"
SETLOCAL enabledelayedexpansion
for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
 SET eachline=%%a
  IF "%eachline:~0,3%"=="ADD" ( echo "%eachline%">>.\WorkFlow_Action_Script_Merged_Folder\ADD_WorkFlows_Merged_File.txt)
  IF "%eachline:~0,3%"=="and" ( echo "%eachline%">>.\WorkFlow_Action_Script_Merged_Folder\ADD_WorkFlows_Merged_File.txt)
  IF "%eachline:~0,10%"=="CHANGEPROP"  ( echo "%eachline%">>.\WorkFlow_Action_Script_Merged_Folder\CHANGEPROP_WorkFlows_Merged_File.txt)
  IF "%eachline:~0,10%"=="changeprop"  ( echo "%eachline%">>.\WorkFlow_Action_Script_Merged_Folder\CHANGEPROP_WorkFlows_Merged_File.txt)
 REM pause
)

在尝试调用/内部块/使用变量的其他方法之后,我感到迷失了方向.对于子串比较位置,没有什么保留刚刚读取的行的值.请提出建议.

I feel like losing the mind - after trying call / internal block / differnt ways of using a variable . nothing is holding value of the just read line for substring compare place. Please suggest.

推荐答案

由于您使用多个if语句,每个语句只有一个命令,因此无需在 echo 命令中加上括号.但是,您的问题实际上是您没有实际使用 enabledelayedexpansion 的事实.您还可以简单地将/i 开关与 if 语句一起使用,以区分大小写,因此无需为匹配而检查每行两次.最后,我将使用%〜dp0 而不是.\

As you are using multiple if statements with one command each, you do not need to parenthesize the echo commands. Your problem however is actually the fact that you enabledelayedexpansion without actually using it. You can also simply use /i switch with the if statement to be case insensitive, so no need to check each line twice for a match. Lastly, I would use %~dp0 instead of .\

@echo off
setlocal enabledelayedexpansion
set "File=%~dp0WorkFlow_Action_Script_Merged_Folder\All_WorkFlows_Merged_File.txt"
for /F "tokens=* delims=" %%a in ('Type "%File%"') do (
 set "eachline=%%a"
  if /i "!eachline:~0,3!" == "ADD" echo "!eachline!">>"%~dp0WorkFlow_Action_Script_Merged_Folder\ADD_WorkFlows_Merged_File.txt"
  if /i "!eachline:~0,10!" == "CHANGEPROP" echo "!eachline!">>"%~dp0WorkFlow_Action_Script_Merged_Folder\CHANGEPROP_WorkFlows_Merged_File.txt"
)

这篇关于Windows批处理根据条件将文件拆分为两个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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