This question already has answers here:
What does symbol ^ mean in Batch script?
(4 answers)
Closed 6 years ago.
I have a simple command within my bat file that is causing it to explode, I think it has to do with using a string within the command but I am a bit clueless now.
The command in question is:
for /f %%i in ('dir *.nupkg /b/a-d/od/t:c | findstr "symbols"') do set LAST=%%i
What is the right way of using the "symbols" string in the above line?
I don't understand why you're piping the results through Findstr.
For /F "Delims=" %%A In ('Dir/B/A-D/OD/T:C *symbols*.nupkg') Do Set "LAST=%%A"
If you're not sure as to whether the last file will contain the string symbols in it's name, and that is what you're trying to ascertain, then you could still check that after the loop.
For /F "Delims=" %%A In ('Dir/B/A-D/OD/T:C *.nupkg') Do Set "LAST=%%A"
If "%LAST:symbols=%"=="%LAST%" (Echo= NOT a symbols file) Else (
Echo= WAS a symbols file)
Related
This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed last year.
to follow up on the last solution in this link https://www.windows-commandline.com/get-date-time-batch-file/
I know there's no windows CMD format with this but I've tried to batch replace and arrays apparently don't have good solutions if you Google for an answer since most of the time people find and replace things in files I keep getting those answers.
If you are on a supported Windows system, powershell.exe will be available.
C:>#FOR /F "delims=" %A IN ('powershell.exe -NoLogo -NoProfile -Command Get-Date -Format 'M-d-yy'') DO #(SET "THEDATE=%~A")
C:>echo %THEDATE%
1-21-22
If this is in a .bat file script, double the PERCENT SIGN characters on the loop variable. %%A
If you want a pure batch solution, wmic provides a few date-related strings without leading zeroes:
#echo off
setlocal
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set "_%%x"
set "Mydate=%_Month%-%_Day%-%_Year%"
echo %mydate%
echo maybe also useful sometimes:
set _
This question already has answers here:
Arrays, linked lists and other data structures in cmd.exe (batch) script
(11 answers)
How to loop through comma separated string in batch?
(4 answers)
Closed 2 years ago.
I have this list in batch:
set list=12,34,56
echo %list%
How can I get the list to print only 34?
To print the second number 34:
for /f "tokens=2 delims=," %%a in ("%list%") do #echo %%a
To print all 3 numbers 12 34 56:
for /f "tokens=1-3 delims=," %%a in ("%list%") do #echo %%a %%b %%c
The double percents in %%a are required in a batch file. At the command prompt, use just %a.
This question already has answers here:
windows cmd: problems with for /f with a quoted command with quoted parameters
(2 answers)
Closed 5 years ago.
In a batch file I want to set a variable to the one-line output of a program - a task explained in multiple questions and internet ressources.
In my specific task I want to execute the command
"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"
As you can see, the path for the program I want to execute, FileVersion.exe contains spaces, and the second parameter handed to FileVersion.exe contains a full path with spaces as well.
When I call the above command either directly at the command line or by using CALL in the batch file it correctly executes the program prints out the expected output.
Now, to capture the output of executing the program I use the follow batch command
FOR /F "usebackq tokens=* delims=" %%i IN (`"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"`) DO (
echo %%i
)
The FOR documentation says
usebackq Use the alternate quoting style:
- Use double quotes for long file names in "filenameset".
- Use single quotes for 'Text string to process'
- Use back quotes for `command to process`
I think I did this correctly by using back quotes around the command, and using quotes around the filenames.
However, the call fails with the message (translated from German)
The command "D:\My" is either incorrect or couldn't be found.
I think this points to a problem with escaping the spaces.
The weird part: If I either replace the path of the exe to call, or the parameter with a path sans spaces, it works. So both
FOR /F "usebackq tokens=* delims=" %%i IN (`FileVersion.exe /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"`) DO (
echo %%i
)
and
FOR /F "usebackq tokens=* delims=" %%i IN (`"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n Test.exe`) DO (
echo %%i
)
work.
Why don't two quote-escaped strings work in the same command and how can I fix it?
I wouldn't use usebackq and first resolve the complex path:
#Echo off
Pushd "D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release"
FOR /F "tokens=*" %%i IN (
' FileVersion.exe /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe" '
) DO echo %%i
PopD
This question already has answers here:
'Pretty print' windows %PATH% variable - how to split on ';' in CMD shell
(12 answers)
Closed 3 years ago.
I just checked stackoverflow that seemed to be very helpful and worked fine on Windows XP. But using Windows 7 it does not work for some obscure reason.
The PATH variable looks like this
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\
It obviously contains \ as well as semicolons I use to split in a batch that contains this FOR-loop:
FOR /F "delims=;" %%A IN ("%PATH%") DO (
echo %%A
)
Executing does not cause any error but it provides just one (the first) token
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
I had no idea why FOR terminates and played around with several variations that have been suggested on the net but none did the job.
Any help will be highly appreciated.
Christian
You could do it this way.
for %%A in ("%path:;=";"%") do (
echo %%~A
)
(Source)
The problem with the way you have it is that, using the for /F switch, %%A only specifies the first token. You would have to do for /f "tokens=1-9 delims=;" %%A in ("%PATH%") and read in %%A through %%I that way.
Combining things learned on this and various other stackoverflow pages, the OP can be extended to:
How to ensure the PATH variable has unique values?
Which can be done this way, using array variables:
REM usage: call :make_path_unique VARNAME "%VARNAME%"
REM 1: splits VARNAME on ';' and builds an array of unique values (case insensitive)
REM 2: glues the array back into a single variable
REM 3: set the VARNAME to this newly unique-ified collection.
REM
REM From: various StackOverflow pages:
REM http://stackoverflow.com/questions/5471556/pretty-print-windows-path-variable-how-to-split-on-in-cmd-shell
REM http://stackoverflow.com/questions/3262287/make-an-environment-variable-survive-endlocal
REM http://stackoverflow.com/questions/14879105/windows-path-variable-how-to-split-on-in-cmd-shell-again
REM
:make_path_unique
setlocal EnableDelayedExpansion
set VNAME=%~1
set VPATH=%~2
set I=0
for %%A in ("%VPATH:;=";"%") do (
set FOUND=NO
for /L %%B in (1,1,!I!) do (
if /I "%%~A"=="!L[%%B]!" set FOUND=YES
)
if NO==!FOUND! (
set /A I+=1
set L[!I!]=%%~A
)
)
set FINAL=!L[1]!
for /L %%n in (2,1,!I!) do (
set FINAL=!FINAL!;!L[%%n]!
set L[%%n]=
)
for %%P in ("!FINAL!") do (
endlocal
set %VNAME%=%%~P
)
exit /b 0
Summary of steps:
for loop splitting PATH at ';' and properly managing quotes
for loop looking at all previously stored paths
only extend the array if this is a new path to be added
glue the array pack together, clearing the array variable as we go
replace the path and clear the temporary variables
return from function with no errors.
invoked, of course, with:
set PATH=%PATH%;%MY_PATH_ADDITIONS%
call :make_path_unique PATH "%PATH%"
I am relatively new to Batch and am having problems trying to find a string value within a txt file. I am using a neat program called CCExtactor to extract closed captioning from a file and need to grab the time of the closed captioning.
The output from CCExtractor looks like this:
###SUBTITLE#08:37#08:40#She ran away
My batch script looks like this:
for /F "delims=" %%a in (subtitle.txt) do ( echo %1|findstr /R /C:"^[^0-9][0-9]*$" )
I can't seem to get this to work! I am trying to skim the time values!
How would I go about doing this??
Here's an example.
#echo off
setlocal
set filename=subtitle.txt
for /f "delims=# tokens=2,3" %%I in ('findstr /r /c:"^###SUBTITLE#[0-9][0-9]:[0-9][0-9]#[0-9][0-9]:[0-9][0-9]#" %filename%') do (
echo start=%%I, end=%%J
)
In your for statement, you should make the delims value to be #. That way you can echo out %%d and %%e to get the start time and end time.