Command line - Some questions

Yaron

New member
Member
VIP
Local time
2:46 PM
Messages
122
I've created a batch file for zipping selected items in a folder.
(The file is placed in 'Send to').


If I use the following code:
for %%* in (.) do set CurrentFolder=%%~n* "C:\Program Files\WinRar\WinRar" a -afzip "%CurrentFolder%.xpi"

  • The file name is that of the current folder (correct).
  • All files are archived whether I select one file or multiple files (wrong).
  • Selected folders are not archived (wrong).
If I use the following code:
set file=%~f1 "C:\Program Files\WinRar\WinRar" a -afzip "%file:~0,-4%.xpi" %1

  • The file name is that of the file on which I right click (correct).
  • Only that file is archived even if multiple files are selected (wrong).


How can I know if a single item is selected or more?
What's the condition syntax?
How can I include folders in the archive?


Thank you.
 

My Computer

OS
Windows 7 Ultimate x32
The %1 should be wrapped in double quotes in case there's a space in the file path.

But as to multiple selections, this is what I was talking about with "single instance" behavior. What Explorer does is launch the program for each file selected during the right click. The only way to avoid getting a copy of the program per file is single instance. Meaning the first copy of the program to run has to get control of some system object or file. Subsequent copies try and fail, since the first already has the resource. So secondary copies use some means to transfer the command tail to the first copy. The first copy is expecting this and has the mechanism to receive it.

It's tough to do with batch. A better choice may be a scripting language. There ae many free ones. Python, AutoHotkey, VBScript etc..

Passing the command tail uses IPC or InterProcess Communication. There are many methods. Which is "easy" tends to vary with the language used. But you might check some VBScript or Python archives. You might find some already written that may be adaptable to your task.

Edit: this site is a good source:
http://www.planetsourcecode.com/vb/default.asp?lngWId=1
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Thank you.
You're a good teacher. :)

The %1 was wrapped in double quotes when I ran it. The mistake was in the pasting here.

I'll try that site and also write to the developer of the script you mentioned.
 

My Computer

OS
Windows 7 Ultimate x32
You're welcome. If I did VB regularly I'd be on that planet source code all the time. Many good libraries free to use there.
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Thank you so much.
I'll update if I get a solution.
 

My Computer

OS
Windows 7 Ultimate x32
Ok. Btw if you have Visual Basic I believe it does single instance just by clicking an option. I'm not sure about VB script.
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
I found one in VBScript"http
? vbscript ? Ensure single instance of the script running at time.

At the bottom of the function where it has the instnace count, test for 1, if first instance overwrite temp file with the file or foldername from the command line. If the count is greater than 1, append file or folder name to the temp file. In the first instance you need to set some timer or other means to guess when additional instances have quit appending data. Next step of main program is to read the temp file and process, then quit.

Code:
If ncount = 1 then
' overwrite file with command tail here
Elseif ncount>1 then
' The script got executed more than once. Script EXIT
' but only after appending command tail to file
' add code to append file with command tail here
wscript.quit
Edit: As good programming practice you would want to return the instance number out of the function(make it a function instead of subroutine etc..) instead of handling everything inside it. But it makes it clearer to show what to do inline I think.
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Many Thanks.

When using
Code:
for %%* in (.) do set CurrentFolder=%%~n*
"C:\Program Files\WinRar\WinRar" a -afzip -r "%CurrentFolder%.xpi"
, multiple files are archived into a single file.

Do you still think the script might help?
I'm not a pro. :)
 

My Computer

OS
Windows 7 Ultimate x32
You shouldn't use batch because you can run whatever you want from VBScript. The only processing of files should be done by the first instance of the script. Any new copies of the script should just hand the file or folder path to the first copy. Once you have that perfected then you can put any commands in.

In fact I would be surprised if WinRar didn't have an option for a file with the list of files to add to the archive. The first copy of the script builds this list. When no more programs come up handing it more filenames, it runs WinRar.

If you post what you are looking for on a VBS forum someone may take you the rest of the way. I do a VBS script about one every 3 months and only a few lines. It's not my strong suit. :)
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Thank you for your patience and kindness.
I didn't think it would be so complicated. :)
I'll keep looking for a solution in different forums. I'll update.
 

My Computer

OS
Windows 7 Ultimate x32
Yeah, Explorer firing off a program copy for each file makes it funky. Another approach you could do is Shift Right click and do Copy As Path and paste the filenames all at once to a text file. The result looks like this:

"C:\download\bbceditor.zip"
"C:\download\bluegriffon-1.7.2.exe"
"C:\download\BootUITuner.zip"
"C:\download\BrowserBunch.zip"
"C:\download\BRU_Setup_WinNTx64.exe"
"C:\download\ccsetup406.zip"

You may need to use search and replace to get rid of the quotes.
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Yet another approach would be to use my hotkey utility Selector. For it to work you would make a shortcut for WinRar with the command line. You must leave the command line so that the files to be added to the archive can be tacked on at the end. You hit the hotkey to Selector and drag the WinRar shortcut and drop it on the List. Now that shortcut will remain in the list until removed.

The way it would work is, you select a bunch of files in Explorer and hit the Selector hotkey. Click on the WinRar shortcut in the list, and hit the Go Button. Selector adds all the filenames it copied to the clipboard, to the shortcut Target line and launches the shortcut.

See the Readme.txt file and also you can press F1 while Selector is the active window, to get help.
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Ok. Thank you. :)

Edit: That Move icon does look nice. I'll try it out. :)
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Great.

Thank you for your kind help.

Have a nice weekend.
 

My Computer

OS
Windows 7 Ultimate x32
You too. If you get everything settled how you would process the files I may be able to throw together a short program. From inside scripting it's much easier to do things like split off the main filename with no path or extension and add the desired extension. That type of thing is tougher with only batch. A batch expert may have no trouble but I use batch for the easy stuff. I'm no expert.

If I know the exact program usage with params for a particular case I may be able to generalize it using AutoHotkey or a similar scripting language. Whichever way ends up easier. :)
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
You don't give up. :)
I appreciate the perseverance.

Let’s assume we have the following structure:
A folder called TestMain.
TestMain contains 2 files: A, B.
And also a folder called TestSub.

The default “Compress to” works as follows:
If you only select file A (and right-click), you get “Compress to A.zip”.
If you only select folder TestSub (and right-click), you get “Compress to TestSub.zip”.
If you select more than one item, you get “Compress to TestMain.zip” and the selected items are compressed into a single file.

Trying to emulate this behavior with "Compress to XPI":
When using
for %%a in (.) do set CurrentFolder=%%~nxa
"C:\Program Files\WinRar\WinRar" a -afzip -r "%CurrentFolder%.xpi"
WinRar compresses all items in ZIP format, and the file name is TestMain.xpi (the switch “-r”: include sub folders).
- That's good if you select all items.

When using
set file=%~f1
"C:\Program Files\WinRar\WinRar" a -afzip "%file:~0,-4%.xpi" "%1"
Only the file on which you right-click is compressed.
- That's good if you select one file.

- We have to get the name of TestSub if it's the only selected item.
- We have to determine whether one item is selected or more.
- We have to be able to compress 2 selected items into a single file.

***
And thank you again. What can I say? :)
 

My Computer

OS
Windows 7 Ultimate x32
Back
Top