Page 1 sur 1
Chemin Relatif
Publié : ven. 20/avr./2007 9:00
par Thyphoon
Voilà je partage une petite procedure que je me suis fait qui permet de transformer un chemin absolu en chemin relatif C'est tres pratique
Code : Tout sélectionner
Procedure.s RelativePath(FromPath.s,Path.s)
Protected l.w,c.w,NewPath.s
Newpath.s=""
c=0
For l=Len(FromPath) To 1 Step -1 ; On lit a l'envers
If Mid(FromPath,l,1)="\" ; A chaque fois que je trouve un nouveau repertoire on compte
c+1
If FindString(Path,Mid(FromPath,1,l),1) ; Si on retrouve alors le même chemin commun que le chemin original alors on casse la boucle
Break
EndIf
EndIf
Next l
For z=1 To c-1
Newpath+"..\"
Next
Newpath+Right(Path,Len(Path)-l)
ProcedureReturn NewPath
EndProcedure
exemple :
Code : Tout sélectionner
File.s="C:\Documents And Settings\lebrun_y\Mes documents\Developpement\Planetes Aliens\MapEditor\Sfx\porte.wav"
Path.s="C:\Documents And Settings\lebrun_y\Mes documents\Developpement\Planetes Aliens\MapEditor\"
Debug RelativePath(Path,File)
voilà

la fonction peut surement être améliorer. Mais ça marche bien !
Re: Chemin Relatif
Publié : ven. 20/avr./2007 9:33
par Backup
Thyphoon a écrit :Voilà je partage une petite procedure que je me suis fait qui permet de transformer un chemin absolu en chemin relatif C'est tres pratique

voilà

la fonction peut surement être améliorer. Mais ça marche bien !

ça me renvoie ce que je lui donne dans "File.s"
donc je vois pas l'interet !?? t'es sur que ça marche ton truc ?

Publié : ven. 20/avr./2007 10:12
par Thyphoon
c'est currieux ça ...
Enfin non c'est normal si il n'y a pas de chemin commun entre file et frompath
tient regarde ce code
Code : Tout sélectionner
Procedure.s RelativePath(FromPath.s,Path.s)
Protected l.w,c.w,NewPath.s
Newpath.s=""
c=0
For l=Len(FromPath) To 1 Step -1 ; On lit a l'envers
If Mid(FromPath,l,1)="\" ; A chaque fois que je trouve un nouveau repertoire on compte
c+1
If FindString(Path,Mid(FromPath,1,l),1) ; Si on retrouve alors le même chemin commun que le chemin original alors on casse la boucle
Break
EndIf
EndIf
Next l
For z=1 To c-1
Newpath+"..\"
Next
Newpath+Right(Path,Len(Path)-l)
ProcedureReturn NewPath
EndProcedure
File.s="C:\Documents And Settings\lebrun_y\Mes documents\Developpement\Planetes Aliens\MapEditor\Sfx\porte.wav"
Path.s="C:\Documents And Settings\lebrun_y\Mes documents\Developpement\Planetes Aliens\MapEditor\"
Debug RelativePath(Path,File)
Le but étant de trouver le chemin non pas a partir de la racine mais a partir du "frompath"
Publié : ven. 20/avr./2007 11:32
par Backup
chez moi ça reviens a faire
file.s= "D:\Mes_Photos\Capture_ecran\puregolo3D\croix.jpg"
chemin$= Right (file.s, Len (file.s)-3)
Debug chemin$
c'est ça ?
[Reedit]
ha non ! , je vois ok ! autant pour moi

Publié : ven. 20/avr./2007 11:39
par Thyphoon
lollllllllllllllllll
bon je te fais un programme pour te montrer exactement
Code : Tout sélectionner
Procedure.s RelativePath(FromPath.s,Path.s)
Protected l.w,c.w,NewPath.s
Newpath.s=""
c=0
For l=Len(FromPath) To 1 Step -1 ; On lit a l'envers
If Mid(FromPath,l,1)="\" ; A chaque fois que je trouve un nouveau repertoire on compte
c+1
If FindString(Path,Mid(FromPath,1,l),1) ; Si on retrouve alors le même chemin commun que le chemin original alors on casse la boucle
Break
EndIf
EndIf
Next l
For z=1 To c-1
Newpath+"..\"
Next
Newpath+Right(Path,Len(Path)-l)
ProcedureReturn NewPath
EndProcedure
File$=OpenFileRequester("Selectionne un fichier", FichierParDefaut$, Filtre$, Filtre)
While File$
Debug RelativePath(GetCurrentDirectory(),File$);RemoveString(File$, GetCurrentDirectory())
File$ = NextSelectedFileName()
Wend
donc tu compiles et tu prend un fichier ou tu veux...

