Page 1 of 1

Noob Help setting up batch file using %1 variable

Posted: 14 May 2011 04:44
by salinsky
Hi, I recently setup a custom context menu in Win7 which passes the selected filename to a batch file, but my problem is that is requires an additional command to be sent. I'll show you what I mean.

Here's my split.bat batch file:

Code: Select all

"C:\splitMKV\splitMKV.exe" %1% /split:2500
pause
Code the Context menu is running which isn't a problem:

Code: Select all

"C:\split.bat" "%1"
Manual code, withOUT batch, works fine:

Code: Select all

C:\> C:\splitMKV\splitMKV.exe C:\1ilupm.mkv /split:2500
Batch with variable pass, doesn't work (passing the variable is just fine, however):

Code: Select all

C:\>"C:\splitMKV\splitMKV.exe" "C:\1ilupm.mkv"2500

Cannot find: C:\1ilupm.mkv2500
I have tried various tactics using quotes but to no avail. I must be missing something when adding the extra necessary /split command since it is concatenating that part of the line. Any guides any of you seasoned users could point me to would be helpful in the future as well.

Many thanks.

Re: Noob Help setting up batch file using %1 variable

Posted: 14 May 2011 05:13
by orange_batch
Just some syntax errors, friend.

%1% <- typo, arguments don't use enclosing %'s like variables

"C:\1ilupm.mkv"2500 <- ? missing space and /split: between the path and 2500


From Command Prompt (quotes not necessary unless the path has any spaces):

Code: Select all

split "C:\1ilupm.mkv"

split.bat:

Code: Select all

"C:\splitMKV\splitMKV.exe" "%~1" /split:2500
pause


%~1 is the same as %1 but it strips any surrounding quotation marks.
So "%~1" will make sure any path with or without quotation marks is processed properly.

Re: Noob Help setting up batch file using %1 variable

Posted: 14 May 2011 08:44
by salinsky
That seems to have done it. Thanks so much orange_batch.

What's the best way in your opinion to go about learning more about batch proficiency? Any particular site other than just researching these forums?

Thanks again.

Re: Noob Help setting up batch file using %1 variable

Posted: 14 May 2011 10:29
by orange_batch
I use...

Mainly: http://ss64.com/nt/ (also click Syntax at the top, the first set of links being the most important)

Google for various questions/info,

MSDN:
Command Prompt: http://msdn.microsoft.com/en-us/library/bb490890.aspx

Getting more technical on the command line with VBScript and its resources (damn 2 url limit!):
VBScript: msdn.microsoft.com/en-us/library/d1wf56tt%28v=vs.85%29.aspx

Wscript: msdn.microsoft.com/en-us/library/at5ydy31%28v=vs.85%29.aspx
(kind of a must-know for vbscript, run vbscripts on the command line through cscript.exe)

WMI: msdn.microsoft.com/en-us/library/aa394572%28v=vs.85%29.aspx
(used best via vbscript, but command line's WMIC.exe works too)
The totally awesome must-have Scriptomatic for WMI:
microsoft.com/downloads/en/details.aspx?FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178

WsShell: msdn.microsoft.com/en-us/library/aew9yb99%28v=vs.85%29.aspx
(this windows object can do some particular/cool stuff)

As for being stumped, I think this place is the best to ask a question. A handful of experienced people like me do research here, so it has a lot of info on the deepest technicalities.

Once you know the ins and outs of DOS, VBScript takes some thought, but it's actually not so daunting. I started learning it not a week ago and I've made some great tools. Heck I'll share a few:

tools.vbs
Command lines:
cscript tools.vbs //nologo sleep seconds
Self-explanatory. Better than using the ping trick to create delays.

cscript tools.vbs //nologo calc expression
Calculates 16-digit numbers. Dunno why, but it's better than DOS's 32-bit limitation, short of splitting it.

cscript tools.vbs //nologo tasks (megabytes)
Lists running processes (excluding explorer.exe and svchost.exe) in the format:
___.exe*memory usage in megabytes*command line
Setting the megabytes argument will show only processes using more than the value. It is optional.
I like it better than tasklist. Should work on XP Home too, where tasklist is nonexistent.
Notes:
-Rounds memory usage of each process to the nearest megabyte.
-Removes all quotation marks from the command line property, for DOS's sake.

Code: Select all

select case wscript.arguments(0)
case "sleep"
wscript.sleep wscript.arguments(1)*1000
case "calc"
wscript.echo eval(wscript.arguments(1))
case "tasks"
if wscript.arguments.count=2 then a=wscript.arguments(1) else a=0
set b=getobject("winmgmts:")
set c=b.execquery("select name,workingsetsize,commandline from win32_process")
for each d in c
if d.name<>"System Idle Process" and d.name<>"System" and d.name<>"svchost.exe" and d.name<>"explorer.exe" then
e=round(d.workingsetsize/1000000)
if not isnull(d.commandline) then f=replace(d.commandline,"""","") else f=null
if e>cint(a) then g=g&d.name&"*"&e&"*"&f&vblf
end if
next
wscript.echo g
end select

Like the above, using WMI is as easy as:

Code: Select all

set a=getobject("winmgmts:")
set b=a.execquery("select whatever_property from win32_whatever_resource")
for each c in b
wscript.echo c.whatever_property
next