Я на самом деле быть_наст ответ на это. Да, это больно, однако у меня была аналогичная проблема, и я действительно не знаю VBScript (хотя я планирую изучить ее ...) на время, хотя моя проблема возникла с коллегой, у которой был клиент с 20 000 файлов, которые они вырвали из преобразование внешних данных. Все файлы были xml, и все они пропускали одну и ту же вторую строку XML, которая вызвала рефикс документа, который мы импортировали.
Я написал стандартный пакетный скрипт в тандеме с другим, который я нашел в StackOverflow, который позволил мне разбить файлы на 2 части, а затем между ними вставить код, который я хотел. Теперь моя единственная проблема (возможно, из-за лени или моего отсутствия знания/терпения) заключалась в том, что я не мог избежать проблемы <. Сценарий продолжал думать, что я пытался записать файл, что было неверно. Я пробовал всевозможные способы использования этого символа, но я хотел его в переменной форме. Само собой разумеется, я получил его работу (хорошо даже) ...
Ниже приведено мое письмо моему коллеге вместе с кодом из каждого файла.
README.txt Проблема: Массивная количество файлов отсутствовали строку или фрагмент кода, который должен быть отредактирован
Решение: Этот инструмент разбирает файлы и впрыскивает строку или кусок кода а затем поместите файлы обратно в другое место.
Всего 4 файла, которые поставляются с этим инструментом.
**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.
Что вам нужно сделать:
Редактировать String_Insert_Launcher и поместить этот файл в директории с файлами, которые вы хотите изменить. ПРИМЕЧАНИЕ Настоятельно необходимо, чтобы этот файл находился в той же папке, что и ВСЕ остальные ваши файлы, которые вы хотите отредактировать. Вам необходимо отредактировать переменные в этом файле, чтобы соответствовать вы файловой batchpath
Редактировать InsertString.bat и поместить этот файл в том же каталоге вы установите переменную batchpath выше Вам необходимо отредактировать переменные в этом файле в сопрягать файловую систему insertpath DestPath top_last_line insert_last_line bot_last_line
Редактировать insert.txt и поместить этот файл в том же каталоге, установить insertpath выше Вам нужно поместите строки (строки), которые вы хотите вставить в свой файл внутри этого текстового документа.
Проверьте свои журналы и убедитесь, что количество файлов в файле «Modified_Filelist.txt» (найдено в папке% insertpath%) так же, как и количество файлов, с которых вы начали.
Структура файлов:
* insert.txt *
В этом файле вы хотите поместить текст, который вы хотите вставить в файлы, которые вы будет нацелена. Причина использования отдельного файла заключается в том, что специальные символы (>, <, /, \, |, ^,% и т. Д.) Не обрабатываются как аргументы в пакетном файле. Этот файл должен находиться в том же месте, что и переменная, которую вы установите в InsertString.bat с именем «insertpath» или ссылаетесь в пакетном файле как% insertpath%.
* InsertString.bat *
В этом файле вы найдете переменные, которые должны быть установлены для работы сценария. Переменные включали:
**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)
Этот файл должен быть в том же месте, что и переменная, которую установят в String_Insert_Launcher.bat под названием «batchpath» или ссылка в пакетном файле как% batchpath%.
* String_Insert_Launcher.bat *
Это скрипт, который вы будете выполнять, чтобы редактировать все файлы. Запустите этот пакетный скрипт из папки с файлами, которые вы хотите редактировать. Этот файл захватывает все имена файлов и запускает InsertString.bat во всех этих файлах. Внутри этого файла вы найдете varaible, который должен быть установлен для работы скрипта. Переменная включена: batchfilepath
- Это местоположение фактического командного файла, который выполняет всю работу. Это местоположение JUST путь к файлу, не включая имена файлов.
ФАЙЛ # 1: String_Insert_Launcher.bat
@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
ФАЙЛ # 2: Insert_String.bat
@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 >> %destpath%\%1
set /a counter+=1
)
goto next
:next
REM echo TEXT TO BE INSERTED >> %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 >> %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 >> %destpath%\%1
set /a counter+=1
)
goto logging
:logging
IF NOT EXIST %insertpath%\Modified_Filelist.txt echo Modified File List: > %insertpath%\Modified_Filelist.txt
for /f "tokens=1 delims=[]" %%a in ('find /v /c "" ^< %insertpath%\Modified_Filelist.txt') do (
echo %%a - %1 >> %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
ФАЙЛ # 3: Insert.txt
<Vocabulary="Conv">
В вашем случае вы могли бы использовать 2 файла ... один с <value>
и один с </value>
(Я знаю, что это неряшливо, но он будет работать ...) Затем из моего командного скрипта InsertString.bat вы бы просто поставьте следующий цикл 2x (по одному для каждого из ваших файлов), а между ними вы будете помещать эхо.% userInputFromBeginningofBatch% >> Файл.xml
Как я уже сказал, я знаю, что это грязно, и вы можете сделать это намного проще в VBScript, но для тех из нас, кто этого не знает, это решение, которое действительно работает.
Вы правы, это невозможно при использовании стандартных окон командами партии. –
его возможно. просто вам понадобится больше времени и усилий – ghostdog74
Ну, проблема с цитированием/побегом в настоящее время не имеет решения для меня. Я не могу прочитать строку, такую как 'a | b" <> & ', и записать ее в файл без изменений. Непревзойденные кавычки приведут к отказу обычных механизмов экранирования (' set "foo =% foo: & =^&%" ') – Joey