Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the path location of a file to the desired path

Tags:

batch-file

I want to replace the content paths defined into the file i.e logging.properties to the desired location path of the jboss7 location .

Basically i'm using installer where i have to browse my jboss7 folder and locate it to any desired location of the user . But in few files of jboss7 there are some hardcoded path defined like in given logging.properties file.

I need to change that hard coded path to the desired location path.

As of now i'm having repl.bat and file test.bat files in the same folder.

repl.bat helper file could be find in following link:-

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

I just copied the code and created repl.bat file.

test.bat file :-

 @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

Here in this test.bat file , i'm searching a file tintin.txt file and setting the path into a variable name as 'folder'. tintin.txt file is just inside the folder of jboss7.This is because of the possibilities of more than one jboss7 application server folder into the system. Till now i have got the path i.e "C:\Users\Anuj\Desktop\jboss7\" and set into the variable 'folder'. Now there is file named logging.properties into the folder location 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

repl.bat helper file helps to replace the url path with the desired path i.e path set to variable name 'folder'. I want to replace the C:\progra~2\Latilla\C4i\jboss7\ with the path set to variable name 'folder'. Note :- here in logging.properties file path contents is having different format of path i.e C:\ means double slash. \

Might be the script that i have tried test.bat is incorrect. When i double click the test.bat file i got error.

like image 627
Little bird Avatar asked Sep 20 '13 09:09

Little bird


1 Answers

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%"

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)

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.

Obviously, the script may change the order of lines in the processed file, but it is assumed that that doesn't matter.

like image 94
Andriy M Avatar answered Oct 04 '22 01:10

Andriy M