Need to copy text from one file to another in batch file - batch-file

Need to copy text from one file to another.
file1 as below:
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
file2 will be
locked/agent.jms.remote.host=mkdfvsh_a-2341
locked/agent.jms.remote.port=1234
& need to replace when creating the second file
command i used:
SET newfile=file2
SEt filetoCOpy=file1
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:<AGENTHOST>=%AGENTHOST%!
SET line=!line:<AGENTPORT>=%AGENTPORT%!
echo !line!>>%newfile%
)
Result i got
locked/agent.jms.remote.host=<AGENTHOST>
locked/agent.jms.remote.port=<AGENTPORT>
variable value not changing.
Can someone help here what's wrong?

You should be getting an error with the code you posted. The < and > are treated as redirection unless they are escaped or quoted:
SET line=!line:^<AGENTHOST^>=%AGENTHOST%!
SET line=!line:^<AGENTPORT^>=%AGENTPORT%!
or
SET "line=!line:<AGENTHOST>=%AGENTHOST%!"
SET "line=!line:<AGENTPORT>=%AGENTPORT%!"
But you still have at least one problem - If you enable delayed expansion within a loop, then you must ENDLOCAL at the end of the loop, otherwise you can run out of SETLOCAL stack space.
Also, I don't see where AGENTHOST or AGENTPORT is defined.

You can try something like that :
#echo off
SET newfile=file2.txt
SEt filetoCOpy=file1.txt
set "ReplaceString1=mkdfvsh_a-2341"
set "ReplaceString2=1234"
If exist %newfile% Del %newfile%
for /f "tokens=*" %%i in (%filetoCOpy%) do (
SET "line=%%i"
SETLOCAL EnableDelayedExpansion
SET line=!line:^<AGENTHOST^>=%ReplaceString1%!
SET line=!line:^<AGENTPORT^>=%ReplaceString2%!
echo !line!>>%newfile%
)
EndLocal
start "" %newfile%

Related

Rename Files using wildcard paths

