0

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.

1 Answer 1

0

This should help you some...

@echo off
::Save passed parameter to a variable...
set _loc=%~1

::Check if passed parameter is a file/folder and process accordingly...
if exist "%~s1\*" (echo Processing all files in folder %_loc% && echo. && goto :ProcFolders) else (echo Processing files && echo. && goto :ProcFile)
goto :exit

::When passed parameter is a folder...
:ProcFolders
::Check if folder is empty...
dir /b /a:-d "%_loc%" >nul 2>&1 && (echo  Non-empty folder!) || (echo Checking for sub-folders... && goto :CheckSubDir)

:CheckSubDir
dir /b /a:-d /s "%_loc%\*">nul 2>&1 && (echo. && echo Subfolders/files exist) || (echo. && echo No file or subfolders found && goto :exit)

::If folder has files or subfolders with files, continue here.
for /f %%g in ('dir /b /a:-d /s "%_loc%\*.*"') do echo Processing %%g &&    echo.
goto :exit

::When passed parameter is a file...
:ProcFile
if "%~1"=="" goto :exit
echo Processing %~1 && echo.
shift
goto :ProcFile
goto :exit

:exit
pause
exit /b

The script checks if the passed parameter is a folder or individual files. If files, it will shift through the passed parameters and process each file individually. Be aware that you may hit a limitation in the number of characters that can be passed as an argument from the command prompt or drag and drop, so if a lot of files need to be processed, just process the entire folder. I've been able to pass 50+ files without a problem, but their names were relatively short. I've modified the script further to check for sub-directories as well.

Hope it helps!

6
  • Thank you so much. I really appreciate your help. I was notified about your reply about an hour ago, and I've been trying to integrate the zip command into your script.
    – Yaron
    Commented Apr 3, 2014 at 1:01
  • I'm not a developer, and it's complicated for me. If I add "C:\Program Files\WinRar\WinRar" a -afzip "%_loc.xpi" to :ProcFile, each file is archived separately and this isn't what I want. Also, if I select all items and right-click on a file, folders are not archived. I'd be grateful for further assistance. *** Pressing Enter in the Comment section, submits the content and I have to start a new one (you can only edit 5 times).
    – Yaron
    Commented Apr 3, 2014 at 1:18
  • @Yaron I will modify the script later on tonight.
    – JSanchez
    Commented Apr 3, 2014 at 2:01
  • Thanks again. It's really kind of you. I've googled a bit and found that when adding the switch "-r", WinRar includes folders in the archive. So, apparently the following structure should work: If (one item is selected) set file=%~f1 "C:\Program Files\WinRar\WinRar" a -afzip "%file:~0,-4%.xpi" %1 Else for %%* in (.) do set CurrentFolder=%%~n* "C:\Program Files\WinRar\WinRar" a -afzip -r "%CurrentFolder%.xpi" *** Two problems: 1) How can I know how many items are selected? 2) If only a folder is selected, the code should be changed.
    – Yaron
    Commented Apr 3, 2014 at 2:39
  • I'll use the name of the first item, or folder, as the archive name. Shouldn't be that difficult to implement. Famous last words. ;-)
    – JSanchez
    Commented Apr 3, 2014 at 2:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .