标题:run 命令运行7zr.exe 没有正常创建压缩文件
只看楼主
ImKani
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-3-6
结帖率:0
已结贴  问题点数:15 回复次数:5 
run 命令运行7zr.exe 没有正常创建压缩文件
我使用了以下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"

该命令能正确创建压缩文件
想请教各位大佬
搜索更多相关主题的帖子: 命令 文件 备份 运行 exe 
2023-04-18 23:58
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:5 
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 
2023-04-19 09:48
schtg
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:https://t.me/pump_upp
等 级:贵宾
威 望:67
帖 子:1355
专家分:2534
注 册:2012-2-29
得分:5 
回复 2楼 吹水佬
谢谢!
2023-04-19 11:11
nbwww
Rank: 6Rank: 6
等 级:贵宾
威 望:10
帖 子:222
专家分:468
注 册:2021-1-9
得分:5 
回复 2楼 吹水佬
吹版出手  非同凡响
2023-04-19 14:21
ImKani
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-3-6
得分:0 
回复 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

使用这段代码可以正常的创建文件
2023-05-02 14:10
zhken
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:11
帖 子:273
专家分:594
注 册:2012-9-21
得分:0 
回复 2楼 吹水佬
2023-05-03 00:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511962-1-1.html




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

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