Copy Directory for file with no extension

Help
Hartono
2009-05-22
2013-06-12
  • Hartono

    Hartono - 2009-05-22

    Usually in order to copy several files inside a directory to another directory, I use this.
    <stafcmd>
        <location>HostMachine</location>
        <service>'FS'</service>
        <request>'COPY DIRECTORY \                    %s \                    TODIRECTORY \                    %s \                    EXT %s \                    TOMACHINE %s' \                    %(FilePath, CopyPath, Extension, TargetMachine)
        </request>
    </stafcmd>

    that way.. I can specify the extension for the file either 'txt', *, or anything else...
    But is there any way to copy a bunch of file with no extension (for example in linux some files does not have extension) to another directory?

     
    • David Michael Bender

      If you don't want to copy the entire directory, then you would need to use the NAME option on the FS COPY DIRECTORY request.

       
      • Hartono

        Hartono - 2009-05-26

        Using the name option it will only give the one that the name specify.
        What I have is a bunch of file with different name. So I would like to copy all of the files in that directory that don't have extension at all. for example:
        abc
        asfd.txt
        asdfs
        sad.html
        sfs.jpg
        adf

        then what I would like to get copied are abc, asdfs, and adf.
        Is it possible

         
        • Sharon Lucas

          Sharon Lucas - 2009-05-26

          You currently cannot specify no extension on a FS COPY DIRECTORY request.  When you specify nothing for the EXT option (which you can via the ColonLengthColon delimiter format, e.g. EXT :0:), that defaults to * (the wildcard that matches all extensions).  Please open a feature request for the FS COPY DIRECTORY request be changed so that you can specify nothing for the EXT option to allow specifying copying files with no extension.  Or, if you prefer that I open the feature request, post again to let me know and I'll open this for you.

          Instead, for now to get around this, you could submit a FS COPY FILE request for each file name that doesn't have an extension that you want to copy.  If you don't know the file names in the directory that don't have an extension, then you can write a function in your STAX job that list the files in the directory (via a "LIST DIRECTORY <fromDirectory> TYPE F" request submitted to the FS service) and check which names don't have an extension and then submit a "COPY FILE" request for each file.  I've included a STAX job at the end of my posting that shows how you can do this.

          Or, you could use the <import> element to import the STAXUtilCopyFiles function provided in the STAXUtil.xml file that is part of the STAX zip/tar file you downloaded (located in the stax/libraries directory where you extracted the STAX zip/tar file) and use the regularExpression argument to specify a regular expression that can be used to match the child entries with no extension.  To get more information on the STAXUtilCopyFiles function in the STAXUtil library, see http://staf.sourceforge.net/current/STAXLibraries/index.html.

          Here's an example of a STAX job that first submits a FS LIST DIRECTORY request and then determines which file names don't have an extension and submits a FS COPY FILE request for each of the file names that don't have an extension.

          <?xml version="1.0" encoding="UTF-8" standalone="no"?>
          <!DOCTYPE stax SYSTEM "stax.dtd">
          <stax>
            <defaultcall function="main"/>

            <function name="main">
              <sequence>

                <!-- Set your variables to be passed to the CopyFiles function -->
                <script>
                  HostMachine = 'local'               # from Machine
                  FilePath = 'C:/temp/fsdir'          # from Directory
                  CopyPath = 'C:/temp/fsdircopy'      # to Directory
                  TargetMachine = 'local'             # to Machine
                </script>

                <call function="'CopyFilesInDirectoryWithNoExtension'">
                  { 'fromMachine': HostMachine, 'fromDirectory': FilePath,
                    'toDirectory': CopyPath, 'toMachine': TargetMachine }
                </call>

                <script>[copyRC, numFilesCopied, errorMsg] = STAXResult</script>

                <if expr="copyRC == 0">
                  <log message="1">
                    'Copied %s files with no extension' % (numFilesCopied) + \           ' from directory %s on machine %s' % (FilePath, HostMachine) + \           ' to directory %s on machine %s' % (CopyPath, TargetMachine)
                  </log>
                  <else>
                    <log message="1">
                      'ERROR:  Copying files with no extension' + \             ' from directory %s on machine %s' % (FilePath, HostMachine) + \             ' to directory %s on machine %s failed' % (CopyPath, TargetMachine) + \             '\n%s' % (errorMsg)
                    </log>
                  </else>
                </if>

              </sequence>
            </function>

            <!-- Copy files with no extension -->

            <function name="CopyFilesInDirectoryWithNoExtension">

              <function-map-args>
                <function-required-arg name="fromMachine"/>
                <function-required-arg name="fromDirectory"/>
                <function-required-arg name="toDirectory"/>
                <function-required-arg name="toMachine"/>
              </function-map-args> 

              <sequence>
                   
                <script>
                  numFilesCopied = 0
                  errMsg = ''
                  errorList = []

                  from com.ibm.staf import STAFUtil
                  request = 'LIST DIRECTORY %s TYPE F' % (STAFUtil.wrapData(fromDirectory))
                </script>

                <stafcmd>
                  <location>HostMachine</location>
                  <service>'FS'</service>
                  <request>request</request>
                </stafcmd>

                <if expr="RC != STAFRC.Ok">
                  <sequence>
                    <script>
                      errMsg = 'STAF %s FS %s failed, RC=%s STAFResult=%s' % \               (HostMachine, request, RC, STAFResult)
                    </script>
                    <return>[1, numFilesCopied, errMsg]</return>
                  </sequence>
                </if>

                <if expr="len(STAFResult) == 0">
                  <return>[0, numFilesCopied, errMsg]</return>
                </if>

                <script>fileList = STAFResult</script>

                <!-- Create the TODIRECTORY if it doesn't already exist -->

                <script>
                  request = 'CREATE DIRECTORY %s FULLPATH' % (STAFUtil.wrapData(toDirectory))
                </script>

                <stafcmd>
                  <location>fromMachine</location>
                  <service>'FS'</service>
                  <request>request</request>
                </stafcmd>

                <if expr="RC != STAFRC.Ok">
                  <sequence>
                    <script>
                      errMsg = 'STAF %s FS %s failed. RC=%s STAFResult=%s' % \               (fromMachine, request, RC, STAFResult)
                    </script>
                    <return>[1, numFilesCopied, errMsg]</return>
                  </sequence>
                </if>

                <iterate var="fileName" in="fileList">
                  <sequence>

                    <script>
                      import os
                      name,ext = os.path.splitext(fileName)
                    </script>

                    <if expr="ext == ''">
                      <sequence>

                        <!-- Filename has no extension -->

                        <script>
                          fromFile = '%s/%s' % (fromDirectory, fileName)

                          request = 'COPY FILE %s TODIRECTORY %s TOMACHINE %s' % \                   (STAFUtil.wrapData(fromFile), STAFUtil.wrapData(toDirectory),
                             STAFUtil.wrapData(toMachine))
                        </script>

                        <stafcmd>
                          <location>fromMachine</location>
                          <service>'FS'</service>
                          <request>request</request>
                        </stafcmd>

                        <if expr="RC == STAFRC.Ok">
                          <script>
                            numFilesCopied += 1
                          </script>
                          <else>
                            <script>
                              errMsg = 'Copying file \'%s\' failed. RC=%s STAFResult=%s' % \                       (fileName, RC, STAFResult)
                              errorList.append(errMsg)
                            </script>
                          </else>
                        </if> 

                      </sequence>
                    </if>

                  </sequence>
                </iterate>

                <script>
                  if len(errorList) == 0:
                    copyRC = 0
                  else:
                    copyRC = 1
                </script>

                <return>[copyRC, numFilesCopied, errorList]</return>

              </sequence>

            </function>
          </stax>

           
  • Anonymous - 2013-01-31

    Old post but the answer is in documentation. Example for delete service :

    Goal: Delete all files in the /tmp directory that have no extension. Note that you must use the length delimited format, :0:, to indicate an empty string for the EXT option because if you specify no value or "", then the STAF command parser thinks that no value was specified for the EXT option and this will cause an "Invalid Request String" error (RC 7) since the EXT option requires a value.
    Syntax:  DELETE ENTRY /tmp CHILDREN EXT :0: TYPE F CONFIRM

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks