1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

NEED HELP writing a batch file for the following task:

Discussion in 'Windows - Software discussion' started by aweathe, May 9, 2007.

  1. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    This is a debugging version with lots of messages and progress updates...

    [pre]
    @echo off
    REM place this batch file in the top folder
    REM and run it from there

    REM remember where we are
    set topfolder=%CD%

    if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
    if exist "%topfolder%\rardirlist.txt" del "%topfolder%\rardirlist.txt" > nul
    echo.
    echo (1) creating list of all sub folders...
    echo.

    REM get a list of all the subfolders
    REM under this one and put it in a temp file
    dir /b /s /ad > "%topfolder%\subdirlist.txt"

    echo all sub folders...
    echo.
    type "%topfolder%\subdirlist.txt"
    echo.
    pause
    echo.
    echo (2) finding folders with rars in them
    echo.

    REM in a loop check each one for rar files
    REM write folder name to another list if any found
    for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
    cd %%a
    if exist *.rar echo %%a >> "%topfolder%\rardirlist.txt"
    )

    echo all sub folders with Rars in...
    echo.
    type "%topfolder%\rardirlist.txt"
    echo.
    pause
    echo.
    echo (3) extracting from Rar archives
    echo.
    echo ready to start...
    echo.
    pause
    echo.
    REM for each folder that contains RARs
    REM enter folder and extract
    for /F "delims==" %%a in ('type "%topfolder%\rardirlist.txt"') do (
    echo.
    echo expanding rars in %%a
    echo.

    cd %%a
    "c:\program files\winrar\rar.exe" e *.rar
    )

    REM clean up
    rem del "%topfolder%\subdirlist.txt" > nul
    rem del "%topfolder%\rardirlist.txt" > nul

    REM go back to where we were
    cd %topfolder%
    echo.
    echo (4) Finished
    echo.
    pause
    echo.

    [/pre]
     
    Last edited: May 10, 2007
  2. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    I'm working through it. Almost works I think... going to have to take a break for lunch tho! Thanks so much for the help. I'll write you again when I solve it or if I get stuck.

    Just a couple (simple) questions so that I can understand the code and make it work:
    1. "for /F" means for all folders in the current directory?
    2. "dir /b /s /ad" : in this statement I believe "/s" means include all subfolders, however I don't understand "/b" and "/ad".

    Finally, the part of the code that has the problem is copied here:
    ""
    REM in a loop check each one for rar files
    REM write folder name to another list if any found
    for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
    cd %%a

    if exist *.rar echo %%a > "%topfolder%\rardirlist.txt"
    )
    ""
    The problem is that the folder "rardirlist.txt" doesn't get created...?

    talk to you again soon...
     
  3. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    Putting /F after the FOR indicates to FOR that whatever is between the parentheses is the source of a series of lines of data to be read and processed. Single quotes around what is in the parentheses indicate that the source is the output of a command.

    In this line,

    for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (

    FOR is told to TYPE the file and deal with it line by line.

    * without the "/f", what is between the parentheses is interpreted as a series of ITEMS.

    for "delims==" %%f in (apple orange banana) do (
    echo %%f
    )

    the result would be

    apple
    orange
    banana

    * single-quotes within the parentheses indicates that the data lines are sourced from the output of a command. Without the single-quotes, FOR /F tries to read a (text-)file named (whatever is within the parentheses)

    2. "dir /b /s /ad" : in this statement I believe "/s" means include all subfolders, however I don't understand "/b" and "/ad".

    Dir /? will give full info.

    Dir /b gives a bare output, just the file names, no date or size info or header or footer. Necessary for processing in batch files.

    Dir /ad --- /(a)ttribute (d)irectory just finds directories.
    Dir /ah --- /(a)ttribute (h)idden finds just hidden files
    Dir /ar --- /(a)ttribute (r)ead only finds just read only files
    Dir /as --- /(a)ttribute (s)ystem finds just system files.

    A minus sign eg dir /a-d inverts the effect.

    Does subdirlist.txt get created?

    Until our next chat...
     
  4. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    New greatly simplified version - no temp files, it just goes ahead and does it.

    [original post 14:28 hrs updated 20:20 hrs]

    [pre]@echo off
    REM place this batch file in the top folder and run it from there
    set topfolder=%CD%
    for /F "delims==" %%a in ('dir /b /s *.rar') do (
    cd "%%~Da%%~pa"
    "c:\program files\winrar\rar.exe" e "%%~Da%%~pa%%~nxa" | findstr "Extracting from"
    )
    cd %topfolder%[/pre]
     
    Last edited: May 10, 2007
  5. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    RE:
    yes it does. and it looks fine.
     
  6. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    the shorter program you wrote takes some time to complete in the command window (5-10 secs)but nothing seems to be changed in the folders. I don't understand that one though! ;o)
     
  7. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    have you tried the latest revised shorter one? (I changed it)

    does it give any messages?

    What you see if you go in the top folder and type dir /b /s
     
    Last edited: May 10, 2007
  8. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    There is something bugging me... the batch files I wrote are working perfectly on my machine, where I have a test setup of folders with rar files exactly as we discussed. In each subfolder is a set of subfolders, some with rars, some without.

    [​IMG]

    There's something I'm missing, something that is different between our two systems, and I need to find out what it is.

    Are you running Windows XP?

    What happens when you go into the top folder, and type the following...

    dir /s /b *.rar

    Does it find any?

    More relevant, does this find any?

    [pre]@echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    set topfolder=%CD%
    set unrarapp="C:\Program files\winrar\rar.exe"
    for /F "delims==" %%a in ('dir /b /s *.rar') do (
    CD "%%~pa" & %unrarapp% e "%%~Dpnxa"
    )
    cd %topfolder%[/pre]
     
    Last edited: May 11, 2007
  9. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    updated version, see above
     
  10. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    I have found one thing that is causing a problem. The files are not (!) winRAR files. (yes i know you're laughing at how I could have not realized this earlier).

    They are labeled as winRAR files by my computer because I have winRAR. But their extensions are not ".rar"

    The extensions are instead ".Z"

    An example of one of the many filenames is "18685643.msg.Z"

    Now, I have changed you're code (I'm working with the longer version becasue I can understand it) so that the statments including "*.rar" get changed to included "*.Z" However, this doesn't solve the problem.

    I am able to decompress these files manually (one at a time) by right clicking them and chosing decompress.

    But for some reason I cannot get them to decompress from the comand prompt. I have tried to do this with sigle files using simple winRAR commands in the command propmt.

    I get the same error message for both your code (with the "*.Z") and for the single file tests that I've been doing in the command prompt.

    The error message is (given in the cmd prompt):
    ******************************************************************
    RAR 3.62 Copyright (c) 1993-2006 Alexander Roshal 3 Dec 2006
    Shareware version Type RAR -? for help

    18685818.msg.Z is not RAR archive
    No files to extract

    (4) Finished

    Press any key to continue . . .
    ************************************************************

    I've been trying to figure this out...

    It also doesn't help if I simply rename the file to ".RAR" (ie 18685818.rar). The same error message is given.
     
  11. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    My responses:
    1. The program "SETLOCAL ENABLEDELAYEDEXPANSION" returns "File not found"

    2. Yes I'm running windows XP

    3. Your file structure diagram is the same as how it is on my computer.
     
  12. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    OK. I was getting worried that my code had run amok and overwritten your hard drive!

    It wouls appear that archive files with the extension .z are files compressed with the Unix "Compress" program. You can get an equivalent DOS/Windows program called gzip.exe here...

    ftp://sis.agr.gc.ca/PUB/cansis/gzip.exe

    Place it in a folder & be aware of its path & location

    eg C:\Program files\Gzip\gzip.exe

    the syntax to decompress is gzip -d (filename)

    After decompression the .z extension is gone, and the archive is REPLACED by its decompressed counterpart.

    Thus...

    gzip -d "18685643.msg.Z" would result in the file ""18685643.msg"

    I will cook up a batch file shortly...








     
  13. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    OK I've saved that file to the dir you suggested.

    Could you tell me what "delims==" means? The loop doesn't seem to be working in the long version... and this is one part I don't understand.

    I'm changing the long version to run with the gzip command instead -- attempting to ;o)
     
  14. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (

    "delims==" tells FOR to treat each line produced by 'type "%topfolder%\subdirlist.txt"' as a complete line, ie not to split it into separate words.
     
  15. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    ok thanks!
     
  16. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    The long versions of the file depended on the ability of the RAR.exe program to take a wildcard (*.rar) instead of a specific filename.

    This technique is not possible with gzip.

    here is a batch program I have just made that seems to work OK with .z files. I suggest you make a copy of your data and run it on that rather than the important stuff. I have added some comments to explain aspects of the code.

    [pre]
    @echo off
    REM I confess I am not sure if this
    REM is needed but it won't hurt
    SETLOCAL ENABLEDELAYEDEXPANSION
    set topfolder=%CD%
    REM for each .z file in any subfolder
    for /F "delims==" %%a in ('dir /b /s *.z') do (
    REM in respect of each file
    REM represented by %%a,
    REM %%~pa is its path (folder)

    REM change to its folder
    CD "%%~pa"

    REM Extract filename parameters
    REM %%~Da is its drive letter and colon
    REM %%~pa is its path (folder)
    REM %%~na is its filename
    REM %%~xa is its .extension
    REM can be combined thus...

    REM inform user of file being processed
    echo processing %%~Dpnxa

    REM call gzip with -d option (decompress)
    "C:\Program Files\gzip\gzip.exe" -d "%%~Dpnxa"
    )
    cd %topfolder%
    [/pre]
     
    Last edited: May 14, 2007
  17. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    Ok I'm going to try the new code you just put up.
    I didn't try it until now because I was trying to make the longer version work... and I think I have. I think I understand the problem you were stating about the longer version when switching to ".z" files, but I didn't run into that problem.

    Here is a copy of your original long version with my modifications. I think it works.. but would like your comments!

    @echo off
    REM place this batch file in the top folder
    REM and run it from there


    REM remember where we are
    set topfolder=%CD%


    if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
    if exist "%topfolder%\gzipdirlist.txt" del "%topfolder%\gzipdirlist.txt" > nul
    echo.
    echo (1) creating list of all sub folders...
    echo.

    pause

    REM get a list of all the subfolders
    REM under this one and put it in a temp file
    dir /b /s /ad > "%topfolder%\subdirlist.txt"


    echo all sub folders...
    echo.
    type "%topfolder%\subdirlist.txt"
    echo.

    echo.
    echo (2) finding folders with gzips in them
    echo.


    REM in a loop check each one for gzip files
    REM write folder name to another list if any found
    for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
    cd %%a

    if exist *.Z echo %%a >> "%topfolder%\gzipdirlist.txt"
    )

    pause

    echo all sub folders with gzips in...
    echo.
    type "%topfolder%\gzipdirlist.txt"
    echo.
    pause
    echo.
    echo (3) extracting from gzip archives
    echo.
    echo ready to start...
    echo.
    pause
    echo.
    REM for each folder that contains gzips
    REM enter folder and extract
    for /F "delims==" %%a in ('type "%topfolder%\gzipdirlist.txt"') do (
    echo.
    echo expanding gzips in %%a
    echo.

    cd %%a

    copy *.Z *.Y


    "c:\program files\gzip\gzip.exe" -d *.Z


    ren *.Y *.Z


    )

    REM clean up
    rem del "%topfolder%\subdirlist.txt" > nul
    rem del "%topfolder%\gzipdirlist.txt" > nul

    REM go back to where we were
    cd %topfolder%
    echo.
    echo (4) Finished
    echo.
    pause
    echo.
     
  18. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    It has been alot to take in for me and so I wanted to try to understand one thing at a time... so that's why I was working with the original... Man do I have a headache! ahh the joy of learning new skills!
     
  19. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    I'm leaving for home now... but I just tried your latest version. It didn't work all the way (I'm too tired to figure out why...). The error message said "No such file or directory", for each iteration. (ie about 30 times or so, with the path directory in between)

    Talk to you tomorrow.

     
  20. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    good thinking.

    Won't work, as i said before. Wildcard ("*.Z") won't work.

    need to make further loop

    see code in next post
     
    Last edited: May 15, 2007

Share This Page