| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1152 人关注过本帖
标题:关于用VC写控制台程序的几个小疑问?
只看楼主 加入收藏
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
_finddata_t 列出清单是這個嗎?

不要選我當版主
2012-02-16 23:16
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
http://topic.
找到了~明天去试试~
多谢了~

不要選我當版主
2012-02-16 23:18
C_printf
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:102
专家分:122
注 册:2010-1-26
收藏
得分:0 
说白了,撷取路径和档名就是字符串解析,可以自己封装函数。
相似查找算法很多啊,你是指定文件名找相似的,还是不指定。。。。。。。
2012-02-17 09:58
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
举例来说~

类似C:\ AAA\11111_22222_01_123456.txt~
要找出C:\ AAA\底下所有11111_22222_01_??????.txt档案~
01有时是02或03...等~01可能有N个档~02可能有M个档~各自分批处理~

并依照??????排先后顺序~
再做批次处理处理内容又是另一道复杂程序~
源代码已经有了~但是现在要加功能~而且以前人写的太(基本)了~
是能用但是代码很长~还有BUG~想改短些~

[ 本帖最后由 wube 于 2012-2-17 13:34 编辑 ]

不要選我當版主
2012-02-17 13:30
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
早些天貼過的,重貼一次給你參考,你應該可以從中得到借鑒,不多解釋了。

程序代码:
//--------------------------------------
// 程序功能:調用Win32功能用通配符查找一批文件的文件名
//     說明:1.採用DOS通配符,即包含*和?號的文件,比如*.c是所有以.c為擴展名的文件,可帶路徑
//           2.用命令行指定文件樣式,若省略,默認為*.*
//           3.默認將結果輸出到控制臺,若用重定向,可輸出到文本文件,語法類如為test *.c > list.txt,即寫到list.txt上
//           4.獲得文件清單,可用數組循環處理(本例是寫到標準容器vector中,相當於文件名數組)
//           5.要獲取文件的其他屬性,可查閱MSDN中WIN32_FIND_DATA結構的內容
//           6.本程序使用寬字符以及安全版本的函數,然而用重定向輸出得到的文本文件是可以用Ansi模式讀入的
//--------------------------------------

#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <vector>

BOOL GetFileList(const wchar_t FileName[], std::vector<WIN32_FIND_DATAW>& FileList);

int wmain(int argc, wchar_t* argv[])
{
    wchar_t FileName[FILENAME_MAX];
    wcscpy_s(FileName, FILENAME_MAX - 1, (argc < 2) ? L"*.*" : argv[1]);

    std::vector<WIN32_FIND_DATAW> FileList;
    FileList.clear();                        // 清空集合,如果需要追加模式,重覆調用GetFileList()函數而不要再清空
    if (GetFileList(FileName, FileList))
    {
        for (std::vector<std::wstring>::size_type index = 0; index != FileList.size(); ++index)
        {
            _putws(FileList[index].cFileName);
        }
    }
    return 0;
}

// 獲取文件清單
// 備註:結果通過集合FileList返回,傳遞的是引用
BOOL GetFileList(const wchar_t FileName[], std::vector<WIN32_FIND_DATAW>& FileList)
{
    WIN32_FIND_DATAW FindFileData;            // 文件數據結構,類型聲明末尾為W表示是寬字符版本,若為A則是Ansi版本的
    HANDLE Handle;                            // 用於搜索文件的句柄,將此句柄供給FindNextFile()函數,文件信息儲存在上面結構中

    Handle = FindFirstFileW(FileName, &FindFileData);
    if (Handle != INVALID_HANDLE_VALUE)
    {
        do
        {
            FileList.push_back(FindFileData);
        } while (FindNextFileW(Handle, &FindFileData));
        return true;
    }
    else return false;
}

授人以渔,不授人以鱼。
2012-02-17 13:34
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
以下是引用TonyDeng在2012-2-17 13:34:21的发言:

早些天貼過的,重貼一次給你參考,你應該可以從中得到借鑒,不多解釋了。

 
//--------------------------------------
// 程序功能:調用Win32功能用通配符查找一批文件的文件名
//     說明:1.採用DOS通配符,即包含*和?號的文件,比如*.c是所有以.c為擴展名的文件,可帶路徑
//           2.用命令行指定文件樣式,若省略,默認為*.*
//           3.默認將結果輸出到控制臺,若用重定向,可輸出到文本文件,語法類如為test *.c > list.txt,即寫到list.txt上
//           4.獲得文件清單,可用數組循環處理(本例是寫到標準容器vector中,相當於文件名數組)
//           5.要獲取文件的其他屬性,可查閱MSDN中WIN32_FIND_DATA結構的內容
//           6.本程序使用寬字符以及安全版本的函數,然而用重定向輸出得到的文本文件是可以用Ansi模式讀入的
//--------------------------------------
 
