Batch Script How to Set Variabes into another variable - batch-file

i asked a question but it was not fully Explained so that is my Question.
i got settings.ini inside it There is sections with variables
[HideAndSeekEvent]
EventRunning=[Hide And Seek Event] Has started at [ %placenameHS% ].
and got another batch file called ini.bat
#setlocal enableextensions enabledelayedexpansion
#echo off
set file=%~1
set area=[%~2]
set key=%~3
set currarea=
for /f "usebackq delims=" %%a in ("!file!") do (
set ln=%%a
if "x!ln:~0,1!"=="x[" (
set currarea=!ln!
) else (
for /f "tokens=1,2 delims==" %%b in ("!ln!") do (
set currkey=%%b
set currval=%%c
if "x!area!"=="x!currarea!" if "x!key!"=="x!currkey!" (
echo !currval!
)
)
)
)
endlocal
and another bat file called Getdata.bat to call variables from .ini file and for this section there is
for /f "delims=" %%a in ('call ini.cmd settings.ini HideAndSeekEvent EventRunning') do (
set EventRunningeHS=%%a
)
in my main .bat file iam calling the Getdata.bat file
in my main .bat i got some thing like this
SET placenameHS=Hotan
echo %EventRunningeHS%
it should show
[Hide And Seek Event] Has started at [ Hotan ].
But it show :
[Hide And Seek Event] Has started at [ %placenameHS% ].
[NOTICE] i do not SET The Place name i got it by SQLCMD Query
osql -S %Server% -U %SQLUser% -P %SQLPass% -d Dother_Events -Q "SET NOCOUNT ON; SELECT Place_Name FROM _HideANDSeek_Map WHERE ID = %IDHS%" %osqluser% -b -w 9999 -h-1 -o .\Logs_in_use\HSLN7.txt
set /p HSLN7=<.\Logs_in_use\HSLN7.txt

I tested the code and data you posted, and the proposed solution. These are the files I used in the test.
settings.ini:
[HideAndSeekEvent]
EventRunning=[Hide And Seek Event] Has started at [ %placenameHS% ].
ini.cmd:
#setlocal enableextensions enabledelayedexpansion
#echo off
set file=%~1
set area=[%~2]
set key=%~3
set currarea=
for /f "usebackq delims=" %%a in ("!file!") do (
set ln=%%a
if "x!ln:~0,1!"=="x[" (
set currarea=!ln!
) else (
for /f "tokens=1,2 delims==" %%b in ("!ln!") do (
set currkey=%%b
set currval=%%c
if "x!area!"=="x!currarea!" if "x!key!"=="x!currkey!" (
echo !currval!
)
)
)
)
endlocal
Getdata.bat:
#echo off
for /f "delims=" %%a in ('call ini.cmd settings.ini HideAndSeekEvent EventRunning') do (
set EventRunningeHS=%%a
)
main.bat:
#echo off
call Getdata.bat
SET placenameHS=Hotan
echo Original output:
echo %EventRunningeHS%
echo Proposed solution:
call echo %EventRunningeHS%
This is the output when I run main.bat:
Original output:
[Hide And Seek Event] Has started at [ %placenameHS% ].
Proposed solution:
[Hide And Seek Event] Has started at [ Hotan ].

Related

Checking if an array element(file) has a specific extension

