I need to change the date in a batch file, this line is a parameter for backup software.
The file with the paramentros:TEXT.INI
[indexa]
testindex=
[Restaurante_Geral]
FWHCOMPPREMEDIO=
[backup]
ultbackp=05/08/13
I need to change the line ultbackp=05/08/13, to current date.
I use the batch file below, but does not change the date if the parameter is already set.
#echo off
SETLOCAL=enabledelayedexpansion
SET YY=%DATE:~8,2%
SET MM=%DATE:~3,2%
SET DD=%DATE:~0,2%
rename text.ini text.tmp
for /f %%a in (text.tmp) do (
set foo=%%a
if !foo!==ultbackp set foo=ultbackp=%DD%/%MM%/%YY%
echo !foo! >> text.ini)
del text.tmp
Can anyone help me?
try this:
if "!foo:ultbackp=!" neq "!foo!" set "foo=ultbackp=%DD%/%MM%/%YY%"
ultbackup is not set to anything. Therefore your compare is failing.
Related
I'm new to batch scripting and need help here.
My file name along with path is
C:\test\My_Test_File_20201006.txt
and I want to rename it as
C:\test\My_File_20201006.txt
using batch script only. I cannot use PowerShell here.
#echo off
set Pattern="Test_File"
set Replace="File"
Rem accepts the filename as cmd line argument
set filename=%1
Rem Update filename
set targetfile=%filename:Pattern=Replace%
Rem Rename the file
Ren %filename% %targetfile%
Exit
Using the above code, My file is renamed as "File". Tried % around the Pattern & replace variables, but no luck. Not sure where I'm going wrong. Tried all possible solutions from the StackOverflow and other tutorials, but none helped.
Edit:
After the proposed solution, getting a syntax error. The code is as below:
#echo off
set "filename=%~nx1"
echo %filename%
echo "%~dp1"
echo "%~dp1%filename:statement_=%"
ren "%~dp1%filename%" "%~dp1%filename:Test_=%"
I call my script from cmd line as:
D:/Test> C:/script/rename.bat C:\test\My_Test_File_20201006.txt
The echo statement correctly prints filename, directory & filename with the directory. Facing issues in rename statement.
Output:
My_Test_File_20201006.txt
"C:\test\"
"C:\test\My_Test_File_20201006.txt"
The syntax of the command is incorrect.
Three things wrong here.
You cannot add the quotes as part of the variable's value. It will actually use them as part of the variable. set variables to have double quotes including the variable name. For instance instead of set Pattern="Test_File" rather do set "Pattern=Test_File"
You never used the variables you've set Replace and Pattern
You either need to enabledelayedexpansion or use call to do the replacement because of the multple % required.
#echo off
set "Pattern=Test_File"
set "Replace=File"
Rem accepts the filename as cmd line argument
set "filename=%~nx1"
Rem Update filename
setlocal enabledelayedexpansion
ren "%~dp1%filename%" "!filename:%Pattern%=%Replace%!"
Another method, seeing as you only replace Test_ in your example:
#echo off
set "filename=%~nx1"
ren "%~dp1%filename%" "%filename:Test_=%"
EDIT
Fixing your example as per the edit.
#echo off
set "filename=%~nx1"
echo %filename%
echo "%~dp1"
echo "%~dp1%filename:statement_=%"
ren "%~dp1%filename%" "%filename:Test_=%"
i have one property file where i need to update the more than one value in property file using batch . i have using the script
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
call %~dp0\Batchenv.bat
SET sourcedir=C:\Users\1026478\Desktop\local
(
FOR /f "usebackqdelims=" %%a IN ("%sourcedir%\test.Property") DO (
FOR /f "tokens=1*delims==" %%g IN ("%%a") DO (
IF /i "%%g"=="INSTALL_PROD" (ECHO(%%g=%INSTALL_PROD%
) ELSE (ECHO(%%a)
)
)
)>newfile.property
:: newfile.txt now contains a modified version.
:: This line will overwrite the original
MOVE /y newfile.Property "%sourcedir%\test.Property"
GOTO :EOF
but this script only update one value in property file.. i need to update more than one value .
my property file like
test.property
I2_JDK_HOME=D:\\Java8\\jdk1.8.0_181
I2_LICENSE_FILE=D:\\license\\PSA100417.lic
I2_LICENSE_FILE_LOCATION=D:\\license
I2_LICENSE_FILE_NAME=PSA100417.lic
I2_ORACLE_DRIVER=oracle.jdbc.driver.OracleDriver
I2_ORACLE_HOME=D:\\Oracle64\\product\\12.1.0\\client_1
I2_ORACLE_HOST=sl1psadevdb2v.jdadelivers.com
I2_ORACLE_INFO_1=ABPPMGR
I2_ORACLE_INFO_2={E:AES}6AA9880B2F94C92CA09E7665CC4AA76B
I2_ORACLE_INFO_2_XOR=#CQQLFS
I2_ORACLE_INFO_3=PSADVDB
I2_ORACLE_INFO_4=
I2_ORACLE_JAR=D:\\Oracle64\\product\\12.1.0\\client_1\\jdbc\\lib\\ojdbc7.jar
I2_ORACLE_PORT=1521
I2_PLATFORM_HOME=
I2_WEBCLIENT_TYPE=NoWeb
I2_WEBLOGIC_BASE_APP=D:\\JDA\\JDA2017_3\\config\\JDAv2017_SEMs1\\web\\base
I2_WEBLOGIC_DOMAIN_HOME=C:\\bea\\user_projects\\domains\\mydomain
I2_WEBSPHERE_APPNAME=
I2_WEBSPHERE_APPNAME_BASE=base
I2_WEBSPHERE_CONTEXTROOT=
I2_WEBSPHERE_CONTEXTROOT_BASE=/base
I2_WEBSPHERE_HOME=C:\\Program Files\\IBM\\WebSphere\\AppServer
I2_WEBSPHERE_INSTALLABLE=
I2_WEBSPHERE_NODE=
I2_WEB_SERVER_PORT=22246
INSTALLER_UI=SWING
USER_INSTALL_DIR=D:\\JDA\\JDA2017_3\\config\\JDAv2017_SEMs1
i need to update all the value..
can anyone pls help me on this
[Edit /]
The variable INSTALL_PROD is set in another batch-file file.
Batchenv.bat:
echo off
SET sourcedir=C:\Users\1026478\Desktop\local
SET INSTALL_PROD=Prod
SET APPSERVER_PORT2=44444
SET /a ABPP_BRE_PATH=D:\\JDA\\JDA2017_3\\config\\JDAv2017_SEMs1\\bre
The information you wish to input into the new file should be Appended, not overwitten
To append Information use >> instead of >
For Example, If you wish to add the value of %%a into the file, you would:
Echo(%%a>>newfile.property
How do I get a Batch file to store data? I've tried using Text files and Dat files but no luck. Could anyone help me out?
I tried this for storing a name:
echo %name% >name.txt
but I'm finding it hard making it extract the name data and printing it in a batch file.
Another way is to save the data like this :
#echo off
set "name=Jaden"
>name.txt echo name=%name%
And to get the value again. Just evaluate the line(s) in name.txt
like this :
#echo off
for /f "delims=" %%a in (name.txt) do set %%a
echo name --^> %name%
First set the value and then if you echo to a file redirect, it will send the content value to a file.
set name=Jaden
echo %name% > name.txt
then it will work.
Just use set /p name=<name.txt
I need help to create a batch file to copy files with specifc date and version. And my date will be as of yesterday. Example file name:- abcde-20150811-v1.csv
I have tried xcopy with /d:08-11-2015 ( it picks all files with date modified as 08-11-2015 MM-DD-YYYY)
Is there a way that my batch automatically picks the date and gets changed everyday.
You might take a look at this: How to get and display yesterday date?
If I were you, I'd go with a scripting language like Perl, or use PowerShell.
Try this piece of code, haven't tried it yet though.
#echo off
set completepath=c:\users\microsoft\desktop\source
set destination=c:\users\microsoft\desktop\destination
set /a yesterday=%date:~4,2% - 1
set yesterday_date=%date:~10,2%%date:~7,2%%yesterday%
FOR /R %completepath% %%G IN (*.csv) DO call :process "%%~dpG" "%%~nG"
pause >nul
:process
SET %name%=%~2
SET chkname=%name:*%yesterday_date%=?%
IF "%chkname:~0,1%"=="?" (
xcopy %~1 %destination% /y
)
You may change the completepath and the destination variable.
I have a txt file naemd as (settings.txt )which has content
SET BACKUP_DRIVE=E:\
SET BACKUP_DIRECTORY=BACKUP\
SET HOURLY_DIRECTORY=HOURLY\
SET INPUT_DIRECTORY=D:\MySQL\Data\CDR\.
I have a bat file where I want to use these variables for prepare backup path
SET BACKUP_PATH=%BACKUP_DRIVE%%BACKUP_DIRECTORY%%HOURLY_DIRECTORY%%CURRENT_HOUR%\
But I am not getting prepared path.
I have tried
type settings.txt in the bat file
It is printing content of setting file but not implementing it..showing echo is off..
if I do echo on then some prob also.
Please tell me how to use these variables
Thanks
Try this:
#echo off&setlocal
:: set CURRENT_HOUR for testing
set "CURRENT_HOUR=03"
for /f "delims=" %%i in (settings.txt) do %%i
SET "BACKUP_PATH=%BACKUP_DRIVE%%BACKUP_DIRECTORY%%HOURLY_DIRECTORY%%CURRENT_HOUR%\"
echo %BACKUP_PATH%
Output is:
E:\BACKUP\HOURLY\03\