0

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.

Aaron Nguyen
  • 195
  • 1
  • 3
  • 14

2 Answers2

2

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

jcern
  • 7,798
  • 4
  • 39
  • 47
  • would I be able to have windows scheduler automatically run perl scripts to automate the script every night? not very familiar with perl – Aaron Nguyen Aug 15 '12 at 21:32
  • As long as you have perl installed. You should be able to schedule any type of script or executable to run. [scheduling a perl script](http://serverfault.com/questions/267013/scheduling-a-perl-script-with-output-redirection) has an explanation – jcern Aug 15 '12 at 21:37
  • interesting. do you know if perl has a cli executable i can download? im trying to avoid installing anything. – Aaron Nguyen Aug 15 '12 at 21:44
  • It comes pre-installed on many unix flavors, but in windows you'd probably need to install the interpretor as it has a bunch of libraries and such. If installing software on the target machine is a problem (but you could do the development on a machine where installation isn't), you can try something like this [compiling perl to exe](http://stackoverflow.com/questions/2948405/how-to-compile-a-perl-script-pl-to-a-windows-executable-exe-with-strawberr) to make an executable. – jcern Aug 15 '12 at 21:48
  • yeah I dont have admin rights to the host machines but i'll definitely take a look into it if the batch doesn't work. Thanks for the help! – Aaron Nguyen Aug 15 '12 at 22:01
1

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 :)

Michael
  • 8,920
  • 3
  • 38
  • 56
  • oh perfect. so this code block would replace my code block one. and i assume that the if statement "7z ... %dirname% ..." would be this line: "7z.exe a -ttar %dirname%.tar %dirname%" ? and how does the dirname sort directories out with exactly 10 chars in the name? – Aaron Nguyen Aug 15 '12 at 21:38