I'm trying to write a code that does the following. I have some files in a directory with specific extensions. I have made a vector that contains all of them. Now I will want to rename each file to something else depending on their extension. So for that I'm trying to parce the created vector with a for loop in which I check for each element extension.
For now I won't rename it just echo it on the screen if the file with the .elf extension is found. I wrote this code but I get no echo as in there would be no .elf file in my directory. Please help me correct this. Thanks.
#echo off
setlocal enabledelayedexpansion
cd C:\Users\uidr0938\Desktop\Copy
set path=C:\Users\uidr0938\Desktop\Copy
set /a index=0
for /r %%i in (*) do (
set value[!index!]=%%i
set /a index+=1
)
set /a limit=%index%-2
for /l %%a in (0;1;%limit%) do (
if !value[%%a]! equ *.elf (
echo !value[%%a]!
)
)
endlocal
try with :
....
for /l %%a in (0;1;%limit%) do (
if "!value[%%a]:~-4!" equ ".elf" (
echo !value[%%a]!
)
)
when comparing string you cannot use wildcards.Here you can see some examples about batch substrings
Here is a slightly different way of doing it.
#IF NOT EXIST "%USERPROFILE%\Desktop\Copy\" #EXIT/B
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_path=%USERPROFILE%\Desktop\Copy"
SET "_index=0"
FOR /F "DELIMS=" %%A IN ('WHERE/R "%_path%" * 2^>NUL') DO (SET/A "_index+=1"
SET "_value[!_index!]=%%A")
IF %_index% EQU 0 EXIT/B
FOR /F "TOKENS=1* DELIMS==" %%A IN ('SET _value['
) DO IF /I "%%~xB"==".elf" ECHO %%B
PAUSE

Storing your local IP in an environment variable with a batch file

I am trying to store my local ip into a system variable but I am struggling with that batch file. I am already able to parse my IP and extract the right substring. However declaring the variable and passing it to SETX seems to be harder than I thought....
Any help would be very much appreciated. :)
#echo off
setlocal enabledelayedexpansion
::just a sample adapter here:
set "adapter=Ethernet-Adapter VirtualBox Host-Only Network"
set adapterfound=false
echo Network Connection Test
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
set "item=%%f"
if /i "!item!"=="!adapter!" (
set adapterfound=true
) else if not "!item!"=="!item:IPv4-Address=!" if "!adapterfound!"=="true" (
echo %%g | cut -d"(" -f 1 | tr -d "[:space:]" <-- this echos my ip correctly
set ipadress=%%g | cut -d"(" -f 1 | tr -d "[:space:]"
echo %ipadress% <-- this echos empty string
setx MY_IP %ipadress% <-- this doesnt work as well
set adapterfound=false
)
)
Improved script (might be locale-dependent):
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
::just a sample adapter here:
set "adapter=Ethernet-Adapter VirtualBox Host-Only Network"
set "adapterfound=false"
echo Network Connection Test
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
set "item=%%f"
if not "!item!"=="!item:%adapter%=!" (
set "adapterfound=true"
echo %adapter%
) else if not "!item!"=="!item:IPv4 Address=!" if "!adapterfound!"=="true" (
rem ↑↑↑↑↑↑↑↑↑↑↑↑ might be locale-dependent
for /F "delims=( " %%G in ("%%g") do set "ipadress=%%G"
echo !ipadress!
rem setx MY_IP !ipadress!
set "adapterfound=false"
)
)
echo %ipadress%
Another approach:
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
:: just a sample adapter here:
set "adapter=Ethernet-Adapter VirtualBox Host-Only Network"
echo Network Connection Test
:: get adapter index
set "_where=where "NetConnectionID = '%adapter%'""
for /F "tokens=1,* delims==" %%G in ('
wmic path Win32_NetworkAdapter %_where% get InterfaceIndex /value ^| findstr "="
') do for /F %%g in ("%%H") do set "_ii=%%~g"
:: get adapter's IP addresses
set "_where=where "InterfaceIndex = '%_ii%'""
for /F "tokens=1,* delims==" %%G in ('
wmic path Win32_NetworkAdapterConfiguration %_where% get IPAddress /value ^| findstr "="
') do for /F "tokens=1,2 delims={,}" %%g in ("%%H") do (
set "_IPv4=%%~g"
set "_IPv6=%%~h"
)
set _ip
Here the for loops are
%%G to retrieve the desired value;
%%g to remove the ending carriage return in the value returned: wmic behaviour: each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>).
See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem

"The system cannot find file" error in batch script

