BASF
BASF

Reputation: 147

Batch script exclude foldername for winrar

I want that the script skip all sub folders in a main folder that contains _Fun_ in the folder name to avoid that winrar archive it.

I tried something but nothing has worked, the last attempt by me looks like:

for /D %%F in ("C:\Users\Admin\Desktop\Pictures\*") do (
"%ProgramFiles%\WinRAR\Rar.exe" m -ep1 -mt5 -m1 -v103M -r -x@"_Fun_" "C:\Users\Admin\Desktop\ArchivePictures\-Archive-Name-" "%%~F"
goto Done
)

Upvotes: 0

Views: 435

Answers (1)

Mofi
Mofi

Reputation: 49096

Use this batch code to test if name of current folder contains _Fun_ and skip that folder.

setlocal EnableDelayedExpansion
for /D %%F in ("C:\Users\Admin\Desktop\Pictures\*") do (
    set "FolderName=%%~nxF"
    if "!FolderName:_Fun_=!" == "%%~nxF" (
        "%ProgramFiles%\WinRAR\Rar.exe" m -ep1 -mt5 -m1 -v103M -r "C:\Users\Admin\Desktop\ArchivePictures\-Archive-Name-" "%%~F"
        goto Done
    )
)
:Done
endlocal

The name of the folder and the extension (in case of folder contains a dot somewhere) without path is assigned to an environment variable FolderName.

In the next line all occurrences of _Fun_ in environment variable FolderName expanded delayed and not already when cmd.exe parses entire FOR block are replaced by nothing and this string is compared with unmodified folder name.

The string comparison is positive if folder name does not contain _Fun_ anywhere. In this case the folder is compressed with WinRAR.

Read more about string manipulations and delayed environment variable expansion by opening a command prompt window, entering set /? or help set, executing with key RETURN and reading the help pages output in console window.

The special character sequences as used here for %%~nxF are explained in help of command call as well as command for /? which can be read by executing call /? or for /? or help call or help for.

Upvotes: 1

Related Questions