et normalement au lieu de te mettre un chemin style c:\truc\bidule\machin\chose\monfichier.ext et bien tu aura le chemin a partir du repertoire ou a été executer ton programme (il faut peut être sauvegarder la source) et donc dans le genre ..\..\chose\monfichier.ext
dit moi si ça marche...lollllll D'autre on essayé ?
Publié : ven. 20/avr./2007 11:52
par tmyke
Je viens d'essayer, car au début je comprenais pas trop le but, mais maintenant
je vois nettement mieux. Ta routine est en effet très pratique dans certains
cas. Maintenant, je n'ai pas analysé le code.
Mais je la garde dans un petit coin, cela me servira, c'est sur

Publié : ven. 20/avr./2007 11:55
par Backup
ok ! super , je garde aussi
[Reedit] voila c'est sur mon Forum de Code_fr

[/Reedit]
Merci

Publié : ven. 20/avr./2007 12:04
par Thyphoon
Chouette ! pour une fois ou je fais quelques chose qui peut être utile

Publié : ven. 20/avr./2007 19:12
par Thyphoon
Petite correction
Sur le forum Anglais quelqu'un m'a fait remarquer que si le fichier se trouvait sur une autre unité (disque dur) alors ...ça pausait un problème
voilà qui devrait régler le problème !!
Code : Tout sélectionner
Procedure.s RelativePath(FromPath.s,Path.s)
Protected l.w,c.w,NewPath.s
Newpath.s=""
c=0
; Debug Path
; Debug FromPath
; Debug Mid(Path,1,1)
; Debug Mid(FromPath,1,1)
If Mid(Path,1,1)<>Mid(FromPath,1,1)
ProcedureReturn Path
EndIf
For l=Len(FromPath) To 1 Step -1 ; We stat at the end of the string
If Mid(FromPath,l,1)="\" ; I count all new folder
c+1
If FindString(Path,Mid(FromPath,1,l),1) ; If we have the same path we break the loop
Break
EndIf
EndIf
Next l
For z=1 To c-1
Newpath+"..\"
Next
Newpath+Right(Path,Len(Path)-l)
ProcedureReturn NewPath
EndProcedure
File$=OpenFileRequester("Choose a File", FichierParDefaut$, Filtre$, Filtre)
While File$
Debug "Absolute Path:"+File$
Debug "Relative Path:"+RelativePath(GetCurrentDirectory(),File$)
File$ = NextSelectedFileName()
Wend
Publié : sam. 21/avr./2007 10:30
par Thyphoon
juste un truc...on peut améliorant en utilisant
Code : Tout sélectionner
If Left(Path, 1) <> Left(FromPath), 1)
ProcedureReturn Path
EndIF
au lieu de
Code : Tout sélectionner
If Mid(Path,1,1)<>Mid(FromPath,1,1)
ProcedureReturn Path
EndIf
Publié : sam. 28/avr./2007 17:36
par Dr. Dri
Je propose de profiter des possibilités de PB4, à savoir les paramètres optionnels
Code : Tout sélectionner
Procedure.s RelativePath(Path.s, FromPath.s = "")
Protected l.w,c.w,NewPath.s
Newpath.s=""
c=0
If Not FromPath
FromPath = GetCurrentDirectory()
EndIf
If Left(Path, 1) <> Left(FromPath, 1)
ProcedureReturn Path
EndIf
For l=Len(FromPath) To 1 Step -1
If Mid(FromPath, l, 1)="\"
c+1
If FindString(Path, Left(FromPath,l), 1)
Break
EndIf
EndIf
Next l
For z=1 To c-1
Newpath+"..\"
Next
Newpath+Right(Path, Len(Path)-l)
ProcedureReturn NewPath
EndProcedure
File$=OpenFileRequester("Choose a File", FichierParDefaut$, Filtre$, Filtre)
While File$
Debug "Absolute Path:"+File$
Debug "Relative Path:"+RelativePath(File$)
File$ = NextSelectedFileName()
Wend
Dri
Publié : sam. 28/avr./2007 17:47
par Thyphoon
Très bonne idée Dr. Dri

Publié : mar. 15/janv./2008 17:38
par GG
Excellent...
Publié : mar. 15/janv./2008 18:10
par Ar-S
Je n'avais pas pigé non plus, maintenant si

Elle s'avérer bien pratique effectivement.
Publié : mar. 15/janv./2008 18:28
par Thyphoon
Ar-S a écrit :Je n'avais pas pigé non plus, maintenant si

Elle s'avérer bien pratique effectivement.
lolllll comme quoi parfois je dis des choses interessantes (Ok c'est pas souvant

)