I'm trying to set the contents of a file to my variable 'reportPath'. The variable 'latestReport' holds the path to the file. When I run this command
for /F %%z in (%latestReport%) do set "reportPath=%%z"
I get the error mentioned in the title of this question ("The system cannot find file"). However, if I replace %latestReport% with the path to the file, the code executes perfectly.
How do I get the code to read my %latestReport% variable as a filename?
Here is the whole script:
#echo off
setLocal Enabledelayedexpansion
set "subject="
set "reportPath="
if "%1"=="" (
for /F "delims== tokens=2" %%a in ('findstr /B test_report_directory my_file_here.properties') do set testReportDir=%%a
set "subject=RentalMan Test Execution Results"
goto :runemaildriver
)
set testReportDir=%~f1
if not exist %testReportDir% (
:: finds string test_report_directory in config file
for /F "delims== tokens=2" %%a in ('findstr /B test_report_directory my_file_here.properties') do set "testReportDir=%%a"
) else (
shift
)
if "%1"=="" (
set "subject=RentalMan Test Execution Results"
) else (
for %%a in (%*) do (
if not exist %%a (
set "subject=!subject!%%a "
)
)
set "subject=!subject:~0,-1!"
)
:runemaildriver
echo testReportDir: %testReportDir%
set latestReport=%testReportDir%\currrentReportPath.txt
echo latestReport: %latestReport%
for /F %%z in (%latestReport%) do set "reportPath=%%z"
echo reportPath: %reportPath%
for /F "delims=: tokens=2" %%d in ('findstr \B Environment %reportPath%') do set "env=%%d"
echo env: %env%
echo Emailing latest TestReport in %testReportDir% with subject line "%subject%"...

Batch File: FOR /F doesn't work as expected

