注册 登录
编程论坛 VFP论坛

run 命令运行7zr.exe 没有正常创建压缩文件

ImKani 发布于 2023-04-18 23:58, 201 次点击
我使用了以下vfp代码,但是运行后并没有如预期的创建压缩文件
程序代码:
LOCAL lcItemId, lcItem, lcFolderSrc, lcFileBak, lcFolderTgr, lcConfirm, lc7zCmd, lcArchiveName, lcFiles
lcItemId = ALLTRIM(bak_set.item_id)
SELECT bak_set
LOCATE FOR item_id = lcItemId
*MESSAGEBOX(lcItemId)
IF FOUND()
   
    lcItem = ALLTRIM(bak_set.item)
    lcFolderSrc = ADDBS(ALLTRIM(bak_set.folder_src))
    lcFileBak = ALLTRIM(bak_set.file_bak)
    lcFolderTgr = ADDBS(ALLTRIM(bak_set.folder_tgr))
   
    lcConfirm = MESSAGEBOX("是否确认备份 " + lcItem + "?", 36, "确认备份")
    IF lcConfirm = 6
        *通过命令行格式化时间
        lcTime = '$(Get-Date -Format "yyyyMMdd-HHmmss")'
        lcArchiveName = lcFolderTgr + lcItem + '-' + lcTime + '.7z'
        * 获取备份文件列表
        LOCAL ARRAY laFiles[1]
        ALINES(laFiles, lcFileBak, ';')
        lcFiles = ""
        FOR EACH lcFile IN laFiles
            lcFiles = lcFiles + ' "' + lcFolderSrc + ALLTRIM(lcFile) + '"'
        ENDFOR
        
        *命令
        lc7zCmd = '.\7zr.exe a -t7z ' + lcArchiveName + lcFiles
        = STRTOFILE(lc7zCmd,'temp.txt')

        RUN &lc7zCmd
        
        MESSAGEBOX("备份完成: " + lcItem, 64, "备份完成")
    ENDIF
ELSE
    MESSAGEBOX("未找到指定的记录。", 48, "错误")
ENDIF

代码是参考了 论坛里@liuxingang28 大佬的备份程序的代码,这里改为运行7zr.exe
运行后temp.txt中
.\7zr.exe a -t7z D:\DOCUMENT\待办-紧急\系统设计\BAK\BACKUP\测试测试-$(Get-Date -Format "yyyyMMdd-HHmmss").7z "D:\DOCUMENT\待办-紧急\系统设计\DATA\TEST.DBF" "D:\DOCUMENT\待办-紧急\系统设计\DATA\USER.DBF"

该命令能正确创建压缩文件
想请教各位大佬
5 回复
#2
吹水佬2023-04-19 09:48
windows 8 之后就有个 Compression API
参考:https://docs.
示例:
程序代码:
DECLARE long CreateCompressor   IN cabinet long,string@,long@
DECLARE long CreateDecompressor IN cabinet long,string@,long@
DECLARE long CloseCompressor    IN cabinet long
DECLARE long CloseDecompressor  IN cabinet long
DECLARE long Compress           IN cabinet long,string@,long,string@,long,long@
DECLARE long Decompress         IN cabinet long,string@,long,string@,long,long@

inFile =  "D:\TEMP\tmp.txt"
outFile = "D:\TEMP\tmp.mszip"
defile  = "D:\TEMP\tmpDe.txt"
? myCompressor(inFile,  outFile, 0) && 压缩
? myCompressor(outFile, deFile,  1) && 解压缩
RETURN

FUNCTION myCompressor(inFile, outFile, nFlag)
    IF !BETWEEN(nFlag,0,1)
        RETURN .f.
    ENDIF
    LOCAL Handle, cData, inBuf, inSize, outBuf, outSize, nSize, ret
    Handle = 0
    ret = IIF(nFlag==0, CreateCompressor(2,NULL,@Handle), CreateDecompressor(2,NULL,@Handle))
    IF ret != 0
        inBuf   = FILETOSTR(inFile)
        inSize  = LEN(inBuf)
        outSize = 0
        IIF(nFlag==0, Compress(Handle,@inBuf,inSize,0,0,@outSize), Decompress(Handle,@inBuf,inSize,0,0,@outSize))
        outBuf = REPLICATE(0h00,outSize)  
        nSize  = 0
        ret = IIF(nFlag==0, Compress(Handle,@inBuf,inSize,@outBuf,outSize,@nSize), Decompress(Handle,@inBuf,inSize,@outBuf,outSize,@nSize))  
        IF ret != 0
            STRTOFILE(LEFT(outBuf,nSize), outFile)
        ENDIF  
        IIF(nFlag==0, CloseCompressor(Handle), CloseDecompressor(Handle))  
    ENDIF   
    RETURN ret != 0
ENDFUNC
#3
schtg2023-04-19 11:11
回复 2楼 吹水佬
谢谢!
#4
nbwww2023-04-19 14:21
回复 2楼 吹水佬
吹版出手  非同凡响
#5
ImKani2023-05-02 14:10
回复 2楼 吹水佬
很感谢您的回答,因为考虑win7等系统,阁下的方案不太适合我,我找到了以下解决方案:
在VFP代码中创建一个批处理文件(例如:runPowerShellCommand.bat),然后将PowerShell命令写入该文件。接下来,在VFP中运行批处理文件:
程序代码:
TEXT TO lcCmdText NOSHOW
@echo off
PowerShell -Command "&{<<lc7zCmd>>}"
ENDTEXT

lcCmdText = STRTRAN(lcCmdText, "<<lc7zCmd>>", lc7zCmd)
STRTOFILE(lcCmdText, "runPowerShellCommand.bat")
RUN runPowerShellCommand.bat
ERASE runPowerShellCommand.bat

使用这段代码可以正常的创建文件
只有本站会员才能查看附件,请 登录
#6
zhken2023-05-03 00:06
回复 2楼 吹水佬
1