2016-02-12 53 views
-1

我試圖運行這個過程看起來到一個TEST.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 

輸出我想實現的是另外寫.BAT/shell腳本得到輸出3個文件:

文件1:object1.bat

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

文件2: object2.bat

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

文件3:object3.bat

object3.dir.file=c:\resources\open 

誰能告訴我什麼是實現這一目標而不窗口

使用任何第三方工具的最佳方式
+0

我會用'Get-Content','Where-Object'和'-matches'運算符來管理一個'ForEach-Object'循環,我會使用'$ Matches'自動變量確定文件名,並將「Add-Content」寫入文件。 – TheMadTechnician

+0

換句話說,你想_「讀取Test.bat文件中的行,並將它們輸出到由第一個以點分隔的標記和.bat擴展名指示的文件中,但將第一行替換爲每個後續文件中的第一行第一個文件的行「_?這可以通過'for/F'tokens = 1 * delims =以非常簡單的方式在.BATch文件中完成。「 (Test.bat)中的%% a執行echo %% a。%% b >> %% a.bat'命令;您只需要將每個文件中第一行的測試添加到此代碼中; – Aacini

+0

請解釋輸出是如何從輸入中導出的。每個輸出文件的第一行是相同的,除了開始的「對象」編號。這可能是修改前綴的輸入數據的第一行。其餘的行似乎直接從他們的「對象」號碼中複製,放下第一個。或者你的混淆是不完整的。你真的只是想要根據對象的數量將數據分割成文件嗎? – Magoo

回答

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 /?
相關問題