cmd script printing out but not executing - cmd

Hi all I'm currently writing up a small bash script to automate some stuff for me but I've hit a bit of a snag My current file looks like the following:
for /f "delims=" %%f in ('dir /b "D:/*"') do C:\MediaInfo\MediaInfo.exe "--Inform=Video;%Width% "D:\%%f"
pause > nul
The pause thing is just there so I can see the output. While the part after the |do| command works fine if I manually type it in (as in I know my syntax for that is correct) however when running the batch script instead of actually executing the above commands it simply prints them out to the command console. Am I missing some syntax here or similar. Also as a side note I would like to push the resulting value of that query into an int so I can use it, do you know if this is possible in bash or should I look at trying to use a higher level language? Thanks!

I have no notion of the intricacies of mediainfo - but it would be unusual if it was to accept unbalanced quotes in its command line as you have posted. I'd suggest an extra after %Width%

Related

Replacement for $() in Windows batch script

I am trying to convert my bash script into a Windows batch file. It's a really simple one liner that's supposed to feed the contents of a file as arguments to script.exe, and send the results to output.txt.
This is the working bash script:
./script.exe $(cat input.txt) > output.txt
I know this might be bad style, but it works. The problem is, I have no idea how to do something like $() in a windows batch file. When I use it it sends the string "$(cat input.txt)" as the argument instead of running the command.
This bash construct is called command substitution. Here is a great answer from #MichaelBurr.
You can get a similar functionality using cmd.exe scripts with the
for /f command:
for /f "usebackq tokens=*" %%a in (`echo Test`) do my_command %%a
Yeah, it's kinda non-obvious (to say the least), but it's what's
there.
See for /? for the gory details.
Sidenote: I thought that to use "echo" inside the backticks in a
"for /f" command would need to be done using "cmd.exe /c echo
Test" since echo is an internal command to cmd.exe, but it works
in the more natural way. Windows batch scripts always surprise me
somehow (but not usually in a good way).
See also, on Superuser: Is there something like Command Substitution in WIndows CLI?

windows oneliner to set a command output in an environment variable

As stated here Is it possible to set an environment variable to the output of a command in cmd.exe I always used that
mycommand.exe>%TEMP%\out.txt
set /P FOO=<%TEMP%\out.txt
But that's ugly because it creates a file.
The for method is better but complicated
I wanted something simple a la unix, like:
mycommand.exe|set /P FOO=
No error, but FOO is not set after I run that.
Why is this not working?
Best way I can think of doing this would be to create your own little batch file that silently uses the FOR construct. For instance, you could create a batch named BatchSet.bat, stored somewhere on your path. The batch would contain the following code:
#Echo off
for /f "delims=" %%i in ('%2') do set %1=%%i
Set %1
If you run this with the following command:
BatchSet MyVar whoami
You'll get something like:
MyVar=servername\John
Obviously, the command you run should limit its output to a single line to be stored properly in the environment variable.
For instance, if you run it like this:
BatchSet MyVar Vol
Then you'll only get the first line of the Vol command's output
MyVar= Volume on drive C is labeled MyDisk
But all in all, it's a fairly elegant way of doing what you were looking for.
Note that the last line in the batch is simply there to provide visual output. It can be removed altogether.

Find and print the sub-string with wildcard character in a file on windows command prompt