I have an issue with reading lines from a *.txt file in batch script to get a list of files.
If my file contain something like
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
2 errors.
and my batch is
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set count_to_sync=-1
for /f "tokens=*" %%i in (bubu.txt) do (
set line=%%i
echo %%i
if "!line:~0,9!" == "MD5 FAIL:" (
set /A count_to_sync+=1
set list[!count_to_sync!]=!line:~10!
)
if "!line:~0,20!" == "File does not exist:" (
set list[!count_to_sync!]=!line:~21!
set /A count_to_sync+=1
)
)
IF "%count_to_sync%" == "-1" (
ECHO Nothing to sync
) ELSE (
ECHO Files to sync
for /F "tokens=2 delims==" %%s in ('set list[') do (
echo %%s
)
)
The output is
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme.txt
2 errors.
Files to sync
release\devpath\readme.txt
release\mainline\readme.txt
and the '!!!' from second line is missing.
I know that if I remove SETLOCAL ENABLEDELAYEDEXPANSION from batch the output will be
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
2 errors.
First part is OK, but the extraction will not work because delayed expansion is disabled.
How I can get the correct output?
Thank you
UPDATE
The input file with all types of lines
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
MD5 FAIL: exf.exe
2 errors.
UPDATE
I need this script to sync changed files based on the output of 'exf.exe' used to check the integrity of folder based on md5 checksum
When the specifications of a problem are not described, but based on examples, we can make assumptions that may or may not be correct. My assumption is that you want the last token of the lines in your text file, so this is a possible (and much simpler) solution:
EDIT: I changed my original method for a simpler one.
#echo off
setlocal
set "msg=Nothing to sync"
(for /F "tokens=3,5" %%a in (bubu.txt) do (
set "msg=Files to sync"
if "%%b" neq "" (echo %%b) else echo %%a
)) > list.txt
echo %msg%
type list.txt
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35477317.txt"
ECHO Files to sync
FOR /f "usebackqtokens=4*delims=: " %%a IN ("%filename1%") DO ECHO(%%b
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q35477317.txt containing your data for my testing.
It's not clear why you've taken your approach - is there something you haven't told us?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35477317.txt"
ECHO Files to sync
FOR /f "usebackqtokens=1*delims=:" %%a IN ("%filename1%") DO (
FOR /f "tokens=*" %%c IN ("%%b") DO ECHO(%%c
)
GOTO :EOF
Easily fixed once we get the full story...
Maybe you need something like that:
#ECHO OFF
rem SETLOCAL ENABLEDELAYEDEXPANSION
set count_to_sync=-1
for /f "tokens=*" %%i in (bubu.txt) do (
set line=%%i
echo %%i
SETLOCAL ENABLEDELAYEDEXPANSION
if "!line:~0,9!" == "MD5 FAIL:" (
set /A count_to_sync+=1
set list[!count_to_sync!]=!line:~10!
)
if "!line:~0,20!" == "File does not exist:" (
set list[!count_to_sync!]=!line:~21!
set /A count_to_sync+=1
)
ENDLOCAL
)
IF "%count_to_sync%" == "-1" (
ECHO Nothing to sync
) ELSE (
ECHO Files to sync
for /F "tokens=2 delims==" %%s in ('set list[') do (
echo %%s
)
)

parse the source safe history output and run the diff command

I am fairly new to writing batch scripts. I am trying to write a script file to achieve the following task.
I have a source safe command below to get the list of files changed between two dates
ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R
The output of the above command is as follows
Building list for $/myproject.......................................................
....................................................................................
...................................
***** AllPages.master *****
Version 67
User: user1 Date: 1/12/12 Time: 1:08p
Checked in $/myproject/websites/website1
Comment:
***** AdminTSSetup.aspx.vb *****
Version 10
User: user2 Date: 1/12/12 Time: 1:09 p
Checked in $/myproject/websites/website1
Comment:
With the output above, I want to read the file name (allpages.master, AdminTsSetup.aspx.vb) and version of each file from the output and run the following command
SS diff -DS <filename from output> -V<version from output - 1>~<version from output>
Basically, I was trying to compare the previous version with the current version for each file in the output.
Can some one please help?
This should give you what you asked for. But it seems like the diff command you specified is missing a reference to the project.
This solution should work even if the file name contains spaces, !, ;, or any special characters like & or ^.
#echo off
setlocal disableDelayedExpansion
for /f "delims=*" %%A in ('ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R ^| findstr /b "***** Version"') do (
set "ln=%%A"
setlocal enableDelayedExpansion
if "!ln:~0,1!"==" " (
for /f "eol=: delims=" %%F in ("!ln:~1!") do (
endlocal
set "file=%%~nxF"
)
) else (
for /f "tokens=2 delims= " %%V in ("!ln!") do (
set /a "version1=%%V, version0=version1-1"
ss.exe dif -DS "!file!" -V!version0!~!version1!
endlocal
)
)
)
Here is a version that adds the project information from the "Checked in" line
#echo off
setlocal disableDelayedExpansion
for /f "delims=*" %%A in ('ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R ^| findstr /b "***** Version Checked"') do (
set "ln=%%A"
setlocal enableDelayedExpansion
if "!ln:~0,1!"==" " (
for /f "eol=: delims=" %%F in ("!ln:~1!") do (
endlocal
set "file=%%~nxF"
)
) else if "!ln:~0,1!"=="V" (
for /f "tokens=2 delims= " %%V in ("!ln!") do (
endlocal
set /a "version1=%%V, version0=version1-1"
)
) else (
for /f "tokens=2* delims= " %%B in ("!ln!") do (
endlocal
set "proj=%%C"
setlocal enableDelayedExpansion
ss.exe dif -DS "!proj!/!file!" -V!version0!~!version1!
endlocal
)
)
)
Basically you need a for loop and a little state:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims= " %%i in ('ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R') do (
if "%%i"=="*****" (
rem a file name
set "FileName=%%j"
) else (
if "%%i"=="Version" (
set Version=%%j
set /a LastVersion=Version - 1
ss diff -DS "!FileName!" -V!LastVersion!~!Version!
set FileName=&set Version=&setLastVersion=
)
)
)
Should kinda work, I guess.

Resources