Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Batch file variable isn't setting in a loop

Tue Feb 11, 2014 5:40 pm

for %%f in (*.mkv) do (
set wtf=%%f
echo %wtf%
)


But echo %wtf% always gives a new line. Why?

E:\Cat's Eye (Season 1) [H264, 480p] [BBT-RMX]>(
set wtf=[BBT-RMX][TMUsubs] Cat's Eye - 36 [BD] [155486BC].mkv
 echo

)
ECHO is on.


And why is it always saying ECHO is on?
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 5:49 pm

The set command is being ignored (don't ask me why).

Then echo is left with nothing, and just "echo" will print out its state.

As a dirty fix, you can kill the "set" and just do:
echo %%f


Also, DOS batch files suck. If you want to save yourself hours of wasted work, do this Perl/Python/PHP. Trust me, been there, done that, got the whole set of sweaters.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 5:53 pm

Crayon Shin Chan wrote:
for %%f in (*.mkv) do (
set wtf=%%f
echo %wtf%
)


But echo %wtf% always gives a new line. Why?

E:\Cat's Eye (Season 1) [H264, 480p] [BBT-RMX]>(
set wtf=[BBT-RMX][TMUsubs] Cat's Eye - 36 [BD] [155486BC].mkv
 echo

)
ECHO is on.


And why is it always saying ECHO is on?

For some reason "wtf" was not set. So you ended up with just the command "echo". Type that in your Command Prompt, it will say "ECHO is on.".

Just use %%F in your real command? Or try "echo %%F" directly?

Looks like setting variables can be tricky. I would recommend against doing it. Since I think you don't really need a variable anyway for what you are trying to do. The shell provides decent string manipulation functionality to do what most people need.

morphine wrote:
Also, DOS batch files suck. If you want to save yourself hours of wasted work, do this Perl/Python/PHP. Trust me, been there, done that, got the whole set of sweaters.
Strictly speaking by using for and the loop, it is not DOS batch files any more. It is more with the NT command shell. It is ok decent. Most admins still prefer that over the full-blown "I am an admin not a bloody programmer!" PowerShell.
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
Crayon Shin Chan
Minister of Gerbil Affairs
Topic Author
Posts: 2313
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia
Contact:

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 5:54 pm

Yeah, I think I'll go back to Python. Thanks anyway.
Edit: well what I wanted to do was replace the .mkv with .mp4, and there are so many arcane special characters here that I figured it would be easier to copy the filename into another variable and perform the string manipulation there.
Mothership: FX-8350, 12GB DDR3, M5A99X EVO, MSI GTX 1070 Sea Hawk, Crucial MX500 500GB
Supply ship: [email protected], 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Thinkpad X230
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 5:58 pm

Flying Fox wrote:
It is more with the NT command shell. It is ok decent

Please, send me some of what you're smoking :P
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 6:05 pm

Unix has been doing this for years, pick up some bash shell scripting and some sed and awk and you are good to go.
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 6:20 pm

morphine wrote:
Flying Fox wrote:
It is more with the NT command shell. It is ok decent

Please, send me some of what you're smoking :P

http://technet.microsoft.com/en-us/libr ... 23564.aspx
(I am slowly getting accustomed to pushd/popd commands myself)
http://stackoverflow.com/questions/1489 ... bat-vs-cmd

And you just need to bring up the reference for the "for" command by typing "for /?". I still couldn't quite get my head around all the little details.

Granted, NT cmd scripting is not as generic and as powerful as other full-blown scripting languages like python, but it is not the lame duck that DOS batch script is. And sometimes, depending on what needs to be done, it may be enough.
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 6:25 pm

I've read all that and more.

The problem is, that any sort of data manipulation (namely, strings) is next to impossible to do without resorting to incredibly complicated code and hacks.

For part of a TR benchmark file processing script, I once tried using a DOS batch file. After I spent over an hour doing something that I eventually just implemented in PHP in a few minutes, I ran away from batch files yipping like a beaten dog.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
 
Scrotos
Graphmaster Gerbil
Posts: 1109
Joined: Tue Oct 02, 2007 12:57 pm
Location: Denver, CO.

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 6:29 pm

http://www.robvanderwoude.com/variableexpansion.php

SETLOCAL ENABLEDELAYEDEXPANSION
for %%f in (*.mkv) do (
set wtf=!wtf!%%f
echo %wtf%
)
ENDLOCAL


I didn't try that, pressed for time. But maybe that gets you moving towards a solution. Been a while since I messed with for loops and delayed expansion.
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Batch file variable isn't setting in a loop

Tue Feb 11, 2014 6:56 pm

morphine wrote:
The problem is, that any sort of data manipulation (namely, strings) is next to impossible to do without resorting to incredibly complicated code and hacks.

For part of a TR benchmark file processing script, I once tried using a DOS batch file. After I spent over an hour doing something that I eventually just implemented in PHP in a few minutes, I ran away from batch files yipping like a beaten dog.

As I said, depending on the needs. For some quick and dirty file name manipulation like getting extension, name without extension etc., they are adequate. I never said it is the be-all-end-all.
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Batch file variable isn't setting in a loop

Wed Feb 12, 2014 4:18 am

If you want to stick to native tools and are running Windows 7 or later, use Windows PowerShell. It is available for earlier Windows versions (back to XP) too, but it needs to be downloaded and installed separately.

