windows 复制文件并将文件版本附加到其名称

r6vfmomb  于 2023-08-07  发布在  Windows
关注(0)|答案(1)|浏览(146)

我想创建一个批处理文件,它复制同一目录中的原始文件,并创建一个新文件,但已创建的新文件的名称为原始文件+文件版本。
我尝试了不同的脚本,但找不到解决方案。
我试过这个:

set /p "testVar"="wmic datafile where name="C:\\Users\\user\\Desktop\\Test\\GCM.exe" get Version /value"

copy C:\Users\user\Desktop\Test\GCM.exe C:\Users\user\Desktop\Test\GSM-%testVar%
pause

字符串

bkhjykvo

bkhjykvo1#

下面是一个批处理文件,它将复制一个给定的.exe,并在文件名后附加版本号。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM This checks whether the user tried to invoke the Help text, or didn't provide the required parameters so needs some help
IF "%~1"=="/?" (GOTO HELP
) ELSE IF "%~1"=="--help" ( GOTO HELP 
) ELSE IF "%~1"=="" (GOTO HELP)

SET "prev="
SET "current="
FOR %%A IN (%*) DO (
    SET "current=%%~A"
    IF DEFINED prev (
        SET "!prev!=%%~A"
        SET "prev="
    ) ELSE (
        SET prev=%%A
    )
)

REM Catch any erroneously-inputted options
FOR /F "tokens=1 delims==" %%B IN ('2^>nul SET --') DO ( 
    IF "%%~B"=="--pathtoexe" ( REM
    ) ELSE IF "%%~B"=="--pathtodest" ( REM
    ) ELSE IF "%%~B"=="--simulate" ( SET "--simulate=ECHO"
    ) ELSE ( GOTO INVALID_ARGS )
)

IF NOT EXIST "%--pathtoexe%" (
    ECHO Exe file does not exist!
    EXIT /B 1
) ELSE (
    FOR %%E IN ("%--pathtoexe%") DO SET "exefilename=%%~nxE"
)

IF NOT DEFINED --pathtodest (
    FOR %%F IN ("%--pathtoexe%") DO SET "--pathtodest=%%~dpF"
)

IF NOT EXIST "%--pathtodest%" (
    ECHO Destination path does not exist!
    EXIT /B 1
)

SET "--pathtoexe=%--pathtoexe:\=\\%"
FOR /F "usebackq delims=^= tokens=2" %%G IN (`WMIC DATAFILE WHERE "NAME='%--pathtoexe%'" GET VERSION /VALUE ^| FINDSTR Version`) DO SET "exeversion=%%G"
%--simulate% COPY /V "%--pathtoexe%" "%--pathtodest%\%exefilename%-%exeversion%"
EXIT /B 0

:INVALID_ARGS
ECHO Argument(s) not valid!

:HELP
ECHO.
ECHO Usage: %~nx0 [options...] 
ECHO --pathtoexe=^<full-quoted-path-to-exe^>
ECHO [--pathtodest=^<full-quoted-folder-path^>] If this option is not specified, this will be set to the same folder path as --pathtoexe
ECHO [--simulate=true] Show what would have been copied
ECHO Example: %~nx0 --pathtoexe="C:\Program Files\WindowsApps\Microsoft.BingWeather_4.53.51922.0_x64__8wekyb3d8bbwe\Microsoft.Msn.Weather.exe" --pathtodest="C:\Windows\Temp"

字符串

使用示例

.exe文件复制到.exe文件所在的文件夹中(省略--pathtodest意味着这一点):

copy-file-plus-version.cmd --pathtoexe="C:\Program Files\WindowsApps\Microsoft.BingWeather_4.53.51922.0_x64__8wekyb3d8bbwe\Microsoft.Msn.Weather.exe"


.exe文件复制到其他文件夹:

copy-file-plus-version.cmd --pathtoexe="C:\Program Files\WindowsApps\Microsoft.BingWeather_4.53.51922.0_x64__8wekyb3d8bbwe\Microsoft.Msn.Weather.exe" --pathtodest="D:\Temp"


模拟复制到与.exe文件所在的目标文件夹不同的目标文件夹。这将回显COPY命令,而不是执行它:

copy-file-plus-version.cmd --pathtoexe="C:\Program Files\WindowsApps\Microsoft.BingWeather_4.53.51922.0_x64__8wekyb3d8bbwe\Microsoft.Msn.Weather.exe" --pathtodest="D:\Temp" --simulate=true

相关问题