I'm pretty sure this is a simple command but I just couldn't find it anywhere.
Example file content:
A-VERY-LONG-LINE-OF-GARBAGE-VERSION123-CONTINUE-LONG-LINE-OF-GARBAGE
1.)Assume the line is really really long
2.)I need to find out what version it is. I know it contains Version but I wouldn't know it is version123.
3.)What I want is a command that would go through the file looking for the sub-string "VERSION" and if it finds it prints out VERSION123 instead of the super duper long line that would most probably causes the system to freeze.
Thank you
Assuming that version is purely numeric and does not start with zero, the following should do it:
set VAR=A-VERY-LONG-LINE-OF-GARBAGE-VERSION123-CONTINUE-LONG-LINE-OF-GARBAGE
set /A VAR=%VAR:*VERSION=%
echo VERSION%VAR%
If there occur multiple VERSION portions, the first one is taken.
Note, that this works for Windows command prompt (cmd.exe) only, it will not work for MS-DOS (command.com) due to set /A which is not supported there (I'm even not sure whether the string substitution syntax works there)!
In case the version code is not purely numeric, you might use the following:
set VAR=A-VERY-LONG-LINE-OF-GARBAGE-VERSION123-CONTINUE-LONG-LINE-OF-GARBAGE
set VAR=%VAR:*VERSION=%
for /F "tokens=1 delims=- eol=-" %%L in ("%VAR%") do (set VAR=%%L)
echo VERSION%VAR%
This relies on the fact that the - character delimits the version code.
If you want to try this in the command prompt directly rather than in a batch file, replace %%L by %L (twice).

`)`was unexpected at this time.

I am running a batch file on Windows 7 and running into this error (I have narrowed down the error to the following line):
FOR /F "delims=" %%I in ('echo %RegVal%') do set sasroot=%%~sI
Where Regval is the file path of a given software, which in this case (on my Win7 machine) is:
RegVal = C:\Program Files\SAS 9.2_M3_10w37\SASFoundation\9.2(32-bit)
This same script used to work on Windows Vista, although I suspect it may be that there a parenthesis in RegVal now as it was previousy C :\Program Files\SAS 9.2_M3_10w37\SASFoundation\ on my previous Vista machine.
You suspection is correct.
To get around it, enclose your variable into doublequotes (You remove them again with the ~ in the setcommand)
FOR /F "delims=" %%I in ('echo "%RegVal%"') do set sasroot=%%~sI
I suggest you create a file with the value of RegVal in it, then parse it using the FOR loop:
echo %RegVal%>C:\SomeFile.txt
FOR /F "delims=" %%I in (C:\SomeFile.txt) do set sasroot=%%~sI
This should help you get around your problem.
Stephan's solution is much simpler, but I'll explain my solution anyway, which might prove useful in some cases.
When the FOR command parses the data specified in the IN part using a command, it replaces the command with the result of the command, then runs the FOR command. For example, with the question above, the FOR command that will be executed after expanding echo %RegVal% is:
FOR /F "delims=" %%I in (C:\Program Files\SAS 9.2_M3_10w37\SASFoundation\9.2(32-bit)) do set sasroot=%%~sI
Thus, when the parser hits the first closing parenthesis, it will stop, thinking that everything it read before is the text to work on. However, in this case this is wrong, as the first closing parenthesis is part of the string to read; it doesn't indicate the end of the string.
When parsing a file with the FOR command, it will read each line, assign the predefined tokens with the correct values, then execute the code block that follows. Rinse and repeat for every line in the file. But in this case, it will not replace the IN part with each line; it will only parse it and assign values to the tokens. This is the reason why special characters (such as parenthesis) do not create parsing errors in this case.

batch for loop not iterating beyond first entry

My problem is pretty simple, but I'm unsure why I am seeing this behaviour. I want to get a list of parameters down to a single entry at a time so I can do some processing on them. The list of jar files I am processing is separated by a ; delimiter.
set JARS=this.jar;that.jar;and.jar;the.jar;other.jar
for /f "delims=;" %%a in ("%JARS%") do echo.%%a
I'm expecting the script to exit listing as follows.
this.jar
that.jar
and.jar
the.jar
other.jar
C:\>
But the script is instead exiting as follows.
this.jar
C:\>
I'm clearly missing something obvious, but I can't seem to see it.
I'm using Windows 7.
Try this: The semicolon is a separator on a command line so it will delimit the filenames.
set JARS=this.jar;that.jar;and.jar;the.jar;other.jar
for %%a in (%JARS%) do echo.%%a

Resources