Running two batch labels (:label) at the same time - batch-file

I am currently making a game in batch for fun, and I have everything done besides the input. So far, for the input, I have this code:
:input
rem Read player input
set "key="
for /f "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>nul') do if not defined key set "key=%%K"
if "!key:~-1!" equ "!CR!" goto endInput
if "!key:~-1!" equ "!BS!" (
if defined input set "input=%input:~0,-1%"
) else (
set "input=%input%!key:~-1!"
)
set /p "=%input%" > input.txt < nul
if not exist input.txt goto error
cls
if exist input.txt (
for /f "delims=" %%x in (input.txt) do set "inp=%%x"
set "dir=!input:~-1!"
) else goto error
goto input
I want this to retrieve user input while running the other section of the program above it. (I know, it uses a external input file, but it's pretty fast.)

Related

Batch Script Variables - Pull from a single line of a text file with multiple lines of variable options

I'm trying to create a batch file that the user can use to connect with SCCM and RDP to PCs that are constantly connected to. I can do this easily with writing out the script for each PC but I would like to do this in a way that I have a text file with variables as I would do with a LOOP but I don't need a LOOP, I need to be able to select a single line of the text file.
Currently I have a list of options and I manually edit the script file with the options and the user selects a number to select the name but I need to remove all script so that the user doesn't accidentally modify the script, only the variables.
Sample text file - (Store user entered variables for both SCCM and RDP)
Variable1 Variable2 Variable3 Variable4
Description1 ComputerName1 ComputerUser1 ComputerPassword1
Description2 ComputerName2 ComputerUser2 ComputerPassword2
Description3 ComputerName3 ComputerUser3 ComputerPassword3
Description4 ComputerName4 ComputerUser4 ComputerPassword4
Description5 ComputerName5 ComputerUser5 ComputerPassword5
Description6 ComputerName6 ComputerUser6 ComputerPassword6
Sample SCCM Batch script -
(SCCM command line interface only allows computer name so I'd only be able to pull the name but I still want a single text file for both SCCM and RDP connections)
Variable1's listed from text file -
Description1
Description2
Description3
Description4
Description5
Description6
Make a selection: ((User enters Variable1))
sccm\CmRcViewer.exe "<variable2>" <-------from a specific line a user selects
Sample RDP Batch script - (I use the cmdkey to store the user/pass since mstsc command line interface only allows the name unless I do this first)
Variable1's listed from text file -
Description1
Description2
Description3
Description4
Description5
Description6
Make a selection: ((User enters Variable1))
cmdkey /generic:"<variable2>" /user:"<variable3>" /pass:"<variable4>" <-------from a specific line a user selects
mstsc /v:"<variable2>" /f <-------from a specific line a user selects
Any assistance is greatly appreciated. As a disclaimer, this is only being used by those in IT who has a decent knowledge of batch scripts, just trying to make it a tad easier to get to our PCs we are constantly accessing and those users we are constantly helping remotely...
This is a line selector I've made previously that builds an Array containing the value of each Line and allows scrolling and selection of the value via the choice command.
The :Load and :Extract labels are where the functions most relevant to your needs reside.
EDIT:
The stripped down components modified for just the output you seek - Using the safer form of input (Choice Command). Note that if the number of options exceeds 9, Set /P will need to be used - though input should be validated.
#Echo Off & Setlocal EnableDelayedExpansion
Set "lines=0"
Set "Options=E"
For /f "Skip=1 Tokens=* Delims=" %%A in (%~dp0\Choices.txt) do (
Set /A lines+=1
Set "line[!lines!]=%%A"
Set "Options=!Options!!lines!"
)
:extract
Cls
For /F "Tokens=1,2 Delims==" %%A in ('Set line[') Do (
Set "Option=%%~A"
Set "Option=!Option:line=!"
Echo !Option! %%B
)
For /F "Delims=" %%C in ('Choice /N /C %Options%') Do (
If "%%C" == "E" (Endlocal & Exit /B 0)
For /F "Tokens=2,3,4 Delims= " %%G in ("!line[%%C]!") Do cmdkey /generic:"%%~G" /user:"%%~H" /pass:"%%~I" & mstsc /v:"%%~G" /f
)
Goto :extract
An example of validating input if using Set /P
Set /P "INPUT=Selection: "
For /F "Delims=" %%C in ("!Input!") Do (
echo.%%C| findstr /R "[^0-9]" >nul 2>nul && Goto :extract
IF "%%C" == "0" (Endlocal & Exit /B) Else IF "%%C" GTR "%lines%" Goto :extract
For /F "Tokens=2,3,4 Delims= " %%G in ("!line[%%C]!") Do cmdkey /generic:"%%~G" /user:"%%~H" /pass:"%%~I" & mstsc /v:"%%~G" /f
)
Original
#Echo off & CD "%~dp0"
TITLE %~n0 & Setlocal
%= Remark =%
(Set LF=^
%= NewLine variable to split set /p input line =%)
::: / Creates variable /AE = Ascii-27 escape code.
::: - http://www.dostips.com/forum/viewtopic.php?t=1733
::: - https://stackoverflow.com/a/34923514/12343998
:::
::: - /AE can be used with and without DelayedExpansion.
Setlocal
For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
Endlocal
Set "/AE=%%a"
)
::: \
%= create blank batch file to store and Selectively retieve variables with =%
If not Exist "inputURLlog.log" (Break>inputURLlog.log & Goto :main)
For %%A in ( "%/AE%[36m[%/AE%[34mN%/AE%[36m]ew" , "[%/AE%[34mL%/AE%[36m]oad%/AE%[0m")Do Echo.%%~A
Choice /N /C nl >nul
If errorlevel 2 (Goto :load)
Goto :Main
:Input <VarName>
Set "variable=%~1"
Setlocal EnableDelayedExpansion
:Validate
%= allow input of variable, test input, store for reuse. =%
Echo(%/AE%[35m
Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
IF "!input!"=="" (
Echo(%/AE%[31m!variable! required.
Goto :Validate
)
If "!input!"=="" (Echo(%/AE%[31m!variable! required. & Goto :Validate)
IF /I "!input!"=="load" (Goto :load)
%= Url Validation - Optional =%
(ping !input!)>nul || (Echo Invalid url & Endlocal & Goto :main)
ECHO(!input!>>inputURLlog.log
Endlocal
Exit /B
:main
Call :Input Url
Goto :main
:load
Setlocal EnableDelayedExpansion
Set lines=0
For /f "Tokens=* Delims=" %%A in (inputURLlog.log) do (
Set /A lines+=1
Set "line[!lines!]=%%A"
)
REM Set "#=%lines%" & REM start from newest entry
Set "#=1" & REM start from oldest entry
Echo %#%
:extract
For %%A in (!#!) do (
Cls
Title Option %%A of !lines!
Echo(Url: !line[%%A]!
For %%B in ("%/AE%[36m[%/AE%[34mS%/AE%[36m]elect" "[%/AE%[34mN%/AE%[36m]ext" "[%/AE%[34mL%/AE%[36m]ast%/AE%[0m")Do Echo.%%~B
Choice /N /C LNS /M ""
If "!errorlevel!"=="3" (Set "Url=!line[%%A]!" & Goto :Selected)
If "!errorlevel!"=="2" (IF not !#! GEQ !lines! (Set /A #+=1) Else (Set "#=1"))
If "!errorlevel!"=="1" (IF not !#! LEQ 1 (Set /A #-=1) Else (Set "#=%lines%"))
)
Goto :extract
%= Demonstrate that variable has been reloaded =%
:Selected
Echo( Selected Url = !Url!
Pause >nul
#echo off
setlocal enabledelayedexpansion
set x=0
for /f "skip=1 tokens=1-4" %%i in (%~dp0\Choices.txt) do (
call set /a x+=1
call set item.%%x%%.1=%%i
call set item.%%x%%.2=%%j
call set item.%%x%%.3=%%k
call set item.%%x%%.4=%%l
)
for /l %%i in (1,1,%x%) do (
call echo %%i. !item.%%i.1!
)
echo. & set /p sel=Enter Selection:
cmdkey /generic:"!item.%sel%.2!>" /user:"!item.%sel%.3!" /pass:"!item.%sel%.4!"
mstsc /v:"!item.%sel%.2!" /f

Batch choice alternative for detecting key without pause

The question is: is here any way to detect keypress in batch without pausing entire script for scripts like chat, games etc? For now I know about useful command called xcopy. I use it to detect and extract pressed key such as Enter. But it pauses entire script and waits for confirmation, while chose may not pause script, but unable to do silent invalid key as well as detecting specific ones
It's not simple with pure batch, but possible.
This is a sample that uses two threads.
The first thread :keyCheck waits for a key.
The second thread :mainFunc counts until a key is pressed.
#echo off
setlocal DisableDelayedExpansion
set "func=%~0"
for /F "delims=\" %%X in ("%func:*\=%") do set "func=%%X"
if ":" == "%func:~0,1%" (
goto %func%
)
( "%~d0\:keyCheck\..%~pnx0" ) > lock.tmp | "%~d0\:mainFunc\..%~pnx0"
echo Ende
exit /b
REM *** Thread1 - waiting for key
:keyCheck
for /f skip^=1^ delims^=^ eol^= %%A in ('replace.exe ? . /u /w') do for /f delims^=^ eol^= %%B in ("%%A") do (
set key=%%B
)
exit /b
REM *** Thread2 - Counting up
:mainFunc
setlocal EnableDelayedExpansion
for /F %%# in ('copy /Z "%~dpf0" NUL') do set "CR=%%#"
set "lastTime="
set cnt=0
:loop
set /a cnt+=1
set /p "=Stop with any key !cnt! !CR!" < nul
call :waitOrStop
goto :loop
:waitOrStop
REM Check if a key was pressed, then the lock is released
(
(
rem
) 2> lock.tmp && (
echo(
exit
)
) 2> nul
set "now=%time:~-2%"
if "!now!" NEQ "!lastTime!" (
set "lastTime=!now!"
exit /b
)
goto :waitOrStop
While it is not at all straightforward, the site Dostips.com has a full implementation of the game "Snake" using nothing but Batch, including non-blocking input.
Essentially, the batchfile spawns a second command prompt, which is blocking on input. When input is received, it is routed to the first, "main" program which polls the input file.
Like I said, not straightforward or simple, but it is do-able.
Although possible, there is not a simple way to do that using pure Batch commands. The simplest way is use a third party .exe program specifically designed to do so, like my GetKey.exe auxiliary file. This program get a key from keyboard and returns via ERRORLEVEL a value that indicate the key pressed. All keys are processed: ASCII characters are returned as positive values and extended keys (function keys, cursor control keys, etc) as negative values. If /N switch is given, GetKey.exe no waits: if a key was not pressed the value returned is zero.
This is a very simple example program that use GetKey.exe auxiliary file:
#echo off
setlocal EnableDelayedExpansion
rem Simple example of GetKey.exe auxiliary program to detect keys pressed
rem Get a Return (ASCII 13) CR character
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
cls
echo Press a key at any moment; press ESC key to end
echo/
set i=0
:loop
set /A i+=1
set /P "=%i%!CR!" < NUL
GetKey /N
if %errorlevel% equ 0 goto loop
echo/
echo Key pressed: %errorlevel%
if %errorlevel% neq 27 goto loop
You may download GetKey.exe auxiliary program from this site; you may also download SHOWKEYCODES.BAT program that show the codes returned by GetKey.exe for all keys in keyboard, including Shift-, Ctrl- and Alt- combinations.
PS - If you are reluctant to use a third-party .exe program, just think that the original choice command is also an .exe file with a 35 KB size, but my GetKey.exe file is just 1536 bytes size. You may review several of my auxiliary .exe files and the replies posted by users of these programs at the same link.
Another multithread method, this time using Xcopy to capture input.
Handles all ASCII printable characters in the range 32 ~ 126, as well as TAB, Carriage Return and Backspace.
#Echo off
REM on relaunch, directs execution to the target label.
If not "%~1"=="" Goto:%1
REM initialize variables for the controller launcher
Set "QuitKey={BACKSPACE}"
Set "SignalFile=%TEMP%\%~n0_signal.dat"
REM Pipe the output of the function XCOPYCONTROLLER to Game to facilitate non blocking input.
Start /Wait /B "" "%~F0" XCOPYCONTROLLER 1>"%SignalFile%" | "%~F0" GAME <"%SignalFile%" 2> nul
REM end of controller launcher. Game has been quit.
Endlocal & Goto:Eof
:GAME
REM enable EDE environment to facilitate the use of posion characters.
REM More advanced gameloops use an infinite for /l loop to iterate through game logic
REM as it is parsed more efficiently by cmd.exe's interpreter.
REM such loops Require EDE to expand variables with their current value.
Setlocal EnableDelayedExpansion
:GAMELOOP
REM your game loop goes here.
Cls
Set /p "key="
If Defined key Title Lastkey= "!Key!". Press !Quitkey! to exit
Echo(%TIME%
If "!Key!"=="quit" EXIT
Set "key="
Goto:GAMELOOP
=======================================================
:XCOPYCONTROLLER by T3RRY
Setlocal EnableExtensions DISABLEdelayedExpansion
REM define unprintable characters
(Set LF=^
%= Do Not Modify. Linefeed Variable =%)
%= Escape =% for /f "Delims=" %%e in ('Echo Prompt $E^|cmd') Do Set "\E=%%e"
%= Tab =% for /f "delims= " %%T in ('robocopy /L . . /njh /njs' )do set "TAB=%%T"
%= Carriage Return =% for /f %%C in ('copy /Z "%~dpf0" nul') do set "CR=%%C"
%= BackSpace =% for /F "delims=#" %%B in ('"prompt #$H# &echo on &for %%b in (1) do rem"') do Set "BS=%%B"
Set "BS=%BS:~0,1%"
%= SUB =% copy nul sub.tmp /a > nul
for /F %%a in (sub.tmp) DO (
set "sub=%%a"
)
del sub.tmp
(TITLE )
<nul Set /P "=Press any Key to start"
REM Environment handling allows use of ! key
For /l %%C in () do (
Set "Key="
for /f "delims=" %%A in ('%SystemRoot%\System32\xcopy.exe /w "%~f0" "%~f0" 2^>nul') do If not Defined Key (
set "key=%%A"
Setlocal ENABLEdelayedExpansion
set "key=!KEY:~-1!"
If "!key!" == "!QuitKey!" (
<nul Set /P "=quit"
EXIT
) Else If "!Key!" == "%BS%" (
If "!QuitKey!" == "{BACKSPACE}" (
<nul Set /P "=quit"
EXIT
)
<nul Set /p "={BACKSPACE}"
) Else If "!Key!" == "!CR!" (
If "!QuitKey!" == "{ENTER}" (
<nul Set /P "=quit"
EXIT
)
<nul Set /p "={ENTER}"
) Else If "!Key!" == "!TAB!" (
If "!QuitKey!" == "{TAB}" (
<nul Set /P "=quit"
EXIT
)
<nul Set /p "={TAB}"
) Else (%= Echo without Linefeed. Allows output of = Key and Space =%
1> %~n0txt.tmp (echo(!Key!!sub!)
copy %~n0txt.tmp /a %~n0txt2.tmp /b > nul
type %~n0txt2.tmp
del %~n0txt.tmp %~n0txt2.tmp
)
Endlocal
)
)

How can we split string using windows bat

How can we split string using windows bat script?
for below .bat code snippet
#echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
rem mkdir !a!
) || (
echo +)
rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause
below config file
Q
What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
How can I get the /path/to/case and value as a pair?
for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"
set j=0
for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
echo mkdir !a!
) || (
echo +)
for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q43407067.txt containing your data for my testing.
(These are setting that suit my system)
SO - to address your problems:
because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----
You can't delete that ) because it's participating in a parenthesis-pair.
You need a space between the in and the (.
I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)
In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.
Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.
I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:
#Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
Echo=MD %%A
Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
) Do (Set/A "i+=1"
Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF
remove Echo= on line 6 if you are happy with the output and really want to create those directories

How to make a batch SET /P statememt not require an enter

I'm making my own console based off batch because I want more then what cmd offers but have the key features that batch has.
One of the most important things is to prompt the user for the command:
:prompt
#echo off
title JDOS command line
echo.
echo.
SET /P command="%FDIR%>"
rem SECTIONS/COMMANDS
rem /I means the if statement is not case-sensitive. USE IT AT ALL TIMES!
if /I "%command%"=="add" goto add
if /I "%command%"=="subtract" goto subtract
goto prompt
(This is just a small portion of all the options but you get the idea)
:add
title Calculator/Add
SET /P Add_A=Please enter the first number:
SET /P Add_B=Please enter the second number:
SET /A sum=%Add_A% + %Add_B%
echo The sum is %sum%
timeout /t 10 >nul
goto prompt
But this is timewasting and sort of idiotic (at least in my opinion).
So, can I execute goto add without the user pressing enter ?
Edit:
I'm thinking an approach similar to CHOICE but with the option to have more than 1 key be pressed (so for example instead of 1/2/3/4/ it would be restart/shutdown/lock/logoff/
I modified my accepted answer at this post and adjusted it for this request. Here it is:
#echo off
setlocal EnableDelayedExpansion
set "commands=add subtract"
for %%a in (%commands%) do set "com[%%a]=1"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set "command="
:nextKey
set "key="
for /F "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%K"
if "!key:~-1!" equ "!CR!" goto endCommand
if "!key:~-1!" equ "!BS!" (
if defined command (
set "command=%command:~0,-1%"
set /P "=.!BS!!BS! !BS!!BS!" < NUL
)
) else if "!key:~-1!" neq " " (
set "command=%command%!key:~-1!"
set /P "=!key:~-1!" < NUL
if defined com[!command!] goto endCommand
)
goto nextKey
:endCommand
echo/
echo/
echo command read: "%command%"
Note: The mechanism used in this method does not allow to insert spaces! This code should be modified in large parts in order to read spaces.

If conditional in batch (inside a for, inside an if)

I have an application that reads through all the files in the directory and will rename those files (if necessary) and create log files from the innards of them, however there are a couple of catches....
If the files 15th character is something other than a "." or "_", substring the 14 characters previous plus the 15th and store as variable.
If the files 15th character is a "." or "_" substring the 14 characters previous and store as variable.
For example, if filename is: DIY_0000000000a.xml the variable would be DIY_0000000000a and if the filename is: DIY_0000000000.xml the variable would be DIY_0000000000
There is a catch to all of this...
If the user picks Option 2, which is to rerun the application, then the application should rename all files in the directory to DIY_xxx_rerun.xml or DIY_xxxa_rerun.xml (should now explain the "_" in the aforementioned options.
The problem I have and what I need assistance with is that I can't seem to get the conditional logic correct. The application works, but if I have a mix of files in the directory (ie: DIY_0000000000a.xml, DIY_0000000000.xml & DIY_0000000000_rerun.xml, DIY_0000000000a_rerun.xml) it may work on one kind but not the other. Help is much appreciated.
Below is a snippet of my current code:
SETLOCAL EnableDelayedExpansion
:EXECUTE
::IF AN XML FILE EXISTS ADD IT TO A LOG FILE, PROMPT USER IF THEY WANT TO VIEW IT
ECHO.
ECHO =============================================
ECHO **SELECT YOUR OPTION BY USING YOUR KEYBOARD**
ECHO =============================================
ECHO.
ECHO 1 - Generate DIY Log Files for normal orders
ECHO 2 - Generate DIY Log Files for _rerun orders
ECHO 3 - EXIT
ECHO.
ECHO.
SET /P CHOICE=Type your option, then press ENTER:
IF %CHOICE%==1 (
SET RERUN=
GOTO PROCESS
)
IF %CHOICE%==2 (
SET RERUN=_rerun
GOTO PROCESS
)
IF %CHOICE%==3 EXIT
IF NOT '%CHOICE%'== SET CHOICE=%CHOICE:~0,1%
ECHO.
ECHO "%CHOICE%" is not a valid option.
PAUSE
GOTO EXECUTE
:PROCESS
IF EXIST DIY*.xml (
SET /a FILECOUNTER=0
ECHO.
ECHO Processing...
FOR /f "delims=" %%a IN ('DIR /b /a-d /on DIY*.xml') DO (
SET /a LOGCOUNTER=!FILECOUNTER!/1+1
SET ORDERNUMBER=%%a
IF "!ORDERNUMBER:~14,1!" NEQ "." (
IF "!ORDERNUMBER:~14,1!" NEQ "_" (
SET ORDERNUMBSTR=!ORDERNUMBER:~0,15!
) ELSE (
SET ORDERNUMBSTR=!ORDERNUMBER:~0,14!
)
)
SET ORDERNUMBSTR=!ORDERNUMBER:~0,14!
IF "%RERUN%"=="_rerun" (
REN !ORDERNUMBSTR!.xml !ORDERNUMBSTR!%RERUN%.xml >NUL 2>&1
) ELSE (
REN *!ORDERNUMBSTR!*.xml !ORDERNUMBSTR!.xml >NUL 2>&1
)
FOR /f "tokens=4 delims=<>" %%i IN ('TYPE !ORDERNUMBSTR!%RERUN%.xml ^|find "DIY_LOG_ID"') DO (
SET DIYLOGID=%%i
)
FOR /f "tokens=7 delims=<>" %%j IN ('TYPE !ORDERNUMBSTR!%RERUN%.xml ^|find "ACCOUNT_KEY"') DO (
SET ACCOUNTKEY=%%j
FOR /f "tokens=1-3 delims=-" %%x IN ("!ACCOUNTKEY!") DO (
SET CONTRACTNR=%%x
SET PLANSEQ=%%y
SET SUBSEQ=%%z
)
)
...
ENDLOCAL
Just needed a day to think about it...
Was able to get what I wanted by using the following FOR loop instead of IFs
FOR /f "delims=" %%a IN ('DIR /b /a-d /on DTL*.XML') DO (
SET /a LOGCOUNTER=!FILECOUNTER!/1+1
SET ORDERNUMBER=%%a
**FOR /f "tokens=1,2 delims=_." %%p IN ("!ORDERNUMBER!") DO (
SET ORDERNUMBSTR=%%p_%%q**
)
IF "%RERUN%"=="_rerun" (
REN !ORDERNUMBSTR!.XML !ORDERNUMBSTR!%RERUN%.XML >NUL 2>&1
) ELSE (
REN *!ORDERNUMBSTR!*.XML !ORDERNUMBSTR!.XML >NUL 2>&1
)
...

Resources