Otherwise, I'd say install Python. Or Cygwin if you want a full suite of *NIX-compatible scripting tools.
Nostalgia isn't what it used to be.
 
Glorious
Gerbilus Supremus
Posts: 12343
Joined: Tue Aug 27, 2002 6:35 pm

Re: Batch file variable isn't setting in a loop

Wed Feb 12, 2014 6:44 am

JBI wrote:
Or Cygwin if you want a full suite of *NIX-compatible scripting tools.


Which finally became available in x64 earlier last year.

Yay.

But I'd definitely have to agree it's either that or powershell, It doesn't make any sense (IMHO) to learn the bizarre behaviors of such a limited tool. It might superficially seem easier to just plunk some stuff in a *.bat, but once you start trying to figure out such byzantine semantics, you're wasting time you'll never get back. Use the right tool/evironment the first time (again, in my opinion) and whatever you learn will actually be useful in the future and probably won't take you as long either.

I'd vote for cygwin because what you'll learn will be useful in windows and beyond, as virtually everything has a similar *nix compatibility layer. For instance, OpenVMS is dying, but there is GNV (GNU's not VMS :P) which provides something very similar to cygwin for that platform (and even though Dave Cutler is responsible for both NT and VMS and neither have a proper fork(), NT's various APIs map even better).

Of course, there are the three Ps (Python, Perl, PHP) but at that point, you're like, programming. Which is fine too, but everything in it's place (again, in my opinion).
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Batch file variable isn't setting in a loop

Wed Feb 12, 2014 8:32 am

Glorious wrote:
JBI wrote:
Or Cygwin if you want a full suite of *NIX-compatible scripting tools.

Which finally became available in x64 earlier last year.

I imagine 64-bit Cygwin was not a high priority for Red Hat, given that A) the 32-bit version was usable on x86-64; and B) the rise of virtualization means Cygwin is less important than it used to be, since if you need *NIX on Windows it is pretty easy these days to just run a full-blown real Linux distro in a VM.

But yes, yay. It is nice to finally have a native 64-bit port.

Glorious wrote:
I'd vote for cygwin because what you'll learn will be useful in windows and beyond, as virtually everything has a similar *nix compatibility layer. For instance, OpenVMS is dying, but there is GNV (GNU's not VMS :P) which provides something very similar to cygwin for that platform (and even though Dave Cutler is responsible for both NT and VMS and neither have a proper fork(), NT's various APIs map even better).

There's a technical reason for the lack of fork(). In the most common use case the forked process is going to immediately exec() something else, so the system has gone through all the effort of setting up an entire new process context that is going to be immediately thrown away. Implementing copy-on-write semantics in the virtual memory manager can mitigate this to a large extent, but I suspect that when VMS and NT were being designed (and hardware was much slower), fork() was viewed as encouraging inefficient coding practices. Yes, it's a very elegant concept; but elegant does not always mean efficient!

This is also why fork() is very slow in Cygwin compared to native *NIX platforms. Since it doesn't map well to any underlying Windows system calls it can't use copy-on-write, so all writable memory pages must be copied (and then immediately discarded in the case of a fork/exec pair).

Glorious wrote:
Of course, there are the three Ps (Python, Perl, PHP) but at that point, you're like, programming. Which is fine too, but everything in it's place (again, in my opinion).

PowerShell is an awful lot like "real" programming too (though I agree less so than the "three Ps")...
Nostalgia isn't what it used to be.
 
jihadjoe
Gerbil Elite
Posts: 835
Joined: Mon Dec 06, 2010 11:34 am

Re: Batch file variable isn't setting in a loop

Wed Feb 12, 2014 10:32 am

The for loop would've let you do the file manipulation by itself. I'm assuming you want to remux a bunch of mkv files into mp4.

for %%F in (*.mkv) do somethingsomething.exe %%~nF.mkv -operator -whatever %%~nF.mp4


If you were using ffmpeg (depending on how your .exe was compiled you might have to add "-strict -2" to the options list):
for %%F in (*.mkv) do ffmpeg.exe -i "%%F" -vcodec copy -acodec aac "%%~nF.mp4"
 
LASR
Gerbil First Class
Posts: 147
Joined: Fri Jan 10, 2014 9:35 pm

Re: Batch file variable isn't setting in a loop

Thu Feb 13, 2014 1:34 am

The more I try to write sh scripts, the more I realize that these were created in the 1970s.

IMO something like python should be the standard - if it isn't already.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Batch file variable isn't setting in a loop

Thu Feb 13, 2014 7:36 am

LASR wrote:
The more I try to write sh scripts, the more I realize that these were created in the 1970s.

IMO something like python should be the standard - if it isn't already.

I'd say use of Python has definitely increased, but sh (and derivatives like ash and bash) is still the defacto standard for scripting in *NIX. Since it is also the standard CLI interpreter you can pretty much count on it being present on every *NIX system, whereas some stripped down installations may lack a Python interpreter by default. The situation with sh as a scripting tool is somewhat like that of vi as an editor -- a legacy tool with roots in the '70s, counter-intuitive if you're not familiar with it; but lightweight, gets the job done, and pretty much guaranteed to be pre-installed on every *NIX system (so not going away any time soon).
Nostalgia isn't what it used to be.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On