|
| HOMEPAGE | INDICE FORUM | REGOLAMENTO | ::. | NEI PREFERITI | .:: | RSS Forum | RSS News | NEWS web | NEWS software | |
| PUBBLICITA' | | | ARTICOLI | WIN XP | VISTA | WIN 7 | REGISTRI | SOFTWARE | MANUALI | RECENSIONI | LINUX | HUMOR | HARDWARE | DOWNLOAD | | | CERCA nel FORUM » | |
15-09-2007, 10.45.13 | #1 |
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.461
|
[C] Verificare l'esistenza di una directory
Per la creazione uso una cosa del tipo: system("md nomedirectory"); ma ho la necessità di crearla solo se ancora non esiste e non so come effettuare il controllo |
15-09-2007, 10.46.53 | #2 |
Guest
Messaggi: n/a
|
tramite la funzione access..
qui c'è un esempio: http://www.tek-tips.com/viewthread.c...=960814&page=1 |
15-09-2007, 10.56.00 | #3 |
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.461
|
Azz che rapidità!
Ci sto dando un'occhiata ed ho trovato cose interessanti, grazie |
15-09-2007, 21.27.27 | #4 |
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.461
|
Il problema di verificare l'esistenza di una directory è risolto... sai anche indicarmi come posso leggere i nomi dei file presenti in una cartella?
|
16-09-2007, 00.21.58 | #5 | |
Guest
Messaggi: n/a
|
Quota:
sennò prova a guardare qui http://www.boost.org/libs/filesystem/doc/index.htm |
|
16-09-2007, 18.46.10 | #6 |
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.461
|
A me servirebbe una cosa così:
- il programma legge i nomi dei file (saranno tutti txt) presenti in una directory - mette questi nomi all'interno di un array - i nomi dei file vengono mostrati sulla console con un numero progressivo di fianco - l'utente digita il numero relativo al file che gli interessa e il contenuto di questo viene stampato nella console Saprei fare tutto, tranne il primo punto. Idee? |
16-09-2007, 20.32.29 | #7 | |
Guest
Messaggi: n/a
|
Quota:
beh, se fossi io userei la shell per risparmiare tempo costruirei un system (con le relative stringhe) così: system("dir *.txt /b >listafile"); poi adeguerei la lettura |
|
16-09-2007, 21.02.49 | #8 |
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.461
|
Niente male, non ci avevo pensato a costruire un file contenente i nomi dei file e poi leggerlo, domani ci provo
|
19-09-2007, 08.12.04 | #9 |
Junior Member
Registrato: 12-04-2007
Messaggi: 96
|
Nei vari C esistono due funzioni che servono a identificare i file e leggere le loro caratteristiche dalle directory.
Queste in genere si chiamano FindFirst FindNext La prima funzione ha due argomenti (ora non mi ricordo ma vai a vedere nell'help del compilatore) : il nome del file :accetta carattri jolly struttura con le caratteristiche del file : directory, nascosto, readonly ecc. In pratica se vuoi leggere le directory basta che alla prima chiamata ovvero a findFirst tu setti *." come argomento e dentro al campo attributi ci metti il flag DIRECTORY. Dopo la prima chiamata a FindFirst, viene fatta solo la prima volta, la struutura che hai passaytto come argomento copnterrà il nome della prima dir trovata. Successivamente ad ogni chiamata di findNext (senza piu' argomenti) ti verrà restituito nella struttura il nome della prossima dir oppure la funzione ti restituirà una valore per dirti che non ha piu' trovato file con le caratteristiche settate in findfirst..... E' semplicissimo ..... Codice:
#include <vcl.h> #pragma hdrstop #include "findfirst_form.h" #include "dir.h" //--------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int iAttributes = 0; iAttributes |= faReadOnly * CheckBox1->Checked; iAttributes |= faHidden * CheckBox2->Checked; iAttributes |= faSysFile * CheckBox3->Checked; iAttributes |= faVolumeID * CheckBox4->Checked; iAttributes |= faDirectory * CheckBox5->Checked; iAttributes |= faArchive * CheckBox6->Checked; iAttributes |= faAnyFile * CheckBox7->Checked; // Reset the grid, display a default failure message (which will // be overwritten if a file is found) StringGrid1->RowCount = 2; StringGrid1->FixedRows = 1; StringGrid1->Rows[0]->CommaText = "Filename,Size,Attributes"; StringGrid1->Cells[0][1] = "No Files Found"; StringGrid1->Cells[1][1] = ""; StringGrid1->Cells[2][1] = ""; StringGrid1->Visible = true; displayFiles(Edit1->Text, iAttributes, 1); if (StringGrid1->RowCount > 2) StringGrid1->RowCount--; } //--------------------------------------------------------------------- void TForm1::displayFiles(AnsiString path, int iAttributes, int nestingLevel) { TSearchRec sr; // Creating the nesting prefix for displaying with each file name AnsiString nesting = ""; for (int i = 0; i < nestingLevel; i++) nesting += " "; if (FindFirst(path, iAttributes, sr) == 0) { do { { StringGrid1->Cells[0][StringGrid1->RowCount-1] = nesting + sr.Name; StringGrid1->Cells[1][StringGrid1->RowCount-1] = IntToStr(sr.Size); StringGrid1->Cells[2][StringGrid1->RowCount-1] = "0x" + IntToHex(sr.Attr & faAnyFile, 2); StringGrid1->RowCount = StringGrid1->RowCount + 1; if ((sr.Attr & faDirectory) && DrillDirectories->Checked) { // Do not drill into "." or ".." if (sr.Name != "." && sr.Name != "..") { AnsiString drillPath = appendDirectory(path, sr.Name); displayFiles(drillPath, iAttributes, (nestingLevel+1)); } } } } while (FindNext(sr) == 0); FindClose(sr); } // If we are supposed to drill into directories but the faDirectory flag // was not set, that means findFirst skipped all of the directories. Do // the search again, this time only looking for directories so that we // can drill into them. // if (DrillDirectories->Checked && ((faDirectory & iAttributes) == 0)) { // Use driveAndPath() to Create a path that does not contain a // filename or extension so that directories can be drilled into if (FindFirst(driveAndPath(path) + "*", iAttributes | faDirectory, sr) == 0) { do { if (sr.Attr & faDirectory) { if (sr.Name != "." && sr.Name != "..") { // When we find a directory that can be drilled into, // recurse into that directory so that the matching // files can be displayed. displayFiles(appendDirectory(path, sr.Name), iAttributes, nestingLevel+1); } } } while (FindNext(sr) == 0); } FindClose(sr); } } // Append the newDir directory to the path, retaining any // filename that might already be on the path. // AnsiString TForm1::appendDirectory(AnsiString path, AnsiString newDir) { char drillIntoPath[MAXPATH]; char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE]; char ext[MAXEXT]; fnsplit(path.c_str(),drive,dir,file,ext); strcat(dir, newDir.c_str()); fnmerge(drillIntoPath,drive,dir,file,ext); return(AnsiString(drillIntoPath)); } // Given a fully qualified filename, return just the drive and // path. // AnsiString TForm1::driveAndPath(AnsiString path) { char drillIntoPath[MAXPATH]; char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE]; char ext[MAXEXT]; fnsplit(path.c_str(),drive,dir,file,ext); fnmerge(drillIntoPath,drive,dir,0,0); return(AnsiString(drillIntoPath)); } * faReadOnly * faHidden * faSysFile * faVolumeID * faDirectory * faArchive * faAnyFile Chiaramente a te serve faDirectory Qui hai il link dell'esempio completo http://www.decompile.com/download/findfirst_example.zip (non è mio il sito .... cercato con google...mentre scrivevo a te di cercarlo su google). Ad ogni modo se ti serve invece verificare solo l'esistenza di un file (una directory è di fatto un file) puoi usare la funzione _stat o stat e seconda del compiler che usi......
___________________________________
Flavio Bernardotti Portale programmazione avanzate e sicurezza biometrica http://www.bernardotti.it Ultima modifica di Flavio58 : 19-09-2007 alle ore 08.19.30 |
19-09-2007, 14.30.07 | #10 |
Gold Member
WT Expert
Registrato: 09-01-2002
Loc.: None of your business
Messaggi: 5.505
|
Aggiungo solo pochissime cose a quello che è già stato suggerito
FindFirst e FindNext in Windows hanno i corrispettivi in FindFirstFile e FindNextFile. Entrambe utilizzano una struttura (WIN32_FIND_DATA) utile sia ad impostare gli attributi di ricerca, sia a ricavare gli stessi in base al nome del file ricercato. WIN32_FIND_DATA utilizza un' ulteriore struttura chiamata FILETIME. Gli attributi del file iniziano con FILE_ATTRIBUTE_XXXXX ad esempio FILE_ATTRIBUTE_DIRECTORY e possono essere combinati insieme attraverso la messa in OR. Per quanto concerne l'ottenimento dell'attributo del file si fa ricorso a GetFileAttributes e la verifica si ottiene con un semplice AND ad esempio: Se GetFileAttributes(File) AND attributo ... |
Utenti attualmente attivi che stanno leggendo questa discussione: 1 (0 utenti e 1 ospiti) | |
Strumenti discussione | |
|
|
Discussioni simili | ||||
Discussione | Autore discussione | Forum | Risposte | Ultimo messaggio |
Pagine web che si aprono da sole | imothep | Sicurezza&Privacy | 30 | 09-02-2008 21.05.16 |
Karen's Directory Printer 5.2.3 | Thor | Archivio News Software | 0 | 23-07-2007 10.04.08 |
Directory pubbliche | vincislao | Linux e altri Sistemi Operativi | 0 | 22-02-2007 11.59.47 |
PROBLEMA CON ACTIVE DIRECTORY | Acquario77 | Windows 7/Vista/XP/ 2003 | 2 | 05-10-2004 16.01.18 |
Accesso directory via FTP | Fosforo | Internet e Reti locali | 2 | 12-11-2003 18.26.58 |