Recently I started working and my first task is to write a batch file that automatically changes filenames to filename_date with the original file-ending.
For that you should be able to write paths into a textfile (e.g. paths.txt) and when you start the program, it should take any line (=path->file) from there and rename it.
I got it to work on my PC quiet well but as I gave it to testing they asked to make the use of wildcards Z:\Path\*.* possible.
My current code looks as follows:
#echo off
setlocal EnableDelayedExpansion
cd %~dp0
For /F "tokens=*" %%m in (paths.txt) do (
set path=%%~dpm
set name=%%~nxm
pushd "!path!"
dir
For /r !path! %%f in (!name!) do (
set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
set "name=!name!_"
set "name=!name!!date:~6,4!"
set "name=!name!!date:~3,2!"
set "name=!name!!date:~0,2!"
set "name=!name!!ending!"
copy "!datsave!" "!name!"
del "!datsave!"
cls
popd
)
)
I know that a lot of it is probably easier and more efficient to do, but this is my first batch project and I am quiet happy except for the wildcard problem.
So an example would be:
C:\Some\Path\*.*
This line would be in paths.txt.
With the splitting
set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
I get the following:
path: C:\Some\Path
name: C:\Some\Path
ending: -empty-
datsave: C:\Some\Path
because name is set to the Path at the start of the first FOR-Loop. But that seems to be working if I do not use wildcards.
Now the question: Why does this happen and how do I get rid of it? Or do I just use the wrong type of wildcards?
Again: This is my first time I work with batch, so it might be something simple ;)
Ok, I figured out 2 problems and now it works
set name=%%~nxm evaluates the wildcard. Even if name is *.txt it will return bar.txt.
I replaced that by a basename computation instead: set name=!name:*\=! done enough times (not very subtle but hey batch files forces us to do such things) which preserves the wildcard
The other problem is the for /R loop: after pushd, the argument needs to be . or it won't be scanned.
Last minor one: use rename instead of copy plus delete. It preserves file time and is very fast. Copying then deleting a large file can take a long time.
#echo off
set DEPTH=20
setlocal EnableDelayedExpansion
cd %~dp0
For /F %%m in (paths.txt) do (
set pth=%%~dpm
set z=%%m
set name=!z!
rem brutal basename. We cannot break the inner loop or
rem it would break the upper loop too
for /L %%I in (1,1,%DEPTH%) do set name=!name:*\=!
rem but we can check if it is really a basename
set chkname=!name:*\=!
if not !chkname!==!name! ( echo please increase DEPTH value
pause
exit /B)
rem set name=%%~nxm
pushd "!pth!"
For /r . %%f in (!name!) do (
set pth=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!
echo renaming "!datsave!" to "!name!"
rem ren "!datsave!" "!name!"
popd
)
)
paths.txt contains just a line C:\full\path\to\test\*.txt
my test directory contains 2 text files and 1 other file
output:
renaming "bar.txt" to "bar_20160812.txt"
renaming "foo.txt" to "foo_20160812.txt"
(just uncomment the ren line to get the job done)
Weeeeell First of all thanks again to #Jean-François Fabre and #aschipfl for their patience with me :)
After the hint with the second batch file I had to test a few things as not everything worked as fine, but now everything works great!
Code of the Main file:
#echo off
setlocal EnableDelayedExpansion
cd %~dp0
set DEPTH=20
For /F %%m in (paths.txt) do (
pause
set pth=%%~dpm
REM pushd !pth!
REM set origpth=!cd!
REM popd
set z=%%m
set name=!z!
For /L %%i in (1,1,%DEPTH%) do set
name=!name:*\=!
set chkname=!name:*\=!
if not !chkname!==!name! ( echo depth to small
pause
exit /B)
rem set name=%%~nxm
pushd "!pth!"
For /r . %%f in (!name!) do (
pushd %~dp0
call renamefiles.bat %%f REM "!origpth!"
popd
)
)
And the code of the sub-file:
#echo off
REM set pth=%~dp1
REM set origpth=%2
REM set origpth=%origpath:"=%\
REM If !pth!==%origpth% (
set path=%~dp1
set name=%~n1
set ending=%~x1
set datsave=%~nx1
pushd !path!
set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!"
pause
echo renaming "!datsave!" to "!name!"
rem "!datsave!" "!name!"
cls
popd
REM )
EDIT: After testing around a bit I figured, that subfolders are included as well! I put extra code to both codes marked with REM and two extra spaces. Take out those REM's and the programm will not longer include subfolders when renaming :)

Replace entire line cmd

