Finalmente, depois de tentar muitas maneiras diferentes, consegui esse lote usando o procedimento.
Funciona como esperado
@ECHO OFF
ECHO %~dp0
ECHO %~dpnx0
SET fileNameForWMIC=
SET "ZAEXE=7za.exe"
SET ZAEXE=%~dp0%ZAEXE%
IF NOT EXIST %ZAEXE% (
ECHO 7za.exe is missing please fix it and restart
GOTO :END
)
SET DEST="C:\Program Files (x86)\My Prog\PFM"
IF /I "%1"=="/revert" GOTO :REVERT
SET "IMPFILE=verPFM.txt"
SET IMPFILE=%~dp0%IMPFILE%
IF NOT EXIST %IMPFILE% (
ECHO verPFM.txt is missing please fix it and restart
GOTO :END
)
SET "REFFILE=refPFM.7z"
SET REFFILE=%~dp0%REFFILE%
IF NOT EXIST %REFFILE% (
ECHO refPFM.7z is missing please fix it and restart
GOTO :END
)
SET "SOURCE=C:\Program Files (x86)\My Prog\*.*"
SET "TARGET=PFM_ALL.7z"
SET TARGET=%~dp0%TARGET%
rem the whole directory
FOR /D %%X IN ("%SOURCE%") do %ZAEXE% a "%TARGET%" "%%X"
IF NOT EXIST %TARGET% (
ECHO PFM_ALL.7z was not created please report
GOTO :END
) ELSE ECHO A complete backup of %SOURCE% is in %TARGET%
rem change target file
SET "TARGET=PFM.7z"
SET TARGET=%~dp0%TARGET%
SET /A NbError=0
rem "!7ZAEXE!" a "!TARGET!" @"!IMPFILE!"
rem only the important files listed in text file
setlocal enabledelayedexpansion
FOR /F "usebackq skip=1 tokens=1,2 delims=," %%G IN ("%IMPFILE%") DO (
rem echo %%G version %%H
SET fileNameForWMIC=%%G
SET GoodVer=%%H
SET "fileNameForWMIC=!fileNameForWMIC:\=\!"
rem ECHO !fileNameForWMIC!
rem ECHO Goodver !GoodVer!
call :process "%%G" %%H
)
goto :next
:next
IF %NbError% EQU 0 (
ECHO All files was already upto date
GOTO :END
) ELSE (
IF NOT EXIST %TARGET% (
ECHO PFM.7z was not created please report
GOTO :END
) ELSE ( ECHO %NbError% files saved in %TARGET% )
)
:END
SET SOURCE=
SET TARGET=
SET fileNameForWMIC=
SET fileNameForW=
SET GoodV=
SET GoodVer=
SET NbError=
SET REFFILE=
SET DEST=
SET filename=
EXIT /B
:process
SET fileNameForW=%1
rem ECHO %fileNameForW%
SET GoodV=%2
SET fileNameForW=%fileNameForW:\=\%
rem ECHO %fileNameForW%
rem ECHO Goodv %GoodV%
SET fileNameForWW=%1
rem ECHO %fileNameForWW%
FOR /F "usebackq skip=1" %%I IN ('wmic datafile where Name^=%fileNameForW% get Version ^| findstr /r /v "^$"') DO (
SET "filever1=%%~I"
rem ECHO( !filever1!
)
IF /I NOT ["!filever1!"] == ["%GoodV%"] (
ECHO %fileNameForW% have version^=%filever1% instead of %GoodV%
"%ZAEXE%" a "%TARGET%" %fileNameForWW%
SET /A NbError+=1
rem ECHO %fileNameForWW%
call :getfilename %fileNameForWW%
"%ZAEXE%" e "%REFFILE%" -o%DEST% !filename! -aoa
GOTO :EOF
) ELSE (
GOTO :EOF
)
:getfilename
SET filename=%~nx1
rem echo The name of the file is %filename%
rem set folder=%~dp1
rem echo It's path is %folder%
GOTO :EOF
:REVERT
rem change target file
SET "TARGET=PFM.7z"
SET TARGET=%~dp0%TARGET%
IF NOT EXIST %TARGET% (
ECHO PFM.7z not found could not revert
GOTO :END
) ELSE (
"%ZAEXE%" e "%TARGET%" -o%DEST% * -aoa
GOTO :END
)
procedimento: END finalizar o lote limpando a variável primeiro
procedure: REVERT desfaz a alteração feita, é envolvida chamando o batch.cmd / revert
o IMPFILE é criado usando o mesmo comando wmic que o usado dentro para obter a versão do arquivo, com o / OUTPUT, então o arquivo é editado para remover o caractere blanc e convertido do UTF16 para ASCII (usando o notepad ++) se não for o arquivo não pode ser lido!