[Gambas-user] Add new command to: Copy directories recursively
Caveat
Gambas at ...1950...
Tue Aug 26 08:21:37 CEST 2014
Hello List
Although there is always "cp -r", it makes for a very unportable program
if you depend on calling external programs. Better to build it in to
the language if it's generally and genuinely useful, or to make a
callable function in some public repository of Gambas code, so people
don't need to reinvent the wheel every time they need a recursive copy.
Kind regards,
Caveat
On 25/08/14 23:04, Tobias Boege wrote:
> On Mon, 25 Aug 2014, Julio Sanchez wrote:
>> Hi, could you add a new order within gambas3 to copy directories
>> recursively.
>>
>> Something like this:
>>
>> PUBLIC <http://gambaswiki.org/wiki/lang/public> SUB
>> <http://gambaswiki.org/wiki/lang/sub> copia_dir(path_origen AS
>> <http://gambaswiki.org/wiki/lang/as> String
>> <http://gambaswiki.org/wiki/lang/type/string>, path_destino AS
>> <http://gambaswiki.org/wiki/lang/as> String
>> <http://gambaswiki.org/wiki/lang/type/string>, OPTIONAL
>> <http://gambaswiki.org/wiki/lang/optional> tipo as
>> <http://gambaswiki.org/wiki/lang/as> String
>> <http://gambaswiki.org/wiki/lang/type/string>)
>>
>> DIM <http://gambaswiki.org/wiki/lang/dim> arDir AS
>> <http://gambaswiki.org/wiki/lang/as> string
>> <http://gambaswiki.org/wiki/lang/type/string>[]
>> DIM <http://gambaswiki.org/wiki/lang/dim> arFile AS
>> <http://gambaswiki.org/wiki/lang/as> string
>> <http://gambaswiki.org/wiki/lang/type/string>[]
>> DIM <http://gambaswiki.org/wiki/lang/dim> nombredir, nombrefile AS
>> <http://gambaswiki.org/wiki/lang/as> String
>> <http://gambaswiki.org/wiki/lang/type/string>
>>
>> IF <http://gambaswiki.org/wiki/lang/if> NOT
>> <http://gambaswiki.org/wiki/lang/not> tipo then
>> <http://gambaswiki.org/wiki/lang/then> tipo="*"
>> IF <http://gambaswiki.org/wiki/lang/if> NOT
>> <http://gambaswiki.org/wiki/lang/not> Exist
>> <http://gambaswiki.org/wiki/lang/exist>(path_destino) THEN
>> <http://gambaswiki.org/wiki/lang/then>
>> MKDIR <http://gambaswiki.org/wiki/lang/mkdir> path_destino
>> ENDIF <http://gambaswiki.org/wiki/lang/endif>
>> arfile = Dir <http://gambaswiki.org/wiki/lang/dir>(path_origen, tipo, gb.
>> file <http://gambaswiki.org/wiki/lang/type/file>) 'extraemos los ficheros
>> FOR <http://gambaswiki.org/wiki/lang/for> EACH
>> <http://gambaswiki.org/wiki/lang/each> nombrefile IN
>> <http://gambaswiki.org/wiki/lang/in> arfile 'los copiamos
>> COPY <http://gambaswiki.org/wiki/lang/copy> path_origen &/
>> nombrefile TO <http://gambaswiki.org/wiki/lang/to> path_destino &/
>> nombrefile
>> NEXT <http://gambaswiki.org/wiki/lang/next>
>> ardir = Dir <http://gambaswiki.org/wiki/lang/dir>(path_origen, "*", gb.
>> Directory)
>> FOR <http://gambaswiki.org/wiki/lang/for> EACH
>> <http://gambaswiki.org/wiki/lang/each> nombredir IN
>> <http://gambaswiki.org/wiki/lang/in> arDir 'extraemos los subdirectorios
>> copia_dir(path_origen &/ nombredir, path_destino &/ nombredir) 'usamos
>> la recursividad
>> NEXT <http://gambaswiki.org/wiki/lang/next>
>> END <http://gambaswiki.org/wiki/lang/end>
>>
> For the other people who can't read the above HTML-like mail, try to pipe
> it through "sed ':a;$!N;s/\n\?<[^>]*>//;ta;P;D'". There are only a few
> places to fix up manually then:
>
> --8<------------------------------------------------------------------------
> PUBLIC SUB copia_dir(path_origen AS String, path_destino AS String, OPTIONAL tipo as String)
>
> DIM arDir AS string[]
> DIM arFile AS string[]
> DIM nombredir, nombrefile AS String
>
> IF NOT tipo then tipo="*"
> IF NOT Exist(path_destino) THEN
> MKDIR path_destino
> ENDIF
> arfile = Dir (path_origen, tipo, gb.file ) 'extraemos los ficheros
> FOR EACH nombrefile IN arfile 'los copiamos
> COPY path_origen &/ nombrefile TO path_destino &/ nombrefile
> NEXT
> ardir = Dir (path_origen, "*", gb.Directory)
> FOR EACH nombredir IN arDir 'extraemos los subdirectorios
> copia_dir(path_origen &/ nombredir, path_destino &/ nombredir) 'usamos la recursividad
> NEXT
> END
> --8<------------------------------------------------------------------------
>
> I'm not a fan of adding a new instruction for something that can be done in
> just a few lines once and for all. Here is my version[0], even a little
> shorter (plus it can copy single files, too):
>
> --8<------------------------------------------------------------------------
> Public Sub CopyDir(sSrc As String, sDst As String)
> Dim sRel As String
>
> If IsDir(sSrc) Then
> Mkdir sDst
> For Each sRel In Dir(sSrc)
> CopyDir(sSrc &/ sRel, sDst &/ sRel)
> Next
> Else
> Copy sSrc To sDst
> Endif
> End
> --8<------------------------------------------------------------------------
>
> And then there is also "cp -r" at your disposal.
>
> Regards,
> Tobi
>
More information about the User
mailing list