#include  
#include  
#include  
#include  
 
BOOL GetFileList(const wchar_t FileName[], std::vector& FileList);
 
int wmain(int argc, wchar_t* argv[])
{
    wchar_t FileName[FILENAME_MAX];
    wcscpy_s(FileName, FILENAME_MAX - 1, (argc < 2) ? L"*.*" : argv[1]);
 
    std::vector FileList;
    FileList.clear();                        // 清空集合,如果需要追加模式,重覆調用GetFileList()函數而不要再清空
    if (GetFileList(FileName, FileList))
    {
        for (std::vector::size_type index = 0; index != FileList.size(); ++index)
        {
            _putws(FileList.cFileName);
        }
    }
    return 0;
}
 
// 獲取文件清單
// 備註:結果通過集合FileList返回,傳遞的是引用
BOOL GetFileList(const wchar_t FileName[], std::vector& FileList)
{
    WIN32_FIND_DATAW FindFileData;            // 文件數據結構,類型聲明末尾為W表示是寬字符版本,若為A則是Ansi版本的
    HANDLE Handle;                            // 用於搜索文件的句柄,將此句柄供給FindNextFile()函數,文件信息儲存在上面結構中
 
    Handle = FindFirstFileW(FileName, &FindFileData);
    if (Handle != INVALID_HANDLE_VALUE)
    {
        do
        {
            FileList.push_back(FindFileData);
        } while (FindNextFileW(Handle, &FindFileData));
        return true;
    }
    else return false;
}
好啊,学习了。

梅尚程荀
马谭杨奚







                                                       
2012-02-17 13:38
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
有些东西跟VB好像~
程序代码:
Option Explicit

Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MaxLFNPath = 260
Const INVALID_HANDLE_VALUE = -1

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MaxLFNPath
    cShortFileName As String * 14
End Type

Dim WFD As WIN32_FIND_DATA
Dim bgndir$, curpath$, schpattern$, aa$, fname$, progdisk$
Dim hItem&, hFile&, rtn&, i%, j%, k%, tfiles&, tfsize#, stopyn As Boolean 'Boolean 数据类型 (Visual Basic)存放只可能为 True 或 False 的值
Dim X1&, buff$ 'Dim x1& 是Dim x1 As Long“长整型”& 是 As Long的缩写,! 是 as single 的缩写,例如:dim x0!,x1!,t!(或:dim x0 as single,x1 as single,t as single)

