将文件的路径位置更改为所需的路径

Lit*_*ird 8 batch-file

我想将定义到文件中的内容路径(即logging.properties)替换为jboss7位置的所需位置路径.

基本上我使用安装程序,我必须浏览我的jboss7文件夹,并将其定位到用户的任何所需位置.但是在jboss7的少数文件中,有一些硬编码路径被定义为在给定的logging.properties文件中.

我需要将该硬编码路径更改为所需的位置路径.

截至目前,我在同一个文件夹中有repl.bat和文件test.bat文件.

repl.bat帮助文件可以在以下链接中找到: -

http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

我刚刚复制了代码并创建了repl.bat文件.

test.bat文件: -

 @ECHO OFF
 SETLOCAL
 SET "folder="
 FOR /r "C:\" %%a IN (tintin.txt) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
 FOR /r "D:\" %%a IN (tintin.txt) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
 :got1
 echo "%folder%"
 PAUSE

 set "newpath=%folder%"
 set "newpath=%newpath:\=\\%"
 echo "%newpath%"
 PAUSE
 type "logging.properties" | repl "(Directory=).*(\\\\standalone\\\\)" "$1%newpath%$2">"logging.properties.tmp"
 PAUSE
 move "logging.properties.tmp" "logging.properties"
 PAUSE
 GOTO :EOF
 PAUSE
Run Code Online (Sandbox Code Playgroud)

在这个test.bat文件中,我正在搜索文件tintin.txt文件并将路径设置为变量名称为'folder'.tintin.txt文件就在jboss7的文件夹里面.这是因为有多个jboss7应用服务器文件夹进入系统的可能性.直到现在我有了路径即"C:\ Users\Anuj\Desktop\jboss7 \"并设置为变量'folder'.现在有一个名为logging.properties的文件到文件夹位置C:\ Users\Anuj\Desktop\jboss7\standalone\configuration

logging.properties: -

 com.latilla.import.uploadDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\ standalone\\uploads
 com.latilla.import.maxFilesUploadNumber=10


com.latilla.export.templateFile=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\templates\\GDV_HDI_Format.xls
com.latilla.etl.pluginsRootDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\cloverETL\\plugins

 com.latilla.etl.templatesDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\etl

 com.latilla.db.user=postgres
 com.latilla.db.pass=password
Run Code Online (Sandbox Code Playgroud)

repl.bat帮助文件有助于用所需的路径替换url路径,即路径设置为变量名'folder'.我想用路径设置为变量名'folder'来替换C:\ progra~2\Latilla\C4i\jboss7 \. 注意: - 这里的logging.properties文件路径内容具有不同的路径格式,即C:\表示双斜杠.\

可能是我尝试过的脚本test.bat不正确.当我双击test.bat文件时,我收到了错误.

And*_*y M 3

Although I can't help you with fixing the issue you are getting while using the repl.bat file, I can suggest a different way of solving the initial problem of path replacement.

If the jboss7 string is guaranteed to be present in all the original paths in your configuration file(s), you could try the following approach:

@ECHO OFF
SETLOCAL DisableDelayedExpansion
FOR /F "delims=" %%A IN ('DIR /B /S C:\tintin.txt') DO (CD /D "%%~dpA" & CALL :got1)
FOR /F "delims=" %%A IN ('DIR /B /S D:\tintin.txt') DO (CD /D "%%~dpA" & CALL :got1)
GOTO :EOF

:got1
SET "propfile=%CD%\standalone\configuration\logging.properties"
IF NOT EXIST "%propfile%" GOTO :EOF
SET "tempfile=%TEMP%\logging.properties.tmp"
FIND /I /V "jboss7\\" >"%tempfile%"
>>"%tempfile%" (
  FOR /F "tokens=1,* delims=" %%I IN ('FIND /I "jboss7\\"') DO (
    SET "pathname=%%J"
    SETLOCAL EnableDelayedExpansion
    IF NOT "!pathname!" == "!pathname:*jboss7\\=!" (
      SET "pathname=%__CD__:\=\\%!pathname:*jboss7\\=!"
    )
    ECHO %%I=!pathname!
    ENDLOCAL
  )
)
ECHO Old file "%propfile%":
TYPE "%propfile%"
ECHO =======================================
ECHO New file:
TYPE "%tempfile%"
PAUSE
:: uncomment the next line once you have verified the replacement works correctly
::MOVE "%tempfile%" "%propfile%"
Run Code Online (Sandbox Code Playgroud)

Searching for the tintin.txt file has been changed slightly so as to possibly make the process faster. Instead of iterating over every directory and checking if it contains the file, the loops now read the output of DIR, which returns only actually existing entries.

Note that you could also use a FOR /R loop, as in your present code, with the same effect i.e. returning only existing paths, but the IN clause would need to contain a mask rather than a normal name, but that would have to be a mask that couldn't match anything else in your system than just tintin.txt. For instance, if you knew for certain that there could be no file called tintin.txt1 or tintin.txtx or anything else where tintin.txt is followed by exactly one character, you could use the following template instead:

FOR /R "C:\" %%A IN (tintin.txt?) DO (CD /D "%%~dpA" & CALL :got1)
Run Code Online (Sandbox Code Playgroud)

and same for D:\. That would return only references to files actually existing and matching the mask.

Also, you can see that the loops do not jump (GOTO) to the got1 label but instead call the got1 subroutine. With that change, it is possible to process many application instances in one go. I don't know yours can be installed multiple times. If not, you'll probably want to change it back to GOTO.

The subroutine in my script is referencing the config file using its full path as specified in your description (...\standalone\configuration\logging.properties). For some reason, in your script the file is referenced simply by its name, even though there's no preceding CD or PUSHD command changing the current directory to the location of the file. I assumed you were trying to simplify your script and omitted that bit, whether intentionally or not. Otherwise I may have missed something in your explanation and/or script.

After verifying that the config file exists at the expected location, the replacement itself is done in this way:

  1. All the non-path config lines are written to a temporary file with one go.

  2. Every config line containing a path is processed in this way:

    • if it does not contain the jboss7\\ string, it is omitted;

    • otherwise the part of the path up to and including jboss7\\ is removed;

    • the current directory is inserted before the remaining part (after every \ is replaced with \\);

    • the new value is put back into the config line;

    • the update line is added to the same temporary file.

  3. The old version is of the configuration file replaced with the new one.

显然,脚本可能会更改已处理文件中的行顺序,但假设并不重要。