VB 批量重命名文件

VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Rename use VB QQ 1009374598"
   ClientHeight    =   3630
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   9270
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3630
   ScaleWidth      =   9270
   ShowInTaskbar   =   0   'False
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1 
      Caption         =   "Go"
      Height          =   495
      Left            =   3600
      TabIndex        =   6
      Top             =   2400
      Width           =   1695
   End
   Begin VB.TextBox txtPreFix 
      Height          =   405
      Left            =   1680
      TabIndex        =   4
      Text            =   "Pic_"
      Top             =   1440
      Width           =   1215
   End
   Begin VB.TextBox txtDest 
      Height          =   375
      Left            =   1680
      TabIndex        =   3
      Top             =   840
      Width           =   6855
   End
   Begin VB.TextBox txtSource 
      Height          =   375
      Left            =   1680
      TabIndex        =   1
      Top             =   240
      Width           =   6855
   End
   Begin VB.Label Label2 
      Caption         =   "PreFix:"
      Height          =   375
      Left            =   360
      TabIndex        =   5
      Top             =   1440
      Width           =   1095
   End
   Begin VB.Label lbDest 
      Caption         =   "Dest Folder:"
      Height          =   375
      Left            =   240
      TabIndex        =   2
      Top             =   840
      Width           =   1215
   End
   Begin VB.Label Label1 
      Caption         =   "Source Folder"
      Height          =   255
      Left            =   240
      TabIndex        =   0
      Top             =   240
      Width           =   1335
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Dim configFile As String

'读写INI例子:
Sub RWConfigFile()
    '读字符串
    Dim lng As Long
    Dim retstr As String
    retstr = String(260, 0)
    lng = GetPrivateProfileString("config", "para1", "", retstr, 256, "c:\config.ini")
    retstr = Replace(retstr, Chr(0), "")
   
    '读整数
    lng = GetPrivateProfileInt("config", "para2", 0, "c:\config.ini")
   
    '写字符串
    lng = WritePrivateProfileString("config", "para3", "写文件测试", "c:\config.ini")
End Sub


Private Sub Form_Load()

configFile = App.Path & "\config.ini"
loadConfig

End Sub

Sub loadConfig()

    Dim lng As Long
    Dim retstr As String
    retstr = String(260, 0)
    lng = GetPrivateProfileString("config", "SourceFolder", "", retstr, 256, configFile)
    retstr = Replace(retstr, Chr(0), "")
    txtSource.Text = retstr
    
    retstr = String(260, 0)
    lng = GetPrivateProfileString("config", "DestFolder", "", retstr, 256, configFile)
    retstr = Replace(retstr, Chr(0), "")
    txtDest.Text = retstr
    
     retstr = String(260, 0)
    lng = GetPrivateProfileString("config", "PreFix", "", retstr, 256, configFile)
    retstr = Replace(retstr, Chr(0), "")
    txtPreFix.Text = retstr

End Sub

Sub saveConfig()

    Dim lng As Long

    lng = WritePrivateProfileString("config", "SourceFolder", txtSource.Text, configFile)
    
    lng = WritePrivateProfileString("config", "DestFolder", txtDest.Text, configFile)
    
    lng = WritePrivateProfileString("config", "PreFix", txtPreFix.Text, configFile)

End Sub









Private Sub Command1_Click()
 
    Dim files, names As String, i As Integer
    Dim destFolder As String, sourceFolder As String
    Dim ext As String
    Dim preFix As String
    On Error GoTo err
    destFolder = txtDest.Text                              ' "C:\Documents and Settings\XPMUser\My Documents\My Pictures\avarta-80\OK\"
    sourceFolder = txtSource.Text                          ' "C:\Documents and Settings\XPMUser\My Documents\My Pictures\avarta-80\"
    preFix = txtPreFix.Text
 
    If Dir(sourceFolder, vbDirectory) = "" Then
        MsgBox "Source folder not exists"
        Exit Sub
    End If
     
    If Dir(destFolder, vbDirectory) = "" Then
        MkDir (destFolder)
    End If
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    files = Dir(sourceFolder)
    Do While files <> ""
        i = i + 1
        names = files
        'If LCase(Right(names, 4)) = ".jpg" Then
        ext = Right(names, 4)
        'Call FileCopy(sourceFolder & names, destFolder & " Pic_" & i & ".jpg")
        Call FileCopy(sourceFolder & names, destFolder & "\" & preFix & i & ext)
        ' End If
        files = Dir
    Loop
 
    MsgBox "done " & i
 
    Exit Sub
 
err:
    MsgBox err.Description
 
 
End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
saveConfig
End Sub

  

转载于:https://www.cnblogs.com/wgscd/p/10255993.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB文件加密、合并、批量重命名等小工具源程序的设计思路如下: 1. 加密:使用VB的加密算法对指定的文件进行加密。这可以通过读取文件内容,对内容进行加密算法处理,然后将加密后的内容写入到新文件中实现。 2. 合并:将多个指定的文件合并成一个文件。可以通过打开每个文件,读取内容并写入到新文件中,实现文件合并功能。 3. 批量重命名:选择一个指定的目录,读取目录中的文件列表,并提供用户界面以输入新的文件名规则。通过遍历文件列表,根据用户输入的规则进行文件重命名。 以上是基本的设计思路,下面是源程序的示例代码: 加密功能: ``` Sub EncryptFile(sourceFile As String, targetFile As String) ' 读取源文件内容 Dim stream As IO.StreamReader = IO.File.OpenText(sourceFile) Dim content As String content = stream.ReadToEnd() stream.Close() ' 对内容进行加密处理 Dim encryptedContent As String encryptedContent = EncryptAlgorithm(content) ' 将加密后的内容写入到目标文件 Dim writer As IO.StreamWriter = IO.File.CreateText(targetFile) writer.Write(encryptedContent) writer.Close() ' 删除源文件 IO.File.Delete(sourceFile) End Sub Function EncryptAlgorithm(content As String) As String ' 对内容进行加密处理的算法代码 ' ... End Function ``` 合并功能: ``` Sub MergeFiles(files() As String, targetFile As String) ' 将多个文件内容合并到一个目标文件中 Dim writer As IO.StreamWriter = IO.File.CreateText(targetFile) For Each file In files '读取源文件内容 Dim stream As IO.StreamReader = IO.File.OpenText(file) Dim content As String content = stream.ReadToEnd() stream.Close() ' 写入到目标文件 writer.Write(content) Next writer.Close() End Sub ``` 批量重命名功能: ``` Sub RenameFiles(directory As String, newNamePattern As String) Dim files As String() = IO.Directory.GetFiles(directory) For Each file In files ' 获取文件名和扩展名 Dim fileName As String = IO.Path.GetFileNameWithoutExtension(file) Dim extension As String = IO.Path.GetExtension(file) ' 构建新的文件名 Dim newFileName As String = newNamePattern.Replace("{fileName}", fileName) newFileName = newFileName.Replace("{extension}", extension) ' 生成新的文件路径 Dim newFilePath As String = IO.Path.Combine(directory, newFileName) ' 重命名文件 IO.File.Move(file, newFilePath) Next End Sub ``` 以上是VB文件加密、合并、批量重命名等小工具源程序的设计示例,根据具体需求可以进行适当修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值