简体   繁体   中英

Set Batch Command Line As String Within Another Command Line

As the title Suggests, I'm Trying to Set a command line output as a string within another command line.

Example:

verpatch %%~ni%%~xi /s desc %%~ni /s comment %%~ni /pv 'call jrepl "[0-9]+.[0-9]+.[0-9]+.[0-9]+" "" /match /s %filename%'

Please note that:

call jrepl "[0-9]+.[0-9]+.[0-9]+.[0-9]+" "" /match /s %filename%

is another command which outputs: 1.2.3.4 [It's a function that extracts numbers from a file name]

So I need this output to like this:

verpatch %%~ni%%~xi /s desc %%~ni /s comment %%~ni /pv 1.2.3.4

Here I share my entire code so you get the idea:

@ECHO OFF
SETLOCAL
set file=C:\Users\Jack\Desktop\Files\*
set dot=.
FOR %%i IN ("%file%") DO (
set filedrive=%%~di
set filepath=%%~pi
set filename=%%~ni
set fileextension=%%~xi
:: This Line is supposed to store jrepl command as a string ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET COMMAND="call jrepl "[0-9]+\.[0-9]+\.[0-9]+.[0-9]+" "" /match /s %filename%"
:: Extract the string and print to the screen
verpatch %%~ni%%~xi  /s desc %%~ni /s comment %%~ni /pv %COMMAND%
pause
)

pause

jrepl is regex text processor batch functions file JREPL.BAT by dbenham

This is My Second Attempt to save the command to line in as a variable:

@ECHO OFF
setlocal enabledelayedexpansion
set "file=C:\Users\Jack\Desktop\Files\*"
FOR %%i IN ("%file%") DO (
set filedrive=%%~di
set filepath=%%~pi
set filename=%%~ni
set fileextension=%%~xi
: Save Command Line as a variable
for /f "delims=" %%a in ('call jrepl "[0-9]+\.[0-9]+\.[0-9]+.[0-9]+" "" /match /s %%~ni') do @set theValue=%%a 
verpatch %%~ni%%~xi  /s desc %%~ni /s comment %%~ni /pv %theValue%
pause
)
pause

I have a folder full of my personal programs

ProgramOne 1.2.3.4.exe

ProgramTwo 2.2.2.2.exe

etc.

and I'm trying to make a batch that extract Program name and Version from those programs and have them inserted into individual command lines, for example:

verpatch "ProgramOne 1.2.3.4.exe" /s desc ProgramOne /s comment ProgramOne /pv 1.2.3.4

Try this, I'm assuming that both your verpatch and JREPL commands are correct, (I've never used them) :

@Echo Off
Set "filelocn=C:\Users\Jack\Desktop\Files"
For %%A In ("%filelocn%\*.exe") Do (For /F %%B In (
        'jrepl "[0-9]+\.[0-9]+\.[0-9]+.[0-9]+" "" /match /s %%~nA') Do (
        verpatch "%%A" /s desc "%%~nA" /s comment "%%~nA" /pv %%B
        Pause))

Edit

Based on your provided filenaming strategy and without using JREPL this may work for you:

@Echo Off
Set "filelocn=C:\Users\Jack\Desktop\Files"
Set "progName="
For %%A In ("%filelocn%\*.exe") Do (SetLocal EnableDelayedExpansion
    For %%B In (%%~nA) Do If Not Defined progName (Set "progName=%%B"
    ) Else Set "fileVer=%%B"
    verpatch "%%A" /s desc !progName! /s comment !progName! /pv !fileVer!
    EndLocal)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM