windowsbatch-filecopywmicgetfileversion

Copy file and append the file version to its name


I want to create a batch file which copies the original file in the same directory, and creates a new file, but the new file that has been created have a name of the original file + file version.

I tried different scripts, but can't find the solution.

I tried this:

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

Solution

  • Here's a batch file that will copy a given .exe with the version number appended to the filename.

    @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"
    

    Usage examples

    Copy .exe file to same folder as the .exe file is in (omitting --pathtodest implies this):

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

    Copy the .exe file to a different folder:

    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 copy to a different destination folder than the .exe file is in. This will echo out the COPY command rather than executing it:

    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