Announcement

Collapse
No announcement yet.

Directory name change

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Directory name change

    I have a little problem that I can't figure out. I need to
    delete files from a computer each time it is booted up. I've
    figured out how to delete the files from "8.3" directories with
    no problem, and have used the long file name files I've found
    in other Power Basic forums questions. I need to delete files
    in the client's "C:\Progra~1\Netscape\Users\Somename\Cache".
    Where my problem occurs is that not all my clients have the
    same "Somename" directory under their Users directory. Some are
    "Default" others, well you get the idea. I'm using PB3.5 for
    DOS. Is there any way I can pull the unknown directory from
    Users and then put that directory in my DIR$ so the files can be
    deleted?

    Again the following does work if "default" is the directory
    between "Users" and "Cache":

    Caches$=DIR$("C:\Progra~1\Netscape\Users\DEFAULT\Cache\*.*")
    if Caches$ <> "" then Kill Caches$

    I just need to know how to find out and replace the "DEFAULT"
    directory (or whatever the name of the directory is) in my
    program line. Any help will be appreciated.


    Tony D'Andrea


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

  • #2
    Hi Tony,

    under dos I´ve testet:

    shell "dir /s default. > yourfile"

    The /s option seaches all subdirectories too.

    The '.' point after Your directory is very important;
    it searches only for directories.

    In 'yourfile' you may extract the whole path.

    Hope this help´s.

    Regards

    Matthias Kuhn

    Germany

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

    Comment


    • #3
      The period works only if your sub-directorys don't have an
      extension. Most don't but some do.

      Also, you can use the /b (bare) switch, making /s/b. This
      deletes all unnecessary entries, giving only the names / paths
      specified in the search criteria.


      ------------------
      There are no atheists in a fox hole or the morning of a math test.
      If my flag offends you, I'll help you pack.

      Comment


      • #4
        One word... "Ugh"! I consider this kind of technique of obtaining a directory listing to be very antiquated.

        That is, once you get the initial directory listing in a file, you have to read the content of the file and decypher it. The format of the content will also depend on the O/S and LOCALE information, etc, so your code will have to be tested on a lot of platforms to ensure your code will work reliably.

        You also have to examine each entry to determine if it is a directory or a file and repeat the SHELL operation for each child directory found. This means a lot of inefficient SHELLing and file reading (read: slow operation), and will require a certain amount of free memory be available for the repetitive SHELL operation to be accomplished by the command interpreter.

        A better way is to enumerate the directories using DIR$("*.*",16).

        In the EXAMPLE folder installed with PB/DOS you should find a file that uses recursive calling to enumerate a list of files. (I think it's called PBFIND.BAS or FILEFIND.BAS or similar)

        To me, this would provide a better way of controlling your file and directory enumeration that is an order or magnitude more efficient aand considerably faster. Additionally, you code can delete the files it finds as it goes, and delete empty directories too.

        Anyway, that is my $0.02 on this topic...

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Hi Lance. Just as a matter of clarification, my version of
          PB/DOS doesn't support finding lower level sub-directories. If
          I just go DIR$ ("C:\*.*,16), in my version, it will find files
          and sub-directories in just the root directory. Will the
          latest version of PB/DOS find lower level subs? (AKA tree)
          ------------------


          [This message has been edited by Mel Bishop (edited March 07, 2001).]
          There are no atheists in a fox hole or the morning of a math test.
          If my flag offends you, I'll help you pack.

          Comment


          • #6
            All versions of PowerBASIC support it [indirectly]. I'm not aware of any language that returns a directory or file name below the level specified in the filespec parameter.

            The concept of enumerating a directory tree is performed like this:

            Starting with a given directory level (say, "C:\*.*"), you read (enumerate) the returned names and test whether they are directories or files. If you encounter a directory, you build that directory name into the the next DIR$() call (ie, if you find a directory called "DOS", you would then enumerate "C:\DOS\*.*").

            This process keeps repeating until no further sub directories can be located for a given directory name. WHile the coding for this may seem to be complicated at first, using a recursive function call makes this a breeze, although writing bug-free recursive function can be difficult, since sorting out variable scope gets complicated, and you have to ensure you do not overflow the stack frame with deep recursion.

            A "basic" example of a recursive function is the Factorial example noted in the PB User Guide.

            For a more intricate example, please study the file I previously mentioned... fully understanding how it works is a good test to see how well your brain operates first thing in the morning without first drinking a couple of cups of strong coffee.

            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              This code is extracted from my tool chest.
              You may find it of use.

              Dave
              Code:
                  $IF 0
              
                      A recursive file finder
              
                      Various bits of this code were pinched from
                      a wide range other people -  thanks to them
              
                      Dave Stanton 8/03/2001
              
                  $ENDIF
              
                  DIM DYNAMIC Fils$(1:1)
                  Filemask$="PB*.EXE"
                  Rootpath$="C:\"
                  F??= Whereis(Filemask$,Rootpath$,Fils$())
                  PRINT Fils$(1)
                  FOR Q?? = 2 TO F??
                    PRINT Fils$(Q??)
                  NEXT Q??
                  PRINT USING$("###### Files",F??)
              
              
                   FUNCTION Whereis(Filemask$,Rootpath$,Fils$()) PUBLIC AS WORD
                       LOCAL D$(),Fptr??,Dptr??
                       DIM D$(0:100)
                       Fptr??=0
                       Dptr??=0
                       D$(0)=Rootpath$
                       Whereinbranch Filemask$,Fils$(),Fptr??,D$(),Dptr??
                       IF Fptr?? THEN
                          REDIM PRESERVE Fils$(1:Fptr??)
                       ELSE
                          Fils$(1)="No matching files for "+Filemask$
                       END IF
                       FUNCTION=Fptr??
                   END FUNCTION
              
                  SUB Whereinbranch(Filemask$,Fils$(),Fptr??,D$(),Dptr??)
                      SHARED Dmax??
                      LOCAL Z$(),Q??,Newsize??,Chunk?
                      DIM Z$(1:10)
                      Chunk?=25 'Extra room in Fils$()
                      Lpath$=RTRIM$(D$(Dptr??))
                      IF RIGHT$(Lpath$,1)<>"\" THEN Lpath$=Lpath$+"\"
                      Q??=Getfilenames(Lpath$+Filemask$,Z$(),0)'Files
                      IF Fptr??+Q??>UBOUND(Fils$) THEN
                         Newsize??=Fptr??+Q??+Chunk?
                         REDIM PRESERVE Fils$(1:Newsize??)
                      END IF
                      FOR P??=1 TO Q??
                          INCR Fptr??
                          Fils$(Fptr??)=Lpath$+Z$(P??)
                      NEXT P??
                      Q??=Getfilenames(Lpath$+"*.*",Z$(),1)'Subdir
                      FOR P??=1 TO Q??
                          INCR Dptr??
                          D$(Dptr??)=Lpath$+Z$(P??)
                          Whereinbranch Filemask$,Fils$(),Fptr??,D$(),Dptr??
                      NEXT P??
                      Dmax??=MAX(Dmax??,Dptr??)
                      Dptr??=Dptr??-Q??
                  END SUB
              
                   FUNCTION Getfilenames(Fmask$,Z$(),Flag?)  PUBLIC AS WORD
                       LOCAL Filname$,Q$,Q1$,Attr?
                       Q$=""
                       IF Flag?=0 THEN Attr?=0  ' Normal files
                       IF Flag?=1 THEN Attr?=16 ' Sub directories
                       Filname$=DIR$(Fmask$,Attr?)
                       DO UNTIL Filname$=""
                          Readdta Filn$,Fsize???,Fattr?,Tim$,Dat$
                          IF (Fattr? AND Attr?)=Attr? THEN
                              Q1$=SPACE$(12)
                              MID$(Q1$,1,12)=Filname$
                              Q$=Q$+Q1$
                          END IF
                          Filname$=DIR$
                       LOOP
                       Alen??=LEN(Q$)/12
                       IF Alen?? THEN
                          REDIM Z$(1:Alen??)
                          FOR Q??= 1 TO Alen??
                             Z$(Q??)=MID$(Q$,Q??*12-11,12)
                          NEXT Q??
                       ELSE
                          Z$(1)="" 'No matching files
                       END IF
                       FUNCTION=Alen??
                   END FUNCTION
              
              
                  SUB Readdta(Filname$,Fsize???,Fattr?,Tim$,Dat$)  PUBLIC
                      LOCAL Dtaseg??,Dtaoff?,Temp?
                      Getdta Dtaseg??,Dtaoff??
                      DEF SEG = Dtaseg??
                      Fattr?=PEEK(Dtaoff?? + &H15)
                      Temp??=BITS??(PEEKI(Dtaoff??+&H16))
                      Secs?=2*(Temp?? AND &H1F)
                      SHIFT RIGHT Temp??,5
                      Mins?=Temp?? AND &H3F
                      SHIFT RIGHT Temp??,6
                      Hour?=Temp?? AND &H1F
                      Tim$= Zpad$(Hour?)+":"+Zpad$(Mins?)+":"+Zpad$(Secs?)
                      Temp??=BITS??(PEEKI(Dtaoff??+&H18))
                      Day?= Temp?? AND &H1F
                      SHIFT RIGHT Temp??,5
                      Mon?= Temp?? AND &H0F
                      SHIFT RIGHT Temp??,4
                      Year?=80+(Temp?? AND &H7F)
                      Dat$= Zpad$(Day?)+"/"+Zpad$(Mon?)+"/"+Zpad$(Year?)
                      Fsize???=BITS???(PEEKL(Dtaoff??+&H1A))
                      Filname$=PEEK$(Dtaoff??+&H1E,13)
                      DEF SEG
                   END SUB
              
              
                   SUB Getdta(Dtaseg??,Dtaoff??)   PUBLIC
                       ! push ds
                       ! push si
                       ! mov  ax,&H2F00
                       ! int &h21
                       ! lds si,Dtaseg??
                       ! mov ds:[si],es
                       ! lds si,Dtaoff??
                       ! mov ds:[si],bx
                       ! pop si
                       ! pop ds
                   END SUB
              
                   FUNCTION Zpad$(BYVAL N)    PUBLIC
                       FUNCTION=Charpad$(N,2,48)
                   END FUNCTION
              
                   FUNCTION Charpad$(BYVAL N,BYVAL Siz?,BYVAL Pchar?)  PUBLIC
                       LOCAL Temp$
                       Temp$=STRING$(Siz?,Pchar?)+Ltrim$(STR$(N))
                       FUNCTION=RIGHT$(Temp$,Siz?)
                   END FUNCTION
              ------------------
              Dave.

              You're never too old to learn something stupid.

              Comment


              • #8
                I would like to take a moment and thank everyone for helping
                me with this project. In time I hope I can help someone using
                PB products. TIA



                Tony D'Andrea
                [email protected]


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

                Comment

                Working...
                X