Drag and Drop multiple files onto batch - batch-file

Please keep in mind that I'm a newb.
I need to drag and drop 244 files with a .tex extension into a batch that then creates a .png that I can edit. Simply selecting them all and dropping them in isn't doing the trick, so someone wrote me a code that I have no idea how to properly use:
for %%f in (*.tex) do c:\python27\python.exe tools/textool.py -x -v -ra %%f
The .tex files are all in the same directory of the batch, which is in C:\users\myname\downloads\folder1\folder2\folder3. Hope you can help.

Try this batch code:
#echo off
if "%~1" == "" goto :EOF
for %%I in ("%~1\*.tex") do C:\python27\python.exe "%~dp0tools\textool.py" -x -v -ra "%%~fI"
The batch file does nothing if called without a parameter.
But in case of batch file is called with a parameter, it expects without verification (could be added) that the (first) parameter specifies a folder path in which all *.tex files should be processed by the Python script.
An alternate solution:
#echo off
if "%~1" == "" goto :EOF
pushd "%~1"
if errorlevel 1 goto :EOF
for %%I in ("*.tex") do C:\python27\python.exe "%~dp0tools\textool.py" -x -v -ra "%%~nxI"
popd
The directory specified as parameter becomes the current working directory for this batch file and the Python script gets just file name with file extension passed via command line parameter.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains:
%~1 ... first parameter without surrounding double quotes
%~dp0 ... drive and path of argument 0 which is path of folder containing the batch file ending with a backslash.
echo /?
for /?
goto /?
if /?
popd /?
pushd /?
Note: It would be much faster to modify the Python script to search itself for all *.tex files in folder path specified on command line as parameter for conversion of each found file matching the pattern to a *.png file.

Related

How can I find my error in the batch script?

