简体   繁体   中英

Batch File to Compress Certain Directories with 7zip?

Just a request for help with batch programming. Need to do periodic backups of certain folders and was wondering how I would achieve that.

Goal: To Scroll through a directory and compress certain folders with a certain name.

Code Logic:

  //For all directories in folder
  for /d %%X in (*) do (
    if ( %%X != tag_****** )
        "7z.exe" a -ttar "%%X.tar" "%%X\"
  )

.

  for %%X in (*.tar) do 
    "7z.exe" a -tgzip "%%X.tgz" "%%X"

How would I differentiate between folder names in batch if it is possible?

ex: compress tag_aaa111 , skip tag_aaa111_v2 , skip all folders without prefix "tag_"

condition is that the directory starts with tag_ and has exactly 6 chars after that.

Been stuck trying to figure out this. Thanks for the help.

edit: code, clarification.

Do you care about the language? If you wanted to use PERL you could do something like:

while(<*>){
  if(-d && m/^tag_/){
    system("7zip $_"); # Change to whatever compress command you need
  } else {
    #other condition for other files and non matching directories
  }
}

Where the system will make a system call with whatever parameters are inside of it. The -d will specify whether the file is a directory, and the m// is a regex that matches against the specified pattern

Have a look at Windows' FOR and IF batch file commands. Basically, it should be possible to write something like this:

for /D %%f in (*) do (
    set dirname=%%f
    if "%dirname:~0,4%"=="tag_" (
        7z ... %dirname% ...
    )
)

untested :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM