Batch Script to Copy Content from one file to another depending on search criteria

时间:2016-02-12 21:15:03

标签: windows shell powershell batch-file

I am trying to run this a process which looks into a Test.bat file with the following information:

import java.util.Scanner;
public class Counter
{ 
    // class variables shared by more than one method
    String prompt;
    static String strUserResponse;

    // main method
    public static void main (String[] args)
    {
        vowelCounter();
    }
public static void vowelCounter()
{
    for (int i = 0; i<strUserResponse.length();i++)
    {
        char v = strUserResponse.charAt(i);
         if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u'
         || v == 'A'|| v == 'E' || v == 'I' || v == 'O' || v == 'U')
        {
            System.out.print ("Vowels: " + v);
        }
   }

The output I am trying to achieve is to write another .bat/Shell script to get output as 3 files:

File 1: object1.bat

object1.dir.file=c:\resources\open
object1.grp.dir=\\\drive\config\code
object1.grp.share=\\\drive\config\code
object2.grp.file=\\\drive\config\code
object2.grp.dir=\\\drive\config\code
object3.grp.file=\\\drive\config\code

File 2: object2.bat

object1.dir.file=c:\resources\open
object1.grp.dir=\\\drive\config\code
object1.grp.share=\\\drive\config\code

File 3: object3.bat

object2.dir.file=c:\resources\open
object2.grp.dir=\\\drive\config\code

Can anyone tell me what is the best way to achieve this without using any 3rd party tools in windows

1 个答案:

答案 0 :(得分:0)

根据发布的输入和输出数据,您真正想要的并不是很清楚。

Aacini已发布 FOR 循环,可用于简单拆分为多个批处理文件。

这是一个增强的评论版本,其中包含一些额外的检查,可以命名为Test.bat以便自行运行。

@echo off
if not exist Test.bat goto :EOF

setlocal EnableDelayedExpansion

rem Delete all existing object*.bat files in current directory.

if exist object*.bat del /A /F /Q object*.bat

rem Process the lines of the batch file and split each line
rem into two strings on first point found in each line.
rem If the first string starts case-insensitive with word "object"
rem and there is also a second string, output the line into a file
rem with name of first string and with .bat as file extension.

for /F "usebackq tokens=1* delims=." %%A in (Test.bat) do (
    set "LineBegin=%%A"
    if /I "!LineBegin:~0,6!" == "object" (
        if not "%%B" == "" echo %%A.%%B>>%%A.bat
    )
)
endlocal
goto :EOF

rem Lines to write into several object*.bat files.

object1.dir.file=c:\resources\open
object1.grp.dir=\\\drive\config\code
object1.grp.share=\\\drive\config\code
object2.grp.file=\\\drive\config\code
object2.grp.dir=\\\drive\config\code
object3.grp.file=\\\drive\config\code

创建的批处理文件包含:

<强> object1.bat:

object1.dir.file=c:\resources\open
object1.grp.dir=\\\drive\config\code
object1.grp.share=\\\drive\config\code

<强> object2.bat:

object2.grp.file=\\\drive\config\code
object2.grp.dir=\\\drive\config\code

<强> object3.bat:

object3.grp.file=\\\drive\config\code

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?