Private Sub cmdBower_Click()
Dim Path As String

    Path = BrowseForFolder(Me.hwnd, "Select Project's Location :", , NEWFOLDER)
    If (Trim(Path) <> "") Then
        txtTargetPath.Text = IIf(Right(Trim(Path), 1) <> "\", Path & "\", Path)
        txtTargetPath.ToolTipText = txtTargetPath.Text
        cmdSearch.Enabled = True
        SelectTargetPath = Path
    End If
    
End Sub

Private Sub CmdExit_Click()
    Call WriteKTPList
    Unload A_frmSearchKTP
    A_MainForm.Show
End Sub

Private Sub WriteKTPList()
Dim FileName As String, TempString As String
Dim i As Integer, j As Integer, FileNum As Integer
    
    FileName = App.Path & "\" & "CSV"
    If IsFolderExist(FileName) = False Then MkDir FileName
    FileName = FileName & "\KTPList.ini"
    FileNum = FreeFile
    If List1.List(0) <> "" Then
        j = 1
        Open FileName For Output As #FileNum
            For i = 0 To List1.ListCount
                If List1.List(i) <> "" Then
                    Print #FileNum, j & "=" & List1.List(i)
                    j = j + 1
                End If
            Next i
        Close #FileNum
    End If
End Sub

Private Sub cmdSearch_Click()
Dim s As String

On Error Resume Next
    
    List1.Clear '清空list1里面的内容
    tfiles = 0: tfsize = 0 '初始化统计文件数为0,文件大小为0,其中冒号是将两个语句分隔开
    stopyn = False 'stopyn估计是按钮的停止属性跟cancel差不多吧
    
    CmdExit.Enabled = Not CmdExit.Enabled
    cmdBower.Enabled = Not cmdBower.Enabled
    cmdSearch.Enabled = Not cmdSearch.Enabled
    cmdSTOP.Enabled = Not cmdSTOP.Enabled
    Text1.Locked = Not Text1.Locked
    Text2.Locked = Not Text2.Locked
    
    If InStr(Text1.Text, ".") = 0 Then Text1.Text = Trim(Text1.Text) & "*.*"    '在text1中查找"."如果"."是第一个则....

    s = Trim(txtTargetPath.Text)
    
    bgndir = s '开始搜的文件夹
    If InStr(bgndir, ":") = 0 And Len(bgndir) = 1 Then bgndir = bgndir & ":"
    If Right(bgndir, 1) <> "\" Then bgndir = bgndir & "\"
    schpattern = Trim(Text1.Text) '模糊搜索条件,例如 *.* 或 *.mp3 或 sc*.*
    Call SearchDirs(bgndir)
    
    If tfiles > 0 Then
        MsgBox "搜索完成,共查找到" & str(tfiles) & " 个文件" & vbCrLf & Chr(10) & "总占空间: " & Format(str(tfsize), "#,###") & " Bytes"
    Else
        MsgBox "搜索完成,未找到符合的文件"
    End If
    
    cmdBower.Enabled = Not cmdBower.Enabled
    cmdSearch.Enabled = Not cmdSearch.Enabled
    cmdSTOP.Enabled = Not cmdSTOP.Enabled
    CmdExit.Enabled = Not CmdExit.Enabled
    Text1.Locked = Not Text1.Locked
    Text2.Locked = Not Text2.Locked
    
    Me.Caption = "快速搜索文件"
    
End Sub

Private Sub cmdSTOP_Click()
    stopyn = True
End Sub

Private Sub Form_Load()
    Call init '窗体加载时,首先调用init配置过程
End Sub

Private Sub SearchDirs(curpath)
Dim dirs%, dircount%, dirbuf$()
Dim FilterName As String

On Error Resume Next
    
    Me.Caption = "正在查找 " & curpath
    DoEvents
    hItem = FindFirstFile(curpath & "*", WFD)
    
    If hItem <> INVALID_HANDLE_VALUE Then
        Do
            DoEvents
            If stopyn Then Exit Do
            FilterName = IIf(Trim(Text2.Text) <> "", Trim(Text2.Text), "")
            If FilterName <> "" Then
                If InStr(WFD.cFileName, FilterName) = 0 Then
                    If (WFD.dwFileAttributes And vbDirectory) And Asc(WFD.cFileName) <> 46 Then
                    If (dirs Mod 10) = 0 Then ReDim Preserve dirbuf(dirs + 10)
                    dirs = dirs + 1
                    dirbuf(dirs) = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
                    End If
                End If
            Else
                If (WFD.dwFileAttributes And vbDirectory) And Asc(WFD.cFileName) <> 46 Then
                    If (dirs Mod 10) = 0 Then ReDim Preserve dirbuf(dirs + 10)
                    dirs = dirs + 1
                    dirbuf(dirs) = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
                End If
            End If
        Loop While FindNextFile(hItem, WFD)
        
        Call FindClose(hItem)
        Call mohusearch(curpath)
    
    End If
    
    For dircount = 1 To dirs
        DoEvents
        If stopyn Then Exit For
        SearchDirs curpath & dirbuf$(dircount) & "\"
    Next dircount
    
End Sub

Private Sub mohusearch(curpath)

On Error Resume Next

    hFile = FindFirstFile(curpath & schpattern, WFD)
    
    If hFile <> INVALID_HANDLE_VALUE Then
    
        Do
            DoEvents
            If stopyn Then Exit Do
            aa = Trim(Trim(curpath) & Trim(WFD.cFileName))
            If (WFD.dwFileAttributes And vbDirectory) Or Asc(WFD.cFileName) = 46 Then
                
            Else
                k = InStr(aa, Chr(0))
                If k > 0 Then
                    fname = Mid(aa, 1, k - 1)
                    aa = fname '& " ----- " & Format(str(FileLen(fname)), "#,###") & " Bytes"
                    tfiles = tfiles + 1
                    tfsize = tfsize + FileLen(fname)
                    List1.AddItem aa
                    List1.Selected(List1.ListCount - 1) = True
                End If
            End If
        Loop While FindNextFile(hFile, WFD)
        
        Call FindClose(hFile)
        
    End If
    
End Sub

Private Sub List1_dblClick()
    If List1.ListCount > 0 Then
        j = List1.ListIndex
        fname = Trim(List1.List(j))
        j = InStr(fname, "-----")
        If j > 0 Then
            fname = Trim(Mid(fname, 1, j - 1))
            Shell "explorer " & fname, vbNormalNoFocus
        End If
    End If
End Sub

Private Sub init() '配置窗体加载
    Text1.Text = "*.KTP"
    'cmdSearch.Enabled = False
    cmdSTOP.Enabled = False
End Sub

不要選我當版主
2012-02-17 13:40
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
所有.NET語言在底層都是一樣的,共用同一套數據結構和API函數,所以看得到很明顯的共同點。

授人以渔,不授人以鱼。
2012-02-17 13:46
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
http://topic.
这只能2005下编译才会过~果然是参考~

不要選我當版主
2012-02-17 14:19
快速回复:关于用VC写控制台程序的几个小疑问?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019212 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved