I have problem with batch. I want to create new dir and save file to it everytime batch is opened
This is sample of my code
SET i=0
FOR /L %%i IN (0,1,100) DO (
IF NOT EXIST res\%%i (
mkdir res\%%i
GOTO run
)
)
:run
start X.exe /stext res\%i%\X.txt
Creating folders works properly. I have problem with
start X.exe /stext res\%i%\X.txt
Thanks
%i% and %%i are totally different variables. %i% is a regular batch variable, while %%i only exists within the for loop. Once you call goto run, it no longer exists.
You can store %%i in a variable and then call it using delayed expansion, because you're setting it inside of a code block (a set of parentheses).
#echo off
setlocal enabledelayedexpansion
SET i=0
FOR /L %%i IN (0,1,100) DO (
IF NOT EXIST res\%%i (
set i=%%i
mkdir res\!i!
GOTO run
)
)
:run
start X.exe /stext res\!i!\X.txt
Related
I am trying to make one batch file to read parameters from the file and pass it to ant for deployment.
I have made the below batch file:
setlocal enabledelayedexpansion
echo on
IF EXIST "D:\testfile1.txt" (
echo Do one thing
set string=
set /p string=< D:\testfile1.txt
echo value taken from file is %string
for /f "tokens=2,4,6 delims==:" %%G IN ("%string%") DO (
echo %%G %%H %%I
set env=%%G
set dom=%%H
set com=%%I
echo ENV !env!
echo DOM !dom!
echo COM !com!
cd D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts
%ANT_HOME%\bin\xanteai deploy %%G %%H %%I
)
) ELSE (
echo Do another thing
)
endlocal
In testfile1.txt I have parameters in below format:
Environment=Env_Name1:Domain=Domain_Name1:Component=Component_name1
Parameters are different deployments. When I am running the above code it is giving below output
D:\>echo off
Do one thing Ant home is C:\tibco\ant\apache-ant-1.9.13
value taken from file is Environment=Env_name1:Domain=Domain_Name1:Component=Component_name1
Env_name1 Domain_Name1 Component_name1
ENV Env_name1
DOM Domain_Name1
COM Component_name1
Deployments starts after this.
The issue I am facing is when I run this code for different parameters (in testfile1.txt) the values of ENV, DOM and COM remains same regardless of any parameters read from testfile1.txt.
Can anyone help me to correct this code and let me know how can I just assign the values read from file to a variable and pass it to ant for deployment?
NOTE:- This batch file will be placed in scheduler which will check for testfile1.txt after every 5 minutes, when it finds that file deployment process gets triggered. Thus I have included if condition to check availability of file.
There really shouldn't be any need to be setting or echoing everything as you go, so why not just do something like this instead:
#Echo Off
If Exist "testfile1.txt" (Set /P "string="<"testfile1.txt"
SetLocal EnableDelayedExpansion
Echo value taken from file is !string!
For /F "Tokens=2,4,6 Delims==:" %%A In ("!string!") Do (
CD /D "D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts"
"%ANT_HOME%\bin\xanteai" deploy %%A %%B %%C)
EndLocal) Else Echo Do another thing
You could first split the read line at the colons with a simple for and
obtain the variable names env,dom,com from the content.
Here preceeded with an underscore for easier output.
:: Q:\Test\2019\03\08\SO_55061545_.cmd
#echo off
setlocal enabledelayedexpansion
set "FileIn=D:\testfile1.txt"
IF not EXIST "%FileIn%" (
echo Do another thing
Goto :somewhere
)
echo Do one thing
set "string="
set /p "string="<"%FileIn%"
echo value taken from file is %string%
for %%A in ("%string::=" "%") do for /f "tokens=1* delims==" %%B IN ("%%~A") DO (
echo %%A %%B %%C
set var=%%B
set "_!var:~0,3!=%%C"
)
set _
:: cd D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts
:: %ANT_HOME%\bin\xanteai deploy %_env% %_dom% %_com%
endlocal
:somewhere
> SO_55061545_.cmd
Do one thing
value taken from file is Environment=Env_Name1:Domain=Domain_Name1:Component=Component_name1
"Environment=Env_Name1" Environment Env_Name1
"Domain=Domain_Name1" Domain Domain_Name1
"Component=Component_name1" Component Component_name1
_Com=Component_name1
_Dom=Domain_Name1
_Env=Env_Name1
I'm trying to make a linux like terminal with batch. I don't know what's wrong with the code.
It crashes after
Set "word[!i!]=%cmd: ="&Set /A i+=1&Set "word[!i!]=%"
echo %word[1]%
if "%word[1]%==ls" dir.
Here's my code:
#Echo on
pushd C:\
Set "cmd="
For /F "Delims==" %%A In ('Set word[') Do Set "%%A="
set "UserAccount=%username%"
cls
:Loop
SetLocal EnableDelayedExpansion
Set /P "cmd=%UserAccount%#%UserAccount%~%cd%$ "
If Not Defined cmd EndLocal EnableDelayedExpansion & GoTo Loop
Set "i=1"
Set "word[!i!]=%cmd: ="&Set /A i+=1&Set "word[!i!]=%"
echo %word[1]%
if "%word[1]%==ls" dir
if "%word[1]%==cd" goto cd
if "%word[1]%==cd .." cd..
if "%word[1]%==cd ." cd.
GoTo Loop
:cd
if Not Defined %word[2]% echo %cd% & GoTo Loop
cd %word[2]%
EndLocal EnableDelayedExpansion
goto Loop
Probably you've coded
Set "word[!i!]=%cmd: ="&Set /A i+=1&Set "word[!i!]=%"
where you intended
Set "word[!i!]=%cmd: =%"&Set /A i+=1&Set "word[!i!]="
You'd then need to fix
if "%word[1]%==ls" dir
to
if /i "%word[1]%"=="ls" dir
(where the /i has been inserted to make the comparison case-insensitive)
I'd also avoid using cmd as a variable-name since cmd.exe is the command-processor itself.
Also, be aware that setlocal establishes a copy of the current environment which is released by an endlocal. As your code currently stands your setlocals are not balanced by endlocals` so you have an ever-accumulating nested environment arrangement - and the nesting level is limited.
As well as completely overhauling your SetLocal/EndLocal expressions...
Change:
if "%word[1]%==ls"
to
If /I "%word[1]%"=="ls"
Also change:
if "%word[1]%==cd"
to
If /I "%word[1]%"=="cd"
These two are not possible because each are two words, where %word[1]% is just cd
if "%word[1]%==cd .."
if "%word[1]%==cd ."
Also change:
if Not Defined %word[2]%
to
If Not Defined word[2]
Also, I told you when I gave you the code for setting your words, that it depended on your sentences. If you are expecting that entering commands and having the special characters and syntax recognized as individual words with that code then your likely to be disappointed.
Why is batch variable TESTTYPE not passed through to the calling batch file (Windows 7 machine)?
I trigger a test system via SVN commit message. Given e.g. this SVN message:
This should trigger my test system for a long test.
testing RunTest#longtest
Problem: The command set at the end of the inner batch file properly outputs longtest but the outer batch file outputs smoketest unexpectedly.
This is the calling batch file test.bat:
call %~dp0gettype.bat trunk 12345
echo %TESTTYPE%
This is the called batch file gettype.bat doing the work:
set TESTTYPE=smoketest
::find all lines in the string which contain the keyword "RunTest#"
setlocal EnableDelayedExpansion
set "cmd=svn log -r %2 -v | findstr /r "RunTest#""
::for all words in that found line call the subroutine "handleElement"
for /F "tokens=*" %%a in ('!cmd!') do (
if "%%a" NEQ "" for %%b in (%%a) do call:handleElement %%b
)
goto ende
::split on hashtag char and handle the keyword after the hashtag (and find the type)
:handleElement
set TMPSTR=%1
set TAGNAME=%TMPSTR:~0,8%
if "%TAGNAME%"=="RunTest#" call:getSpecialType %TMPSTR:~8%
goto:eof
:getSpecialType
set TESTTYPE=%1
goto:eof
:ende
set
change
call %~dp0gettype.bat
echo %TESTTYPE%
to
for /F "usebackq" %%i in (`call %~dp0gettype.bat`) do set "TESTTYPE=%%i"
echo %TESTTYPE%
and in gettype.bat
change
:ende
set
to
:ende
echo %TESTTYPE%
Running the following batch script, I get an infinite loop even when all files are moved into the destination folder. I get the message: "The system cannot find the file specified" at each iteration.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
cd .\IMPORT
set nlot=0
:beginfor
set count=0
set /a nlot=%nlot%+1
FOR /R "." %%A IN (Zango_Sku_*.xml) DO (
IF NOT EXIST .\lot%nlot% mkdir .\lot%nlot%
move .\%%~nxA .\lot%nlot%\%%~nxA
set /a count=!count!+1
IF !count! EQU 2000 goto :beginfor
)
i am trying to get files from a directory and want to set names of the file to a variable using batch script.
this is my code .but it always setting same value to variable
can any body give solution
echo on
setlocal EnableDelayedExpansion
for /f %%x in ('dir /b C:\backup_dir') do (
SET test=%%~nx
if "%test:~0,6%"=="kdc_db" (set DUMP=%%x)
if "%test:~0,6%"=="kdc_ke" (set KEYS=%%x)
)
echo %DUMP%
echo %KEYS%
here dump and keys variables are always set to same value
You need to use delayed expansion. You have already enabled it, you just need to replace your %'s with !'s
echo on
setlocal EnableDelayedExpansion
for /f %%x in ('dir /b C:\backup_dir') do (
SET test=%%~nx
if "!test:~0,6!"=="kdc_db" (set DUMP=%%x)
if "!test:~0,6!"=="kdc_ke" (set KEYS=%%x)
)
echo %DUMP%
echo %KEYS%