The code below should archive some files by moving them into a subfolder. The batch file asks the user for the folder path. Then a subfolder should be created and if that was successful, it should move all files in the user input directory into the subdirectory. It works, but it closes although using pause. It does not output anything about a syntax error or anything at all. Please let me know if somebody notices something.
#echo off
SETLOCAL EnableDelayedExpansion
echo Insert path:
set /p path=
echo the path is %path%
cd %path%
echo The files will be moved to a new folder
pause
mkdir %path%\archived_files
IF EXIST "archived_files" (
for /f %%A in ('DIR /A /D /B') do (
echo %%A && move /Y %path%\%%A %path%\archived_files)
echo Folder "archived_files" created or already exists
) else ( echo Folder "archived_files" does not exist )
echo the files have been transferred
pause
ENDLOCAL
I suggest to use this batch file for the file moving task.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BatchFileName=%~nx0"
set "BatchFilePath=%~dp0"
set "UserPath=%~1"
if defined UserPath goto ChangeFolder
:UserPrompt
set "UserPath="
set /P "UserPath=Enter path: "
rem Has the user not entered a string?
if not defined UserPath goto UserPrompt
rem Remove all double quotes from input string.
set "UserPath=%UserPath:"=%"
rem Has the user entered just one or more double quotes?
if not defined UserPath goto UserPrompt
:ChangeFolder
pushd "%UserPath%" 2>nul || (echo Folder "%UserPath%" does not exist.& goto UserPrompt)
for /F "eol=| delims=" %%I in ('dir /A-D /B 2^>nul') do goto CreateSubfolder
echo The folder does not contain any file to archive.& goto EndBatch
:CreateSubfolder
md "archived_files" 2>nul
if not exist "archived_files\" echo Failed to create subfolder: "archived_files"& goto EndBatch
rem It must be avoided that the currently running batch file is moved too.
set "ExcludeFileOption="
for %%I in ("%UserPath%\") do set "CurrentFolderPath=%%~dpI"
if "%CurrentFolderPath%" == "%BatchFilePath%" set "ExcludeFileOption= /XF "%BatchFileName%""
rem The command MOVE used with wildcard * does not move hidden files. A FOR loop
rem with MOVE is slow in comparison to usage of ROBOCOPY to move really all files.
rem The ROBOCOPY option /IS can be removed to avoid moving same files existing
rem already in the subfolder archived_files from a previous batch execution.
echo The files are moved to a new folder.
%SystemRoot%\System32\robocopy.exe . archived_files%ExcludeFileOption% /MOV /R:2 /W:5 /IS /NDL /NFL /NJH /NJS
if not errorlevel 2 if errorlevel 1 echo All files are moved successfully.
:EndBatch
popd
endlocal
pause
The batch file can be started with a a folder path as argument. So it is possible to right click on the batch file and click in opened context menu in submenu Send to on item Desktop (create shortcut). The .lnk file created on the user´s desktop can be renamed now also via context menu or key F2 to whatever name is useful like Archive files. Then the shortcut file can be cut with Ctrl+X and pasted with Ctrl+V in the folder %APPDATA%\Microsoft\Windows\SendTo to have in Send to context submenu the menu item Archive files. This makes it possible to right click on a folder and click in opened context menu in submenu Send to on Archive files to run the batch file without the need to enter a folder path manually.
The batch file prompts the user for the path if not started with a folder path as first argument or the folder cannot be found at all. This user prompt is done using a safe method. The batch file makes the passed or entered folder temporarily the current folder for the remaining commands using PUSHD and POPD instead of CD to work also with UNC paths.
There is checked next if the folder contains any file at all. Otherwise the user is informed that the directory does not contain files to archive and batch file ends without any further action.
The file movement is done with ROBOCOPY for the reasons described in a remark in the batch file which requires Windows Vista or a newer Windows version or Windows Server 2003 or a newer Windows server version.
I recommend to see also:
Debugging a batch file which answers your question.
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"? It explains why path as name for the environment variable to assign the user entered path is a really bad idea.
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? It explains the reasons for using the additional code to evaluate the string entered by the user.
Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains the recommended syntax for the (re)definition of an environment variable and why using this syntax.
Syntax error in one of two almost-identical batch scripts: ")" cannot be processed syntactically here describes several common issues made by beginners in batch file coding like not enclosing a file/folder path in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~nx0, %~dp0 and %~1 whereby argument 0 is always the batch file itself.
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
pause /?
popd /?
pushd /?
rem /?
robocopy /?
set /?
setlocal /?
Other useful documentations used to write this code:
single line with multiple commands using Windows batch file
the Microsoft documentations for the used Windows Commands
the SS64 documentations for the used Windows CMD commands, especially:
ROBOCOPY.exe
ROBOCOPY Exit Codes
the Microsoft documentation about Using command redirection operators
and the SS64 documentation How-to: Redirection
Note: The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.

Batch script calling its own name instead of files of a certain type?

cd %cd%
ffmpeg -i %cd%/%04d.png out.mp4
A script with just this in it works completely fine and outputs exactly what I need it to, but:
:: A simple script to convert a png or jpg image sequence to an
mp4 file with ffmpeg
cls
#echo off
title PNG2MP4
color C
echo Ensure you have ffmpeg installed and setup in your environment variables or this script won't work.
:QUERY
echo This will convert all image files in the following directory to a single mp4,
echo %cd%
echo are the files PNGs or JPEGs(PNG/P/JPG/J/CANCEL)?
set/p "ch=>"
if /I %ch%==PNG goto CONVERTPNG
if /I %ch%==P goto CONVERTPNG
if /I %ch%==JPG goto CONVERTJPG
if /I %ch%==J goto CONVERTJPG
if /I %ch%==CANCEL goto :eof
echo Invalid choice & goto QUERY
:CONVERTPNG
cd %cd%
ffmpeg -i %cd%/%04d.png out.mp4
:CONVERTJPG
cd %cd%
ffmpeg -i %cd%/%04d.jpg out.mp4
This more complex version of the script fails, outputting:
C:\tmp/img2mp4.bat4d.jpg: No such file or directory
Why is it no longer calling the files that it did before and is there an easy fix for this?
Here is my suggestion for the batch file:
#echo off
rem A simple script to convert a png or jpg image sequence to an mp4 file with ffmpeg
cls
title PNG2MP4
color C
echo Ensure you have ffmpeg installed and setup in your environment variables
echo or this script won't work.
echo/
echo This will convert all image files in the following directory to a single mp4:
echo/
echo %cd%
echo/
%SystemRoot%\System32\choice.exe /C PJC /N /M "Are the files PNGs or JPEGs or Cancel (P/J/C)? "
if errorlevel 3 color & goto :EOF
echo/
if errorlevel 2 (
ffmpeg.exe -i %%04d.jpg out.mp4
) else (
ffmpeg.exe -i %%04d.png out.mp4
)
color
The character % must be escaped in a batch file with one more % to be interpreted as literal character which was the main problem causing the batch file not working as expected. %0 references the string used to start the batch file which was img2mp4.bat. So %04d.jpg concatenated img2mp4.bat with 4d.jpg and the result was running ffmpeg.exe with img2mp4.bat4d.jpg as file name instead of the argument string %04d.jpg.
To reference one or more files/folders in current directory the file/folders can be simply specified in arguments list of a script or application with no path. This is explained in Microsoft documentation about Naming Files, Paths, and Namespaces. This page describes further that on Windows the directory separator is the backslash character \ and not the forward slash / as on Linux and Mac. / is used on Windows mainly for options as it can be seen on line with command CHOICE because of this character is not possible in file/folder names. - is used on Linux/Mac for options which is possible also in file/folder names even as first character of a file/folder name. So on Windows \ should be always used as directory separator although the Windows kernel functions for file system accesses automatically correct / in file/folder names to \.
CHOICE is much better for prompting a user to take a choice from several offered options than the command SET with option /P. set/p is syntactically not correct at all because of command set should be separated with a space from option /P which should be separated with a space from next argument variable=prompt text. set/p forces cmd.exe to automatically correct the command line to set /p. Batch files should be syntactically correct coded and not depend on automatic error corrections of Windows command processor.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains how to reference batch file arguments.
echo /?
rem /?
cls /?
title /?
color /?
set /?
choice /?
if /?
goto /?
Further I suggest to read:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Single line with multiple commands using Windows batch file
ECHO. FAILS to give text or blank line - Instead use ECHO/
Where does GOTO :EOF return to?

change to directory after searching for the file via DIR

Outcome is to change to directory after searching for the file via DIR through cmd
Location of file is C:\Folder
is the following code possible?
set /a variable= dir document.doc /s /p
cd %%variable%%
Change directory to C:\Folder
try this:
for /f "delims=" %%# in ('dir document.doc /s /b') do (
set "new_dir=%%~dp#"
)
cd /d "%new_dir%"
Run in a command prompt window set /? and read the output help carefully from first to last page.
set /A is for evaluating an arithmetic expression. So the string after set /A is interpreted by Windows command line interpreter as arithmetic expression (formula).
set /a variable= dir document.doc /s /p
This command line outputs on execution the error message missing operator because dir is interpreted as a variable name and also document.doc both most likely not existing and therefore replaced by 0 on evaluating the expression. But cmd.exe expects an operator between those two environment variable names and as there is none like / before variable s, the error message is output.
It is not possible to assign possible multi-line output of a command line like dir document.doc /s /p to an environment variable with command SET.
In a batch file you can use this code:
#echo off
for /R %%I in ("document*.doc") do cd /D "%%~dpI" & goto FoundFile
echo Could not find any document*.doc in %CD% or its subdirectories.
pause
goto :EOF
:FoundFile
echo Found a file document*.doc in directory %CD%.
pause
Command FOR searches for any non hidden file matching the pattern document*.doc in current directory and all non hidden subdirectories. A wildcard character like * or ? must be specified to run a search for a file. If a file is found, the command CD is executed to change to directory of the file and the loop is exited with a jump to a label.
Another solution to really search only for file document.doc:
#echo off
for /F "delims=" %%I in ('dir "document*.doc" /A-D /B /S 2^>nul') do cd /D "%%~dpI" & goto FoundFile
echo Could not find file document.doc in %CD% or its subdirectories.
pause
goto :EOF
:FoundFile
echo Found file document.doc in directory %CD%.
pause
This example shows how to run a command line like dir "document*.doc" /A-D /B /S 2>nul in a separate command process started by FOR with cmd.exe /C with capturing all output lines written to handle STDOUT while in this case redirecting an error message written to handle STDERR to device NUL to suppress it.
The captured output is next processed by FOR line by line with skipping with default options all empty lines and lines starting with a semicolon and splitting up each line into substrings (tokens) using space and tab as delimiters with assigning only first substring to specified loop variable I. This line splitting behavior is disabled by using "delims=" which defines an empty list of delimiters and so no line splitting is possible anymore. In this case it is impossible that a line output by DIR starts with ; and so we don't need to care about eol (end of line) option.
%%~dpI expanding to just drive and path of current file name with path can be also assigned to an environment variable for example with set "FilePath=%%~dpI". And the environment variable FilePath can be referenced in rest of the batch file either with immediate expansion using %FilePath% or with delayed expansion using !FilePath! enclosed the entire argument string containing this variable reference in double quotes for working also for file paths containing a space or one of these characters: &()[]{}^=;!'+,`~
Both batch codes always changes to first found document*.doc respectively document.doc file and ignoring all other files matching the pattern respectively with same name perhaps also found by FOR or DIR in other directories in searched directory tree. The first solution is faster on a large directory tree needs to be searched for the file. But first solution ignores hidden subdirectories and can change to a directory containing for example document_1.doc instead of document.doc.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
echo /?
for /?
goto /?
pause /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

