Command line - Some questions

Page 1 of 3 123 LastLast

  1. Posts : 122
    Windows 7 Ultimate x32
       #1

    Command line - Some questions


    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


  2. Posts : 5,092
    Windows 7 32 bit
       #2

    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


  3. Posts : 122
    Windows 7 Ultimate x32
    Thread Starter
       #3

    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


  4. Posts : 122
    Windows 7 Ultimate x32
    Thread Starter
       #4
      My Computer


  5. Posts : 5,092
    Windows 7 32 bit
       #5

    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


  6. Posts : 122
    Windows 7 Ultimate x32
    Thread Starter
       #6

    Thank you so much.
    I'll update if I get a solution.
      My Computer


  7. Posts : 5,092
    Windows 7 32 bit
       #7

    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


  8. Posts : 122
    Windows 7 Ultimate x32
    Thread Starter
       #8

    Unfortunately I don't. :)
    I appreciate your kindness.

    batch file - Command line - Some questions - Super User
      My Computer


  9. Posts : 5,092
    Windows 7 32 bit
       #9

    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


  10. Posts : 122
    Windows 7 Ultimate x32
    Thread Starter
       #10

    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


 
Page 1 of 3 123 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 12:45.
Find Us