Batch script question

Discussion in 'Programming/Html' started by Pandora13, Apr 25, 2016.

  1. Pandora13

    Pandora13 New Member

    Messages:
    5
    Likes Received:
    0
    GPU:
    Intel HD Graphics
    Hi everyone!
    First let me apologize if this is the wrong forum for this post... This seemed like the closest fit.

    I need help writing a batch script. I want to de-compress a bunch of files using WinRar that sit in a folder and whole bunch of sub-folders.

    For the life of me I can't get it to work - spent a lot of time digging through forums and sites... It just doesn't run.

    Ideally I would like it to start in the root folder, then run through every sub-folder in sequence, uncompressing what it finds into individual folders in those sub folders, then deleting the original compressed files only after success.

    Did that make sense?

    I'd really appreciate any help on this. Thanks.
    :banana:
     
  2. vase

    vase Ancient Guru

    Messages:
    1,653
    Likes Received:
    0
    GPU:
    -
    Code:
    $Directory = C:\*"
    
    $zipfiles = Get-ChildItem -path $Directory -Recurse -Include *.rar
    
    foreach($line in $zipfiles){
        $destdir = $line.Directory
        $sourcedir = $line.VersionInfo.FileName
        C:\"Program Files (x86)"\WinRAR\unrar.exe e -ro- $sourcedir $destdir
    }
    thats in powershell. so you basically start this script out of a batch which starts powershell.exe
     
  3. mbk1969

    mbk1969 Ancient Guru

    Messages:
    3,571
    Likes Received:
    13
    GPU:
    GeForce GTX 970
    vase, that script will search for rar-files to the bottom of folder tree. As I take the task, script should look for rar-files only in subfolders of root folder.

    Edit: Also recursive Get-ChildItem can throw error traversing system protected folders.

    Edit: And your script doesn`t delete rar-files after decompression.

    Edit:
    $Directory = "C:\*"

    Code:
    $Directory = "C:\*"
    
    $zipfiles = Get-ChildItem -path $Directory -Recurse -Include *.rar
    
    foreach($line in $zipfiles){
        $destdir = $line.DirectoryName
        $sourcedir = $line.FullName
        C:\"Program Files (x86)"\WinRAR\unrar.exe e -ro- $sourcedir $destdir
        if($LastExitCode -eq 0) {
            $line.Delete()
        }
    }
    I mean Get-ChildItem returns System.IO.FileInfo type (in that script) and its property "Directory" is of type System.IO.DirectoryInfo, and property "VersionInfo" is a script type of property added to object by PS itself. So properties "DirectoryName" and "FullName" are quicker way to get name of parent directory and full path of file.

    Edit: Don`t forget to permit PowerShell script execution with help of Set-ExecutionPolicy

    Edit: Pandora13, just copy that text into file with "ps1" extension and you can run it either in PowerShell or in PowerShell ISE.
     
    Last edited: Apr 26, 2016
  4. Pandora13

    Pandora13 New Member

    Messages:
    5
    Likes Received:
    0
    GPU:
    Intel HD Graphics
    Thanks but I must be brain dead...

    Thanks everyone for helping with this.

    But I must be brain dead here. I'll admit I am no programmer - I do fix computers, and such for a living... But programming not really my thing.

    That being said - I tried the script posted and I KNOW I am doing something wrong here. Feel free to laugh at my ineptitude -- but I need very specific instructions here.

    It just not working.

    FYI, the root folder is the F: drive - all the folders/sub-folders in the F: drive the script should run on.

    I enabled powershell (I think) using the command and adjusting the parameters as suggested...

    But the script doesn't run. I changed the extension to PS1 but Powershell won't run it. Windows won't run it either - it just blips on the screen but nothing happens.

    What did I do wrong?

    Thanks
     
    Last edited: Apr 27, 2016

  5. Pandora13

    Pandora13 New Member

    Messages:
    5
    Likes Received:
    0
    GPU:
    Intel HD Graphics
    Ok, with much fiddling about I got PowerShell enabled to run scripts... and ran the script. Still got errors. Here's what it said:

    FYI - to me it just looked like the path for unrar was wrong, i checked and on my computer it resides in: C:\Program Files\WinRAR\unrar.exe - i did change it and tried running the script again (yes I did save it). still gave the same error.

    PS C:\Users\DELL> F:\runme.ps1
    The term 'C:\Program Files (x86)\WinRAR\unrar.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, o
    r if a path was included, verify that the path is correct and try again.
    At F:\runme.ps1:8 char:46
    + C:\"Program Files (x86)"\WinRAR\unrar.exe <<<< e -ro- $sourcedir $destdir
    + CategoryInfo : ObjectNotFound: (C:\Program Files (x86)\WinRAR\unrar.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    -------------------------------------------------------------

    here's the error when i changed the filepath:

    The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, ver
    ify that the path is correct and try again.
    At F:\runme.ps1:8 char:15
    + C:\Program <<<< Files\WinRAR\unrar.exe e -ro- $sourcedir $destdir
    + CategoryInfo : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    ------------------------------------------------------------------------

    do i need to put an _ in there between program and files?

    not sure why it comes up garbled like that in the script, i double checked and it's typed correctly when i edit it with notepad

    thanks again
     
  6. Pandora13

    Pandora13 New Member

    Messages:
    5
    Likes Received:
    0
    GPU:
    Intel HD Graphics
    Ok. Ignore my two other posts. The mistake I made when changing the file path for winrar was forgetting to put " around it. It runs THANK YOU

    I really appreciate the help everyone! Been trying to get this to work for a while now - this saves me A LOT of time and effort!!!!!!!!!!!!!!
     
  7. mbk1969

    mbk1969 Ancient Guru

    Messages:
    3,571
    Likes Received:
    13
    GPU:
    GeForce GTX 970
    You are welcome. You did well troubleshooting the script (and pretty fast).

    I did know MS DOS batch files thoroughly but that was long ago. New shell - PowerShell - is much more powerful, flexible and clean. And big fat plus for me, it is based on .Net framework which I know pretty much well being a professional programmer. You can ask for new scripts any time.
     
  8. Pandora13

    Pandora13 New Member

    Messages:
    5
    Likes Received:
    0
    GPU:
    Intel HD Graphics
    MBK1969 (and others) again a big thanks! The script is super helpful.

    Ya, on my to do list now is learn powershell - things keep changing in IT faster than I can stay on top of it. Which is a good thing - love new technologies but I will admit I get frustrated in forever having to relearn stuff! lol

    I had a leg up in troubleshooting the script as I am motivated to get it running - been trying to write something like this for the past couple of months lol. I have a whole bunch of files I really don't want to go through manually. This totally fixes that.

    Thanks again!

    MBK1969 - curious, what kind of stuff do you program? Everything? Or do you have a specialty?
     
  9. mbk1969

    mbk1969 Ancient Guru

    Messages:
    3,571
    Likes Received:
    13
    GPU:
    GeForce GTX 970
    Do you want a book(s) on PowerShell? I bought a bunch at www.oreilly.com and they permit free copying after...

    On previous work (www.paragon-software.com) I participated in tools for backup, restore, partitioning and other storage oriented stuff for 10 years. Before that I worked with protection against unauthorized access. Now I work in field of electronic document management. In other words - desktop applications in Windows.
     
    Last edited: Apr 28, 2016
  10. RPGgamesplayer

    RPGgamesplayer Maha Guru

    Messages:
    1,453
    Likes Received:
    0
    GPU:
    1080 Strix OC
    Hello Guru's,

    Just after some help, does anyone have a batch script handy to add a font pack
    I want to do this via Group Policy

    Thanks in advance :D
     

Share This Page