Guru3D.com Forums

Go Back   Guru3D.com Forums > General > Programming/Html
Programming/Html C++,Visual Basic,HTML,Flash,OpenGL,Modelling,Java.



Reply
 
Thread Tools Display Modes
Batch script question
Old
  (#1)
Pandora13
Newbie
 
Videocard: Intel HD Graphics
Processor: Intel Pent N3540 2.16ghz
Mainboard:
Memory:
Soundcard:
PSU: 500 psu
Smile Batch script question - 04-25-2016, 00:24 | posts: 5

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.
   
Reply With Quote
 
Old
  (#2)
vase
Maha Guru
 
vase's Avatar
 
Videocard: -
Processor: -
Mainboard:
Memory:
Soundcard:
PSU: -
Default 04-25-2016, 01:45 | posts: 1,653 | Location: #1 http://www.3dmark.com/fs/8257155

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
   
Reply With Quote
Old
  (#3)
mbk1969
Ancient Guru
 
mbk1969's Avatar
 
Videocard: GeForce GTX 970
Processor: I7-4930
Mainboard: Asus p9x79
Memory: G Skill RipjawZ, 16GB
Soundcard: Onboard + FiiO E17
PSU: 1000 W
Default 04-25-2016, 13:11 | posts: 3,507 | Location: Moscow, Russia

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 by mbk1969; 04-26-2016 at 10:45.
   
Reply With Quote
Thanks but I must be brain dead...
Old
  (#4)
Pandora13
Newbie
 
Videocard: Intel HD Graphics
Processor: Intel Pent N3540 2.16ghz
Mainboard:
Memory:
Soundcard:
PSU: 500 psu
Default Thanks but I must be brain dead... - 04-27-2016, 16:41 | posts: 5

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 by Pandora13; 04-27-2016 at 16:45. Reason: additional info
   
Reply With Quote
 
Old
  (#5)
Pandora13
Newbie
 
Videocard: Intel HD Graphics
Processor: Intel Pent N3540 2.16ghz
Mainboard:
Memory:
Soundcard:
PSU: 500 psu
Default 04-27-2016, 17:10 | posts: 5

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
   
Reply With Quote
Old
  (#6)
Pandora13
Newbie
 
Videocard: Intel HD Graphics
Processor: Intel Pent N3540 2.16ghz
Mainboard:
Memory:
Soundcard:
PSU: 500 psu
Default 04-27-2016, 17:23 | posts: 5

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!!!!!!!!!!!!!!
   
Reply With Quote
Old
  (#7)
mbk1969
Ancient Guru
 
mbk1969's Avatar
 
Videocard: GeForce GTX 970
Processor: I7-4930
Mainboard: Asus p9x79
Memory: G Skill RipjawZ, 16GB
Soundcard: Onboard + FiiO E17
PSU: 1000 W
Default 04-27-2016, 21:56 | posts: 3,507 | Location: Moscow, Russia

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.
   
Reply With Quote
Old
  (#8)
Pandora13
Newbie
 
Videocard: Intel HD Graphics
Processor: Intel Pent N3540 2.16ghz
Mainboard:
Memory:
Soundcard:
PSU: 500 psu
Default 04-28-2016, 17:38 | posts: 5

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?
   
Reply With Quote
Old
  (#9)
mbk1969
Ancient Guru
 
mbk1969's Avatar
 
Videocard: GeForce GTX 970
Processor: I7-4930
Mainboard: Asus p9x79
Memory: G Skill RipjawZ, 16GB
Soundcard: Onboard + FiiO E17
PSU: 1000 W
Default 04-28-2016, 19:46 | posts: 3,507 | Location: Moscow, Russia

Quote:
Originally Posted by Pandora13 View Post
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?
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 by mbk1969; 04-28-2016 at 19:48.
   
Reply With Quote
Old
  (#10)
RPGgamesplayer
Maha Guru
 
RPGgamesplayer's Avatar
 
Videocard: 1080 Strix OC
Processor: I7 6700k@4.6
Mainboard: Maximus VIII Hero Alpha
Memory: Corsair DDR4 3866mhz 16gb
Soundcard: X-Fi Titanium
PSU: Superflower 1000w Plat
Default 10-19-2016, 11:58 | posts: 1,452 | Location: London

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
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
Copyright (c) 2017, All Rights Reserved. The Guru of 3D, the Hardware Guru, and 3D Guru are trademarks owned by Hilbert Hagedoorn.