I have a file where I search for a specific text and want to replace it with another. For example:
<role-name>test</role-name>
this is for example what I want to replace it with:
<role-name>file</role-name>
The problem here would be that in the <role-name> tag might be other text then "test".
How can I find the whole line and replace it with the text that I need?
Or maybe I can retrieve the line number of the tag and replace it whole, but again I have no idea how to do that :-)
The key to doing this without external code is using the string substitution expression. I assume you want to parameterize the tag and value strings. This would also depend on the sequence not having any newline characters.
C:>type seb.bat
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET EXITCODE=0
SET "TAG=%~1"
SET "FROMVAL=%~2"
SET "TOVAL=%~3"
FOR /F "usebackq tokens=*" %%a IN (`TYPE "seb.html"`) DO (
echo %%a
SET S1=%%a
SET S2=!S1:^<%TAG%^>%FROMVAL%^</%TAG%^>=^<%TAG%^>%TOVAL%^</%TAG%^>!
ECHO !S2!
)
EXIT /B %EXITCODE%
C:>type seb.html
<role-name>test</role-name>
C:>seb role-name test file
<role-name>test</role-name>
<role-name>file</role-name>
<role-name></role-name>
<role-name></role-name>
C:>seb role-name test ""
<role-name>test</role-name>
<role-name></role-name>
<role-name></role-name>
<role-name></role-name>
C:>seb role-name "" "file"
<role-name>test</role-name>
<role-name>test</role-name>
<role-name></role-name>
<role-name>file</role-name>
The problem was that I was reading from a file and the string that I wanted to compare came in a for loop. This is my code that reads from file and writes the code that I needed, if anyone needs it :
set "nameofapp=%<display-name>"
set "nameofapp_country=% <display-name>ASD</display-name>"
set "rolename=%<role-name>"
set "rolename_country=% <role-name>ASD-role</role-name>"
set "propertiesfile=%<env-entry-value>"
set "properties_file=% <env-entry-value>java-asd.properties</env-entry-value>"
set "web=src\main\webapp\WEB-INF\web.xml"
set "web_1=web.xml"
(for /f "delims=" %%i in (%web%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "name=!line:%nameofapp%=%!"
if not !name! == !line! (
set "line=%nameofapp_country%"
)
set "role=!line:%rolename%=%!"
if not !role! == !line! (
set "line=%rolename_country%"
)
set "property=!line:%propertiesfile%=%!"
if not !property! == !line! (
set "line=%properties_file%"
)
echo(!line!
endlocal
))>"%web_1%"
del %web%
Move %web_1% "src\main\webapp\WEB-INF\"

Reading a csv file with batch to create users

I'm an IT student. I was trying to make users in my virtual machine of windows server 2008, i though that should be a better way to make them less than make it one by one, then i found that i can make users with a database, in that case csv, and a loop script. But cause my low level of knowledge about commands and batch files i can't achieve my goal. This is what i have in the csv file:
Pedro,12345,939293c
Juanjo,23456,213123v
Eufrasio,34567,2131312b
Dani,45678,123213n
Pepe,56789,12344v
Manolo,67891,12312567b
And this is the batch I've made, I'm stuck in the part of the echo, the command didn't read
SET LOCAL
#echo incio del script
set n1=
set n2=
set n3=
for /F "tokens=1,2,3* delims=," %%i in (Libro2.csv) do (
set "n1=%%i"
set "n2=%%j"
set "n3=%%k"
echo %n1 %n2 %n3
)
pause
Variables in batch are referenced with %var%, not %var.
But in blocks (between ( and ) you need to enable delayed expansion to show the current value (if you change them inside the block):
setlocal enabledelayedexpansion
set x=one
if 1==1 (
set x=two
echo %x% !x!
)
change your script a little bit:
SETLOCAL enabledelayedexpansion
#echo incio del script
set n1=
set n2=
set n3=
for /F "tokens=1,2,3 delims=," %%i in (Libro2.csv) do (
set "n1=%%i"
set "n2=%%j"
set "n3=%%k"
echo %%i %%j %%k
echo !n1! !%n2! !%n3!
)
(Note: you can directly work with the for-variables - see the first echo in the block)
(Note: tokens=1,2,3,* means: for the fourth token take the rest of the line. You don't really need it here, as you don't use %%l)

Add Date/time/old location to file name Batch

I have the following code:
#echo off
setlocal disableDelayedExpansion
set "src=C:\test"
set "dst=C:\test2"
set "search=test"
for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal enableDelayedExpansion
copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!"
endlocal
)
what this code does is copy a file from 1 location and put it on other location and change the name and put date/time in the filename.
Where i was looking for is that behind the filename and time also comes the location of the copyed file. so something like this:
Filename-10-03-2014-15:58:45-C:\test\test1\testfile.txt
so i can see the date time and old path in my filename.
Hope you guys can help.
Kind regards,
Kaluh
#echo off
setlocal disableDelayedExpansion
set "src=C:\sourcedir"
set "dst=C:\destdir"
set "search=jpg"
for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal ENABLEDELAYEDEXPANSION
SET "appendix=!full::=-!"
SET "appendix=!appendix:\=_!"
ECHO copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!-!appendix!"
endlocal
)
GOTO :EOF
I changed the search pattern, source and destination directories to suit my system.
You would need to nominate your own substitute characters for : and \ which cannot appear in a filename.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.

Batch file to create multiple variables from single lines in a text file

I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)

Resources