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
    Further to my recent posts...

    (1)

    You need a later version of Gzip specifically for Win32

    You can get the Gzip 1.3.11 installer here

    http://dfn.dl.sourceforge.net/sourceforge/gnuwin32/gzip-1.3.11.exe

    You need to run it and select an installation directory.

    By default it will create a subfolder structure under that directory
    and the gzip.exe will be in a subdirectory called bin.

    You can leave it there & use the full path in your batch files or move or copy it
    where you want...

    I put mine into c:\Program Files\Gzip to make things easier to remember.

    (2)

    The copy *.Z *.Y thing does not work. Wildcards are OK for REN but not for COPY.
    Solution in this batch file, which seems to do the required things...

    You can delete or comment out (by preceding with REM) the pause statements if
    you want to run it unattended. They are there to provide a chance to hit CTRL + C and abort the batch during debugging really.

    (3) This gzip supports wildcards which makes things simpler

    (4) new code...

    @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 (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 (
    cd %%a
    for /F "delims==" %%b in ('dir /b *.z') do (
    echo copying "%%b" to "%%~nb.y"
    copy "%%b" "%%~nb.y"
    )
    echo.
    echo expanding gzips in %%a
    echo.
    "c:\program files\gzip\gzip.exe" -d -v *.z
    ren *.Y *.Z
    )
    REM clean up
    del "%topfolder%\subdirlist.txt" > nul
    del "%topfolder%\gzipdirlist.txt" > nul
    REM go back to where we were
    cd %topfolder%
    echo.
    echo (4) Finished
    echo.
    pause
    echo.
     
    Last edited: May 15, 2007
  2. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    short version (no pauses, less messages, just does it)

    @echo off
    echo Started...
    set topfolder=%CD%
    if exist "%topfolder%\subdirlist.txt" del "%topfolder%\subdirlist.txt" > nul
    if exist "%topfolder%\gzipdirlist.txt" del "%topfolder%\gzipdirlist.txt" > nul
    dir /b /s /ad > "%topfolder%\subdirlist.txt"
    for /F "delims==" %%a in ('type "%topfolder%\subdirlist.txt"') do (
    cd %%a & if exist *.Z echo %%a >> "%topfolder%\gzipdirlist.txt"
    )
    For /F "delims==" %%a in ('type "%topfolder%\gzipdirlist.txt"') do (
    cd %%a & copy *.Z *.Y
    "c:\program files\gzip\gzip.exe" -d -q *.z & ren *.Y *.Z
    )
    del "%topfolder%\subdirlist.txt" > nul & del "%topfolder%\gzipdirlist.txt" > nul
    cd %topfolder%
    echo Finished
     
    Last edited: May 15, 2007
  3. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    OK here's what's going on:

    1) I've ran both of the two latest scripts you've posted (the long version and the short version) and they both work.

    2) The last script that I posted (at 14. May 2007 @ 16:18) (modified from you're original long version) also seems to work. Ie: after copying the folder (and subfolders) so that I have 3 new folders to test these codes on, when I run each of these codes they all seem to work. And thus produce the same result. Can you tell me where I can look for difference/problems? Or what is the risk to running the code that I modified? I didn't understand that yet. The *.z seems to have worked for the copy command...?

    3) I have run each of these programs using the same gzip.exe file: the original link you sent me. It seems to be working... should I still download the newer version?


    PS when the program worked for the first time... man then it was all worth it! : huge time saver for sure.
     
  4. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    The only line I don't understand about you're latest script is:

    copy "%%b" "%%~nb.y"

    Can you tell me what "%%~nb.y" mean in this line?
     
  5. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    The only line I don't understand about you're latest script is:

    copy "%%b" "%%~nb.y"

    Can you tell me what "%%~nb.y" means in this line?
     
  6. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    I should stick with your modified version of my long code. It has the advantage of being familiar to you; also the alterations which made it work are yours, and you are entitled to them!

    I would like to say "very well done" in this regard!

    I wrote the long version like that, heavily commented, with frequent reassuring messages and PAUSE commands because I wanted to be sure at each step that it was working OK, was in the right folder, was doing the right things, etc. The shorter version is essentialy the same code but with all the hand holding and comments removed, for the sake of compactness.

    For example, in Windows NT prompt (Win2K, XP, Vista) you can combine short lines into one line if they are always executed one after the other -

    @echo off
    CLS
    echo hello
    DIR

    could be replaced by

    @echo off & DIR & echo hello & DIR

    I tend to write out my batch files in a long form and spend time getting them right, and if I am going to use one often, I might well "tidy it up" in this way.

    It does work; I was mistaken thinking it didn't. I have revised my short version above to take account of this.

    I found the earlier version worked but showed strange characters on the screen when run in command mode. It dates from 1993, and the later version 1.3.11 which was compiled for Win32 (Win95 onwards) doesn't do this.

    Obviously it is a matter of preference.

    It's a great feeling.



     
  7. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    I cooked that up because I mistakenly thought that you couldn't do a

    copy *.z *.y

    operation.

    with FOR in NT family Windows, you can extract information from variables.

    If %%b is a file, (in this case a .z file)

    %%~nb is the filename without any extension (ie with ".z" missing)

    so if %%b expands to apple-pie.z
    then %%~nb expands to apple-pie
    and %%~nb.y expands to apple-pie.y

    Type FOR /? at the prompt to get full info...

    here I represents any variable letter (I used letter b)

    (In batch files you prefix an extra % sign)

    %~I - expands %I removing any surrounding quotes (")
    %~fI - expands %I to a fully qualified path name
    %~dI - expands %I to a drive letter only (and colon character) eg D:
    %~pI - expands %I to a path only
    %~nI - expands %I to a file name only
    %~xI - expands %I to a file extension only
    %~sI - expanded path contains short names only
    %~aI - expands %I to file attributes of file
    %~tI - expands %I to date/time of file
    %~zI - expands %I to size of file

    The modifiers can be combined to get compound results:

    %~dpI - expands %I to a drive letter and path only
    %~nxI - expands %I to a file name and extension only
    %~fsI - expands %I to a full path name with short names only
     
    Last edited: May 15, 2007
  8. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    great thanks. that makes it clear.
     
  9. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    I'm starting on my second task. But thanks to your help I think I can do it almost all on my own.

    I have run into a small problem:

    If I want to check match up files from the following two lists:

    I17CI
    I33MG
    I35NA
    I47ZA

    IS17.site
    IS33.site
    IS35.site
    IS47.site

    What would the true/false testing statement look like when comparing files from one list with files from the other list, if I wanted it to return true only when the numbers (within the file names) match?


    And if you're getting a little tired of my questions, please just let me know... I would understand completely... you've helped a ton already.
     
    Last edited: May 15, 2007
  10. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    Are the lines in the files always in this format...

    first file

    one letter, two figures, two letters

    second file

    two letters, two figures and ".site"?

    Are all the four examples you gave "hits"?

    so
    A17QW
    B17TR

    would match

    SA17.site ?



     
  11. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    yes the examples i gave are all in order to be 'hits'

    and yes the ordering of the filenames is always the same for the two list;
    first file:
    one letter, two figures, two letters

    second file:
    two letters, two figures and ".site"?
     
  12. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    are there just the two files, or numbers of each? do they have distinctive names / extensions?
     
  13. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    there are two text files with a list in each. the lists in each are much longer than the two lists that I typed, but the format of the file names stay the same.

    basically I'm not sure how to write the 'if' statement. I'll give it a go and then it'll be easier for you to correct:

    set name1=I17CI
    set result1=%name1:~1,2%

    set name2=IS17.site
    set result2=%name2:~2,3%.

    if restult1==result2 (command)
     
  14. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    I'll give it an indepth look later, but it looks like you're on the right track.
     
  15. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    Quick string slicing demo

    @echo off
    REM %Somename:~10,5%
    REM
    REM would expand the Somename environment variable, and then use only the 5
    REM characters that begin at the 11th (offset 10) character of the expanded
    REM result. If the length is not specified, then it defaults to the
    REM remainder of the variable value. If either number (offset or length) is
    REM negative, then the number used is the length of the environment variable
    REM value added to the offset or length specified.
    REM

    REM required: starting at 2nd character (offset 1), use 2 characters
    set name1=I17CI
    set result1=%name1:~1,2%

    REM required: starting at 3rd character (offset 2), use 2 characters
    set name2=IS17.site
    set result2=%name2:~2,2%

    if %result1% == %result2% echo same

    REM Saving 2 variables
    if %name1:~1,2% == %name2:~2,2% echo also the same

    Result:

    same
    also the same

    [edit]

    Also, it might be helpful to know what results are going to be interesting to you

    (1) A two digit pair in file1 is found one (or more?) time in file 2
    (2) Vice versa
    (3) The same two digit pair is found in both files
    (4) A two digit pair is found in file1 but not file2
    (5) Vice versa

    And... What is expected... a number in file1 is matched a few or many times in file2, etc...
     
    Last edited: May 16, 2007
  16. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    Ok i've gotten to a point where i'm stuck. Here's part of the code that i've written:

    for /F "delims==" %%s in ('type "%topfolder%\site files\sitefiles.txt"') do (

    if %%%l:~1,2%.==%%%s:~2,3% (copy %topfolder%\site files\%%s %topfolder%\%%d\%%l)


    Now you can probably see some problems with this... as relating to my last post, the variable %%l is of the form "I17CI" and the variable %%s is of the form "IS17.site".

    Now 1. How can I write an if statement to compare the two?
    and 2. Is the copy statement going to work? (the variable %%d is of the form "17-Aug-2006")

    Thank You!
     
    Last edited: May 16, 2007
  17. Indochine

    Indochine Regular member

    Joined:
    Dec 21, 2006
    Messages:
    1,447
    Likes Received:
    0
    Trophy Points:
    46
    Is this a nested loop? Beware!!!

    1. too many % signs! loop variables should have two only.

    2. where did %%s and %%d come from? They can only exist in loops.

    3. that COPY command looks kind weird. You are trying to copy to a folder whose name is generated by variable expansion. Does it exist?







    as relating to my last post, the variable %%l is of the form "I17CI" and the variable %%s is of the form "IS17.site".

    Now 1. How can I write an if statement to compare the two?
    and 2. Is the copy statement going to work? (the variable %%d is of the form "17-Aug-2006")

    Thank You![/quote]
     
  18. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
     
  19. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    Here's the code just in case it makes it easier for you to answer my question: (but likely no need to get into the logic of it)

    echo off

    REM Run this file from the desktop.
    REM Two folders: one containing the site files, and one
    REM containing the event data, must be placed on the desktop.

    set topfolder=%cd%


    echo STEP 1: create sitefile.txt
    pause

    cd %topfolder%\site files

    dir /b > sitefiles.txt


    echo STEP 2: create dates.txt
    pause

    cd %topfolder%\Assign1

    dir /b /ad > dates.txt


    echo STEP 3: move site files over to correct folders


    for /F "delims==" %%d in ('type "%topfolder%\Assign1\dates.txt"') do (

    pause

    echo %%d

    pause

    REM line below will work???

    cd %topfolder%\Assign1\%%d

    ECHO current dir is %cd%

    pause

    dir /ad /b > locations.txt

    for /F "delims==" %%l in ('type "%topfolder%\%%d\locations.txt"') do (

    for /F "delims==" %%s in ('type "%topfolder%\site files\sitefiles.txt"') do (

    if %%%l:~1,2%.==%%%s:~2,2% (copy %topfolder%\site files\%%s %topfolder%\%%d\%%l)

    )
    )

    del %topfolder%\Assign1\%%d\locations.txt > nul
    )


    REM STEP 4: clean up

    del "%topfolder%\site files\sitefiles.txt" > nul

    del "%topfolder%\Assign1\dates.txt" > nul


    REM STEP 5: return to desktop
     
    Last edited: May 16, 2007
  20. aweathe

    aweathe Member

    Joined:
    May 9, 2007
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    16
    hi Indochine,

    I posted a similar question on another forum (computerhope.com) and there's a guy there that's helping me out with this. Thanks for all your help! I can write you back with the solution if you like when I get it.. or maybe this wouldn't be beneficial to you.

    You've given me a great jump start to writing batch files. I've really appreciated all your help and time!!!
     

Share This Page