get filename in the batch file each time a new file is placed

My requirement is - i need to read the filename from an input folder say - C:\Encrypt\In and pass it to the command java.exe -jar D:\SYS\src\PI\IN\Cryptage.jar -rc4 -crypt D:\SYS\src\PI\IN\Decrypt\ D:\src\PI\IN\Encrypt\ %VAR1%%VAR2%
i tried doing the one below - but no luck
set VAR1=FOR /R C:\Encrypt\In %F in (*.*) do echo %~nF
set VAR2=ABCD
echo %VAR1%%VAR2% (concatenate the filename with "ABCD" as constant)
java.exe -jar D:\SYS\src\PI\IN\Cryptage.jar -rc4 -crypt D:\SYS\src\PI\IN\Decrypt\ D:\src\PI\IN\Encrypt\ %VAR1%%VAR2%
(pass it here - so that each time a file comes in the input directory the variables can pick up the file names dynamically through the variables)
echo %VAR1%%VAR2% is not working.
Thanks anyway - i achieved it through this :- cd C:\Encrypt\In\ for %%f in (.) do ( rem echo %%~nfAPSI set v=%%~nfAPSI ) echo %v%
Here is a commented batch code for your task:
#echo off
set "ScanFolder=C:\Encrypt\In"
rem The loop runs command DIR to get a list of files with archive attribute set.
rem Directories are ignored even if archive attribute is set on a directory.
rem On each file with archive attribute currently set the archive attribute
rem is removed from file and then the command is started to process the file.
rem After all files with archive attribute were processed, the batch file
rem waits 5 seconds before scanning the folder again. The loop is infinite
rem and can be breaked only by pressing Ctrl+C or closing command prompt
rem window to stop command line interpreter.
:Loop
for /F "delims=" %%F in ('dir /AA-D /B "%ScanFolder%" 2^>nul') do (
%SystemRoot%\System32\attrib.exe -A "%ScanFolder%\%%~nxF"
java.exe -jar D:\SYS\src\PI\IN\Cryptage.jar -rc4 -crypt D:\SYS\src\PI\IN\Decrypt\ D:\src\PI\IN\Encrypt\ "%ScanFolder%\%%~nxF"
)
%SystemRoot%\System32\ping.exe -n 6 127.0.0.1>nul
goto Loop
java.exe should be called with full path enclosed in double quotes if possible as in this case command line interpreter would not always need to search for it in the folders of environment variable PATH.
Note: The batch file calls the new file with full path, file name and extension without anything appended. Of course you can replace %%~nxF at end of line calling java.exe also with %%~nFABCD if this is necessary in your environment.
For an explanation of the used commands and how they work in detail open a command prompt window and execute following commands to see the help of those commands:
attrib /?
dir /?
for /?
ping /?

