Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I actually have an answer for this. Yes it is painful, however I had a similar problem and I don't actually know VBScript (though I am planning on learning it...) for the time though my problem occurred with a coworker having a customer with 20,000 files they flubbed from a conversion of outside data. All files were xml and they all were missing the same 2nd line of the XML which triggered a refile of the document we were importing.</p> <p>I wrote a standard batch script in tandem with another I found on StackOverflow which allowed me to split the files into 2 parts and then in between them insert the code I wanted. Now my only problem (probably due to laziness or my lack of knowledge/patience) was that I couldn't escape the &lt; , > problem. The script kept thinking that I was trying to write to a file, which was invalid. I tried all sorts of ways to use that character, but I wanted it in a variable form. Needless to say, I got it working (well even)...</p> <p>Below is the readme I provided to my coworker, along with the code from each file.</p> <p><strong>README.txt</strong> Problem: Massive amount of files were missing a string or piece of code and need to be edited</p> <p>Solution: This tools takes apart files and injects a string or piece of code and then put the files back together in another location.</p> <p>There are a total of 4 files that come with this tool.</p> <pre><code> **1 - _README.txt - This file describes how to use the script **2 - insert.txt - This file contains the text that will be inserted into the file you need edited. **3 - InsertString.bat - This file contains the actual script that loops to restructure the file. Here you will find all the variables that need to be set to make this work. **4 - String_Insert_Launcher.bat - This file is what you will launch to run the InsertString.bat file. </code></pre> <p>What you need to do:</p> <ol> <li><p>Edit String_Insert_Launcher and place this file in the directory with the files you want to edit. <strong><em>NOTE</em></strong> It is imperative that this file be in the same folder as ALL of the rest of your files you want edited. You need to edit the variables in this file to match you filesystem batchpath</p></li> <li><p>Edit InsertString.bat and place this file in the same directory you set the batchpath variable above You need to edit the variables in this file to match your filesystem insertpath destpath top_last_line insert_last_line bot_last_line</p></li> <li><p>Edit the insert.txt and place this file in the same directory you set the insertpath above You need to put the string(s) you want to be inserted into your file inside this text document</p></li> <li><p>Check your logs and make sure that the number of files in the " Modified_Filelist.txt " (found in the %insertpath%) is the same as the number of file you started with.</p></li> </ol> <p>Breakdown of files:</p> <hr> <p><strong>* insert.txt *</strong></p> <hr> <p>Inside this file you will want to put the text that you want inserted into the files you will target. The reason for using a separate file is so that special characters (>,&lt;,/,\,|,^,%,etc...) aren't treated like arguments within the batch file. This file HAS TO BE in the same location as the variable you will set in InsertString.bat called ' insertpath ' or referenced in the batch file as %insertpath%.</p> <hr> <p><strong>* InsertString.bat *</strong></p> <hr> <p>Inside this file you will find the variables that need to be set for the script to work. Variables included:</p> <pre><code> **1. filelist - This sets the counter for counting how many files were edited *this should not be edited* **2. insertpath - This sets the path of insert.txt file containing the string you want to insert into the files that will be edited. If this location does not exist it will create it. **3. destpath - This sets the path for the location of the files after they're edited. If this location does not exist it will create it. **4. top_last_line - This sets the LAST GOOD LINE of the file that will be edited before the insert.txt is added. In essence this will split the file into 2 parts and add the contents of " insert.txt " into the middle of those 2 parts. **5. insert_last_line - This sets the number of lines to add to the file from insert.txt (i.e. if insert_last_line=2 then the top two lines will be added after top_last_line) **6. bot_last_line - This sets the last line of the original file (i.e. if there are 25 lines in the original file bot_last_line should be 25 - always over esitimate this, because if this number is less than the original not all lines will be rewritten to the new file) </code></pre> <p>This file HAS TO BE in the same location as the variable you will set in String_Insert_Launcher.bat called ' batchpath ' or referenced in the batch file as %batchpath%.</p> <hr> <p><strong>* String_Insert_Launcher.bat *</strong></p> <hr> <p>This is the script you will execute to edit all the files. Launch this batch script FROM the folder with the files in it you want to edit. This file grabs all of the file names and runs the InsertString.bat ON all of these files. Inside this file you will find a varaible that nees to be set for the script to work. Variable included: <code>batchfilepath</code> - This is the location of the actual batch file that does all of the work. This location is JUST the filepath, not including any filenames.</p> <p><strong>FILE #1: String_Insert_Launcher.bat</strong></p> <pre><code>@ECHO off TITLE Insert String to XML Script Launch File COLOR 02 set batchfilepath=C:\JHA\Synergy\insertpath REM This is the location of the actual batch file that does all of the work. This location is JUST the filepath, not including any filenames. IF NOT exist %batchfilepath% md %batchfilepath% IF NOT exist %batchfilepath%\InsertString.bat goto pause :run for /f "delims=" %%f in ('dir /b /a-d-h-s') do "%batchfilepath%\InsertString.bat" %%f REM This command string gets the names of all of the files in the directory it's in and then runs the InsertString.bat file against every file individually. :pause cls echo.The file InsertString.bat is not in the correct directory. echo.Please put this file in the location listed below: echo. echo.------------------------- echo.%batchfilepath% echo.------------------------- echo. echo.When this file has been added press any key to continue running the script. pause goto run REM Insert String to XML Script REM Created by Trevor Giannetti REM An unpublished work </code></pre> <p><strong>FILE #2: Insert_String.bat</strong></p> <pre><code>@ECHO off TITLE Insert String to XML Script COLOR 02 SETLOCAL enabledelayedexpansion REM From Command Line: for /f "delims=" %f in ('dir /b /a-d-h-s') do InsertString.bat %f REM --------------------------- REM *** EDIT VARIABLES BELOW *** REM --------------------------- set insertpath=C:\JHA\Synergy\insertpath REM This sets the path of insert.txt file containing the string you want to insert into the files that will be edited. If this location does not exist it will create it. set destpath=C:\JHA\Synergy\destination REM This sets the path for the location of the files after they're edited. If this location does not exist it will create it. set top_last_line=1 REM This sets the LAST GOOD LINE of the file to be edited before the insert.txt is added. In essence this will split the file into 2 parts and add the contents of " insert.txt " into the middle of those 2 parts. set insert_last_line=1 REM This sets the number of lines to add to the file from insert.txt (i.e. if insert_last_line=2 then the top two lines will be added after top_last_line) set bot_last_line=25 REM This sets the last line of the original file (i.e. if there are 25 lines in the original file bot_last_line should be 25 - always over esitimate this, because if this number is less than the original not all lines will be rewritten to the new file) REM --------------------------- REM *** DO NOT EDIT BELOW *** REM --------------------------- set filelist=0 REM This sets the counter for counting how many files were edited IF '%1'=='' goto usage IF NOT exist %insertpath% md %insertpath% IF NOT exist %destpath% md %destpath% :top_of_file IF EXIST %destpath%\%1 set done=T IF EXIST %destpath%\%1 goto exit IF '%1'=='InsertString.bat' goto exit IF '%1'=='insert.txt' goto exit IF '%1'=='Modified_Filelist.txt' goto exit IF '%1'=='String_Insert_Launcher.bat' goto exit set /a FirstLineNumber = 1 REM This is the first line in the file that you want edited set /a LastLineNumber = %top_last_line% REM This is the last line in the file that you want edited SET /a counter=1 for /f "usebackq delims=" %%a in (%1) do ( if !counter! GTR !LastLineNumber! goto next if !counter! GEQ !FirstLineNumber! echo %%a &gt;&gt; %destpath%\%1 set /a counter+=1 ) goto next :next REM echo TEXT TO BE INSERTED &gt;&gt; %destpath%\%1 REM goto bottom_of_file REM The above can be substituted for the rest of :next if you don't have special characters in the text you need inserted set /a FirstLineNumber = 1 REM This is the first line in the file with the text you need inserted in the file you want edited set /a LastLineNumber = %insert_last_line% REM This is the last line in the file with the text you need inserted in the file you want edited SET /a counter=1 for /f "usebackq delims=" %%a in (%insertpath%\insert.txt) do ( if !counter! GTR !LastLineNumber! goto next if !counter! GEQ !FirstLineNumber! echo %%a &gt;&gt; %destpath%\%1 set /a counter+=1 ) REM The %insertpath%\insert.txt is the name of the file with the text you want inserted into the file you want edited goto bottom_of_file :bottom_of_file set /a FirstLineNumber = 1+%top_last_line% REM This is the first line in the second part of the file with the text you need inserted in the file you want edited set /a LastLineNumber = %bot_last_line% REM This is the last line in the second part of the file with the text you need inserted in the file you want edited REM The above is the split, after the top_of_file. The rest of the contents of the original file will be added after the text you want inserted is appended to the file SET /a counter=1 for /f "usebackq delims=" %%a in (%1) do ( if !counter! GTR !LastLineNumber! goto exit if !counter! GEQ !FirstLineNumber! echo %%a &gt;&gt; %destpath%\%1 set /a counter+=1 ) goto logging :logging IF NOT EXIST %insertpath%\Modified_Filelist.txt echo Modified File List: &gt; %insertpath%\Modified_Filelist.txt for /f "tokens=1 delims=[]" %%a in ('find /v /c "" ^&lt; %insertpath%\Modified_Filelist.txt') do ( echo %%a - %1 &gt;&gt; %insertpath%\Modified_Filelist.txt ) goto exit :usage cls echo Usage: InsertString.bat FILENAME echo You are missing the file name in your string :exit IF '%done%'=='T' echo %1 Already exists in folder! IF '%done%'=='T' echo Not modifying %1 IF '%done%'=='T' echo Moving on to next file... IF EXIST %destpath%\InsertString.bat del %destpath%\InsertString.bat IF EXIST %destpath%\insert.txt del %destpath%\insert.txt REM Insert String to XML Script REM Created by Trevor Giannetti REM An unpublished work </code></pre> <p><strong>FILE #3: Insert.txt</strong></p> <pre><code>&lt;Vocabulary="Conv"&gt; </code></pre> <p>In your case you might be able to use 2 files...one with <code>&lt;value&gt;</code> and one with <code>&lt;/value&gt;</code> (I know this is sloppy, but it will work...) Then from my batch script InsertString.bat you would just put the :next loop 2x (one for each of your files) and in between them you would put echo.%userInputFromBeginningofBatch% >> File.xml</p> <p>Like I said, I know this is messy and you can doe it a lot easier in VBScript, but for those of us that don't know it this is a solution that does work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload