Les Snippets

Connexion

Vérifier et forcer la validité d'un fichier (caractères interdits)

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 08/09/2007 10:37:35 et initié par PCPT [Liste]
Date de mise à jour : 14/09/2007 14:08:48
Vue : 6500
Catégorie(s) : Fichier / Disque, Chaîne de caractères
Langages dispo pour ce code :
- VB6, VBA
- Delphi 5
- C# 2.x, C# 3.x
- VB 2005, VB 2008



Langage : VB6 , VBA
Date ajout : 08/09/2007
Posté par PCPT [Liste]
Sub SetValidFile(ByRef sFile As StringOptional ByVal sReplace As String = "_")
'   sReplace doit contenir un seul  caractère (valide)
    Const sForbidden As String "\/:*?""<>|"
    Dim As Integer
    
    If (Not LenB(sReplace) = 2Or (InStrB(1,  sForbidden, sReplace)) Then sReplace = "_"
    If LenB(sFile) Then
        For i = To LenB(sFile) Step 2
            If InStrB(1, sForbidden, MidB$(sFile, i, 2)) Then MidB$(sFile, i, 2) = sReplace
        Next i
    End If
End Sub
'
'
'  =====================
' EXEMPLE  D'UTILISATION
'  =====================
'
    Dim sFile As String
    sFile = "mon fichier?de dest*ination|.txt"
    Call SetValidFile(sFile)
'   sFile  contient alors "mon fichier_de dest_ination_.txt"

Langage : Delphi 5
Date ajout : 14/09/2007
Posté par f0xi [Liste]
DateMAJ : 14/09/2007
{ renvois True si le nom de fichier est valide }
function IsValidFilename(const FileName : string) : boolean;
var
  n : integer;
const
  fbd = ['\','/',':','*','?','"','<','>','|'];
begin
  for n := 1 to Length(FileName) do
  begin
    result := not (FileName[N] in fbd);
    if not result then
      exit;   
  end;
end;

{ retourne FileName avec tout les caracteres invalides remplacés par underscore "_" }
function SafeValidFilename(const FileName : string) : string;
var
  pBOut : ^byte;
const
  fbd = [$22,$2a,$2f,$3a,$3c,$3e,$3f,$5c,$7c];
begin
  result := filename;
  pBOut:= @Result[1];
  while pBOut^ <> $0 do
  begin
    if pBOut^ in fbd then
      pBOut^ := $5f;
    inc(pBOut);
  end;
end;

Langage : C# 2.x , C# 3.x
Date ajout : 10/12/2008
Posté par Charles Racaud [Liste]
private static bool IsValideFileName(string FileName) {
  char[] InvalidFileNameChars = System.IO.Path.GetInvalidFileNameChars();
  foreach (char InvalidFileNameChar in InvalidFileNameChars)
    if (FileName.Contains(InvalidFileNameChar.ToString()))
      return false;
  return true;
}
private static string ConvertToValideFileName(string FileName, char RemplaceChar) {
  char[] InvalidFileNameChars = System.IO.Path.GetInvalidFileNameChars();
  foreach (char InvalidFileNameChar in InvalidFileNameChars)
    if (FileName.Contains(InvalidFileNameChar.ToString()))
      FileName = FileName.Replace(InvalidFileNameChar, RemplaceChar);
  return FileName;
}
Langage : VB 2005 , VB 2008
Date ajout : 10/12/2008
Posté par Charles Racaud [Liste]
Private Function IsValideFileName(ByVal FileName As StringAs Boolean
  Dim InvalidFileNameChars() As Char = System.IO.Path.GetInvalidFileNameChars()
  For Each InvalidFileNameChar As Char In InvalidFileNameChars
    If FileName.Contains(InvalidFileNameChar.ToString()) Then Return False
  Next
  Return True
End Function
Private Function ConvertToValideFileName(ByVal FileName As StringByVal RemplaceChar As CharAs String
  Dim InvalidFileNameChars() As Char = System.IO.Path.GetInvalidFileNameChars()
  For Each InvalidFileNameChar As Char In InvalidFileNameChars
    If FileName.Contains(InvalidFileNameChar.ToString()) Then FileName = FileName.Replace(InvalidFileNameChar, RemplaceChar)
  Next
  Return FileName
End Function

Snippets en rapport avec : Fichier, Validité, Chaîne, Format, Écriture



Codes sources en rapport avec : Fichier, Validité, Chaîne, Format, Écriture

{C / C++ / C++.NET} CREATION DE FICHIER PDF
Ce code montre comment créer un fichier PDF grâce à la librairie SFPDF que je développe également. C...

{C / C++ / C++.NET} GESTION DES CHAINES DE CARACTÈRES POUR OUVERTURE ET FERMETURE DES FICHIERS - WINDOWS
Gèere les chaines de caractère des chemins de fichiers (pratique lorsque l'on utilise les argv avec ...

{Visual Basic, VB6, VB.NET, VB 2005} JEUX D'ENREGISTREMENT DANS UN FICHIER
JEUX D'ENREGISTREMENT DANS UN FICHIER-->ajouter, supprimer, consulter des enregistrements a partir d...

{Delphi} DÉCRYPTER LES FICHIERS IMAGE JPG (VOUS AVEZ ÉGARÉ LA CLÉ)
Certains fichiers jpg cryptés nécessitent une clé pour être visionnés. L'algorythme de cryptage est ...

{PHP} ECRITURE AU DÉBUT D'UN FICHIER SANS ÉFFACER LE RESTE
Cette fonction vous permettra d'écrire au début d'un fichier. Elle prends en paramètres le fichier ...

{C / C++ / C++.NET} SUITE ET ÉCRITURE DANS UN FICHIER
bon ok ce code est super pourri et inutile mais c'est mon premier et j'en suis fier. J'ai allumé pou...

{Visual Basic, VB6, VB.NET, VB 2005} TROUVER LE FORMAT D'UN FICHIER MULTIMEDIA
Il suffit de relever son extension et de la comparer avec celles contenues dans WIN.INI ...

{Visual Basic, VB6, VB.NET, VB 2005} CRYPTAGE DÉCRYPTAGE DE FICHIER PAR MOT DE PASSE
Cette source crypt et decrypt simplement par un mot de passe et un codage interne à la source, le se...

{C / C++ / C++.NET} COPIE AVEC SHFILEOPERATION
Bonjour Voici un exemple de l'utilisation de la fonction SHFileOperation de l'api windows. La ...

{Visual Basic, VB6, VB.NET, VB 2005} FILE FOLDER LOCKER - BLOQUER (ET DÉBLOQUER) L'ACCÈS À CERTAINS FICHIERS ET DOSSIERS [DLL]
Cette DLL est une collection de fichier et dossier sur laquelle vous pourrez appliquer un verrou ...