How to compress each subfolder in a folder into a separate RAR archive using WinRAR?

I am really new to batch file coding and need your help.
I've these directories:
c:\rar\temp1\xy.jpg
c:\rar\temp1\sd.jpg
c:\rar\temp1\dd.jpg
c:\rar\temp2\ss.jpg
c:\rar\temp2\aa.jpg
c:\rar\temp2\sd.jpg
c:\rar\temp3\pp.jpg
c:\rar\temp3\ll.jpg
c:\rar\temp3\kk.jpg
And I want to compress them to this
c:\rar\temp1\temp1.rar
c:\rar\temp2\temp2.rar
c:\rar\temp3\temp3.rar
How could this be done using WinRAR?
This can be done also with WinRAR without using a batch file, not exactly as requested, but similar to what is wanted.
Start WinRAR and navigate to folder c:\rar\.
Select the folders temp1, temp2 and temp3 and click on button Add in the toolbar.
As archive name specify now the folder for the RAR archives, for example c:\rar\.
Switch to tab Files and check there the option Put each file to separate archive.
Click on button OK.
WinRAR creates now three RAR archives with the file names temp1.rar, temp2.rar and temp3.rar in folder c:\rar\ with each archive containing the appropriate folder with all files and subfolders.
The list of files to add can be changed also on tab Files by entering for example *.txt in Files to exclude to ignore text files in the three folders on creating the archives.
And finally it makes sense to enter *.jpg on tab Files in edit field below Files to store without compression as JPEG files usually contain already compressed data and therefore WinRAR cannot really compress the data of the files further.
Here is also a batch file solution to move the files in all non-hidden subfolders of c:\rar\ and their subfolders into an archive file with name of the subfolder created in each subfolder as requested.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RAREXE=Rar.exe"
if exist "%RAREXE%" goto CreateArchives
if exist "%ProgramFiles%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles%\WinRAR\Rar.exe" & goto CreateArchives
if exist "%ProgramFiles(x86)%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles(x86)%\WinRAR\Rar.exe" & goto CreateArchives
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe Rar.exe 2^>nul') do set "RAREXE=%%I" & goto CreateArchives
echo ERROR: Could not find Rar.exe!
echo/
echo Please define the variable RAREXE at top of the batch file
echo "%~f0"
echo with the full qualified file name of the executable Rar.exe.
echo/
pause
goto :EOF
:CreateArchives
set "Error="
for /D %%I in ("c:\rar\*") do (
echo Creating RAR archive for "%%I" ...
"%RAREXE%" m -# -cfg- -ep1 -idq -m3 -msgif;png;jpg;rar;zip -r -s- -tl -y -- "%%I\%%~nxI.rar" "%%I\"
if errorlevel 1 set "Error=1"
)
if defined Error echo/& pause
endlocal
The lines after set "RAREXE=Rar.exe" up to :CreateArchives can be omitted on definition of environment variable RAREXE with correct full qualified file name.
Please read the text file Rar.txt in the WinRAR program files folder for an explanation of RAR command m and the used switches. The question does not contain any information with which options the RAR archives should be created at all.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~f0 ... full name of batch file
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
set /?
setlocal /?
where /?
See also single line with multiple commands using Windows batch file for an explanation of the operator &.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on the three FOR command lines to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg or where command line with using a separate command process started in background.
This script can work as well:
#echo off
for %%a in ("C:\rar\temp1" "C:\rar\temp2" "C:\rar\temp3") do (
pushd "%%~a"
"C:\Program Files\WinRAR\rar.exe" a -r temp.rar *
popd
)
In Python v3.x:
Tested on Python v3.7
Tested on Windows 10 x64
import os
# NOTE: Script is disabled by default, uncomment final line to run for real.
base_dir = "E:\target_dir"
# base_dir = os.getcwd() # Uncomment this to run on the directory the script is in.
# Stage 1: Get list of directories to compress. Top level only.
sub_dirs_raw = [os.path.join(base_dir, o) for o in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, o))]
# Stage 2: Allow us exclude directories we do not want (can omit this entire section if we wish).
dirs = []
for d in sub_dirs_raw:
if "legacy" in d or "legacy_old" in d:
continue # Skip unwanted directories
print(d)
dirs.append(d)
# Stage 3: Compress directories into .rar files.
for d in dirs:
os.chdir(d) # Change to target directory.
# Also adds 3% recovery record using "-rr3" switch.
cmd = f"\"C:\Program Files\\WinRAR\\rar.exe\" a -rr3 -r {d}.rar *"
print(cmd)
# Script is disabled by default, uncomment this next line to execute the command.
# os.system(cmd)
Notes:
Script will do nothing but print commands, unless the final line os.system(cmd) is uncommented by removing the leading # .
Run the script, it will print out the DOS commands that it will execute. When you are happy with the results, uncomment final line to run it for real.
Example: if there was a directory containing three folders mydir1, mydir2, mydir3, it would create three .rar files: mydir1.rar, mydir2.rar, mydir3.rar.
This demo code will skip directories with "legacy" and "legacy_old" in the name. You can update to add your own directories to skip.
To execute the script, install Python 3.x, paste the lines above into script.py, then run the DOS command python script.py from any directory. Set the target directory using the second line. Alternatively, run the script using PyCharm.
This should work it also checks if the files were compressed alright.
You may need to change this part "cd Program Files\WinRAR" depending on where winrar is installed.
#echo Off
Cd\
cd Program Files\WinRAR
rar a -r c:\rar\temp1\temp1.rar c:\rar\temp1\*.jpg c:\rar\temp1\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp2\temp2.rar c:\rar\temp2\*.jpg c:\rar\temp2\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp3\temp3.rar c:\rar\temp3\*.jpg c:\rar\temp3\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
Pause
Below Script will compress each folder as a RAR file within the current directory with very useful info while compressing a large size of data.
#echo off
#for /D %%I in (".\*") do echo started at %date% %time% compressing... "%%I" && #"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"
echo "Completed Successfully !!!"
pause

Resources