VideoHelp Forum

Try DVDFab and download streaming video, copy, convert or make Blu-rays,DVDs! Download free trial !
+ Reply to Thread
Results 1 to 8 of 8
Thread
  1. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Hope this is the right place for this since it is meant to be a sort of guide.

    Recently, I needed to obtain the 'Artist' Metadata from an MP3 so I could add this to the filename when I created an MP4 from an MP3 and a JPG image. In order to do this, I needed to use EXIFTOOL to obtain the Metadata and set this into a variable to be used as part of a batch file. I used the following script as a batch file.

    All mp3, batchfile.bat, ffmpeg, exiftool and you your image should be in the same folder or complete path names should be used and the quotes are very important for possible spaces in the file names or paths.

    Code:
    for %%a in ("*.mp3") do (
    exiftool.exe -T -artist  "%%a" > tmpfile.txt
    timeout 5
    set /p nb_artist=< tmpFile.txt
    "ffmpeg.exe" -loop 1 -i SCREEN.jpg -i "%%a" -s 640x480 -shortest -c:v libx264 -tune stillimage -c:a copy -movflags faststart "%nb_artist%  %%~na.mp4"
    )
    What this does is use exiftool to get the -Artist metadata and store it in a file called 'tmpfile.txt' then retrieve this and set as a variable called nb_artist. This is used to append the Artist name to the file name.

    The Timeout is necessary for DOS (CMD) to pause until the tmpfile.txt is written completely or it cannot be retrieved and set as a variable by the next step. It should be set long enough for your type of drive to completely write tmpfile.txt iscase you have slow drive like a USB drive. Timeout is included in Windows 7 and 8 Dos but not sure of Windows 10.

    The resize -s was used here only because my image was not a valid x264 size (divisble by 2). It's not necessary otherwise.

    This same obtaining information and then passing it to the rest of your batch file can be done with other multiple usage programs as well like obtaining Mediainfo information and using it in FFMpeg scripts.
    Quote Quote  
  2. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Latest news? I think the Guides section is better?

    We can keep/post all useful complex batch tips in this thread.
    Quote Quote  
  3. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    I'll add more here then since I have a ton of them like embedding Created date/Time in images, set Meta creation date into MP4, run DGIndex against an MPG, and more unusual types.

    Watch the quotes,single and double, around path names and variables containing path names!!!


    Creating copies of images with created date embedded in image:

    Code:
    for %%a in ("C:\Users\Bud\Desktop\imagefolder\*.jpg") do (
    IF NOT EXIST "%%~dpaprocessed" MD "%%~dpaprocessed"
    "c:\path\exiftool" -CreateDate -FileCreateDate "%%a"  > "tmpfile.txt"
    Timeout 1
    FOR /F "tokens=1, 2, 3, 4, 5, 6, 7-15* delims=-: " %%b in (tmpfile.txt) DO (
    "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='c\:\\Windows\\fonts\\Arial.ttf':fontsize=24:text='Created %%e\:%%f\:%%g %%h\:%%i\:%%j': x=(w-(tw*1.05)): y=0: fontcolor=white@0.5: box=1: boxcolor=0x00000000@0.5" -y "%%~dpa\processed\%%~na.png"
    )
    )
    Last edited by Budman1; 30th Dec 2015 at 16:03. Reason: Added Timeout for file write time/added direct path to Windows 7 Arial Font
    Quote Quote  
  4. This could get handy also, it tmpFile.txt does not exist or if tmpFile.txt is in use it waits 1 second. Advantage is, if there is everything all right no waiting needed ...
    Code:
    :check
    if not exist  "tmpFile.txt"  timeout 1& goto :check
    ( type nul >> "tmpFile.txt" ) 2>nul || ( timeout 1& goto :check )
    this could be handy in other situations, especially that "if file is in use - do something"
    Quote Quote  
  5. Member
    Join Date
    Sep 2012
    Location
    Australia
    Search Comp PM
    If we're doing this:

    StringLength.bat for retrieving the length of a string, DigitCount.bat for retrieving info on whether a whole string is digits, whether the first character is a digit and how many characters are digits before a non-digit character occurs and PathExist.bat which retrieves whether a path exists and whether its a file or a directory.

    StringLength.bat
    Code:
    @if not defined EMode (Set "EMode=OFF" & Set "Pause_On=Y")
    @ECHO %EMode%
    
    :strlen  
    
    	SetLocal DisableDelayedExpansion
    		CALL set "Test_String=%%%~2:"=_%%#"
    		set "len=0"
    		for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do if defined Test_String CALL :strlen_loop %%P "%%Test_String:~%%P,1%%"
    	EndLocal & set "%~1=%len%" & if defined Pause_On echo The string is %len% characters long.
    
    goto :eof
    
    :strlen_loop
    
    	if %2=="" goto :eof
    	set /a "len+=%1"
    	CALL set "Test_String=%%Test_String:~%1%%"
    
    goto :eof
    DigitCount.bat
    Code:
    @if not defined EMode (Set "EMode=OFF" & Set "Pause_On=Y")
    @ECHO %EMode%
    
    :DIGIT_COUNT 
    	SetLocal DisableDelayedExpansion
    	if "%~1"=="" goto :eof
    	if not defined %~1 (
    		if Defined Pause_On (
    			echo The string is empty.
    			pause
    		)
    		EndLocal
    		Set "Digit_Count=0"
    		Set "String_Length=0"
    		Set "Is_Digit="
    		Set "Is_Number="
    		goto :eof
    	)
    	CALL Set "WORD=%%%~1:"=_%%"
    	CALL StringLength LEN WORD
    	Set "Digit_Count=0"
    	
    	for /l %%P in (0, 1, %LEN%) do (
    		Set "Is_Digit="
    		CALL Set "Test_Char=%%WORD:~%%~P, 1%%"
    		for /l %%Q in (0, 1, 9) do if NOT Defined Is_Digit CALL :DIGIT_COUNT_LOOP ^%%Q
    		if NOT Defined Is_Digit goto :DIGIT_COUNT_NEXT
    	)
    :DIGIT_COUNT_NEXT
    	if Defined Pause_On echo There are %Digit_Count% digits.
    	(
    		EndLocal
    		Set "Digit_Count=%Digit_Count%"
    		Set "String_Length=%LEN%"
    		if %Digit_Count% GTR 0 (
    			if defined Pause_On (
    				if %Digit_Count% GTR 1 (
    					if %Digit_Count%==%LEN% (
    						echo The whole string is a number.
    					) else (
    						echo The string starts with %Digit_Count% digits.
    					)
    				) else (
    					echo The character is a digit.
    				)
    			)
    			Set "Is_Digit=TRUE"
    			if %Digit_Count%==%LEN% (
    				Set "Is_Number=TRUE"
    			) else (
    				Set "Is_Number="
    			)
    		) else (
    			if Defined Pause_On echo The first character is NOT a digit.
    			Set "Is_Digit="
    			Set "Is_Number="
    		)
    	)
    goto :eof
    
    :DIGIT_COUNT_LOOP
    
    	if "%Test_Char%"=="%~1" (
    		Set "Is_Digit=1"
    		Set /a Digit_Count+=1
    	)
    	
    goto :eof
    PathExist.bat
    Code:
    @if not defined EMode (Set "EMode=OFF" & Set "Pause_On=Y")
    @ECHO %EMode%
    
    Set "DirExist="
    Set "FileExist="
    
    if NOT EXIST "%~f1" (
    	if NOT "%Pause_On%"=="" echo Path ^:%1^: Does Not Exist & pause
    	Set "PathExist="
    	Set "IsSymLink="
    	goto :eof
    )
    Set "PathExist=TRUE"
    
    Set "IsSymLink=%~a1"
    if "%IsSymLink:~8,1%"=="l" (
    	Set "IsSymLink=TRUE"
    	if defined Pause_On echo Path ^:%1^: Is A Sybolic Link
    ) else (
    	Set "IsSymLink="
    )
    
    type NUL
    pushd "%~f1" 1>NUL 2>&1
    if "%ERRORLEVEL%"=="0" (
    	popd
    	if defined Pause_On echo Path ^:%1^: Is A Folder & pause
    	Set "DirExist=TRUE"
    	goto :eof
    )
    if defined Pause_On echo Path ^:%1^: Is A File & pause
    Set "FileExist=TRUE"
    Last edited by ndjamena; 27th Dec 2016 at 10:44.
    Quote Quote  
  6. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    A few more newly added ones:

    Generate silent stereo audio:
    Code:
    ffmpeg -f lavfi -i "aevalsrc=0|0:d=5" output.wav
    Joining segments of different videos: (FLV CAN BE IMAGES CREATED WITH BLANK SOUND ABOVE)
    Code:
    a=directshowsource("C:\PathToVideo\img00001_IMG2VID_wAudio2.flv",pixel_type="rgb24").Spline36Resize(720,480).trim(1,120).ChangeFPS(30)
    b=directshowsource("C:\PathToVideo\img00003_IMG2VID_wAudio2.flv",pixel_type="rgb24").Spline36Resize(720,480).trim(1,120).ChangeFPS(30)
    c=directshowsource("C:\PathToVideo\clip.mp4",pixel_type="rgb24").lanczosResize(720,480).trim(1,120).ChangeFPS(30)
    d=directshowsource("C:\PathToVideo\img00005_IMG2VID_wAudio2.flv",pixel_type="rgb24").Spline36Resize(720,480).trim(1,120).ChangeFPS(30)
    aa = a
    ba = b
    ca = c
    da = d
    vid=aa+ba+ca+da
    newvid = vid.ChangeFPS(30)newvid

    Transitions betwen images: (CAN MIX IMAGES AND VIDEO)
    Code:
    loadplugin("c:\users\bud\desktop\transall.dll")
    a=imagereader("C:\Users\Bud\Desktop\img00001.jpg",pixel_type="rgb24").Spline36Resize(720,480).trim(1,120).ChangeFPS(30)
    b=imagereader("C:\Users\Bud\Desktop\img00003.jpg",pixel_type="rgb24").Spline36Resize(720,480).trim(1,120).ChangeFPS(30)
    c=directshowsource("C:\Users\Bud\Desktop\clip.mp4",audio=false,pixel_type="rgb24").lanczosResize(720,480).trim(1,120).ChangeFPS(30)
    d=imagereader("C:\Users\Bud\Desktop\img00005.jpg",pixel_type="rgb24").Spline36Resize(720,480).trim(1,120).ChangeFPS(30)
    aa = TransAccord(a,b,90,"hor",true,false)  
    ba = TransBubbles(b, c, 90)
    next=d
    time=-5
    ca = TransCrumple(c, next, 90,"fan",True)
    vid=aa+ba+ca
    newvid = vid.ChangeFPS(30)newvid
    Quote Quote  
  7. Member
    Join Date
    Sep 2012
    Location
    Australia
    Search Comp PM
    I got bored, so this is my new PathExist.bat

    It detects symbolic links, returns the path the link is pointing to, plus if it's pointing to yet another symbolic link or is in a symbolic link directory/directory junction it resolves those as well and returns the path to the ultimate target. AND it lets you set a prefix to the output variables so that if you need to check multiple paths you can do it without wiping all the previous values. If a symbolic link points to a path that doesn't exist or ultimately points back to itself the batch detects that an sets a brokenlink value. In the case of a directory symbolic link pointing to a file or vice versa the brokenlink value is set to MISMATCH.

    for a command line like:

    pathexist "C:\Users\User\Videos\TV Series (HD)\Eureka\Season 03\Eureka - 03x02 - What About Bob_.m4v" Source


    Ultimate output looks something like this:

    Code:
    C:\Users\User>set Source
    Source.Aliases="C:\Users\User\Videos\TV Series (HD)\Eureka\Season 03\Eureka - 03x02 - What About Bob_.m4v" "F:\Videos\TV Series\Science Fiction\Eureka\Eureka - 03x02 - What About Bob_.m4v" "F:\Videos\iTunes\TV Shows\Eureka\Season 3\02 What About Bob_ (1080p HD).m4v" "F:\Music\iTunes\iTunes Media\TV Shows\Eureka\Season 3\02 What About Bob_ (1080p HD).m4v"
    Source.FileDate=04/04/2015 10:33 AM
    Source.FileExist=TRUE
    Source.FileName=C:\Users\User\Videos\TV Series (HD)\Eureka\Season 03\Eureka - 03x02 - What About Bob_.m4v
    Source.FileSize=1906139281
    Source.IsSymLink=TRUE
    Source.PathExist=TRUE
    Source.PathLink=F:\Videos\TV Series\Science Fiction\Eureka\Eureka - 03x02 - What About Bob_.m4v
    Source.RootLink=F:\Music\iTunes\iTunes Media\TV Shows\Eureka\Season 3\02 What About Bob_ (1080p HD).m4v
    The missing unset values are Source.DirExist and Source.BrokenLink.

    Code:
    @if not defined EMode (Set "EMode=OFF" & Set "Pause_On=Y")
    @ECHO %EMode%
    
    SetLocal DisableDelayedExpansion
    
    :START
    
    	Set "PathExistSet=%~2"
    	if defined PathExistSet (
    		if "%PathExistSet:~-1%"=="." (
    			Set "PathExistSet=%PathExistSet%:"
    		) else (
    			Set "PathExistSet=%PathExistSet%.:"
    		)
    	) else (
    		Set "PathExistSet=:"
    	)
    
    	Set "PathExist="
    	Set "DirExist="
    	Set "FileExist="
    	Set "IsSymLink="
    	Set "FileName="
    	Set "PathLink="
    	Set "BrokenLink="
    	Set "RootLink="
    	Set "AliasCount=0"
    
    	if NOT EXIST "%~f1" (
    		if NOT "%Pause_On%"=="" echo Path ^:%1^: Does Not Exist
    		goto :END
    	)
    	Set "FileName=%~f1"
    	Set "PathExist=TRUE"
    
    	Set "Alias1=%~f1"
    	Set "AliasCount=1"
    	
    	Set "Root=%~nx1"
    	CALL :FindRoot "%~dp1"
    	Set "PathLink=%Root%"
    
    	Set "Check=%~a1"
    
    	if "%Check:~0,1%"=="d" (
    		if defined Pause_On echo Path ^:%1^: Is A Folder
    		Set "DirExist=TRUE"
    	) else (
    		if defined Pause_On echo Path ^:%1^: Is A File
    		Set "FileExist=TRUE"
    	)
    	
    	if NOT "%Check:~8,1%"=="l" goto :NotSymLink
    
    	Set "IsSymLink=TRUE"
    	
    :IsSymLink
    
    	CALL :GetPathLink "%PathLink%"
    	if NOT defined PathLink goto :SymLinkInfo
    	if NOT defined RootLink Set "RootLink=%PathLink%"
    	if NOT defined FirstBroken (
    		if defined BrokenLink (
    			Set "FirstBroken=%BrokenLink%"
    		) else (
    			Set "FirstBroken=:"
    		)
    	)
    	if "%FirstBroken%"==":" (
    		if defined BrokenLink (
    			Set "FirstBroken=%BrokenLink%"
    		) else (
    			Set "FirstBroken=:"
    		)
    	)
    	
    	Set "Check="
    	for /l %%i in (1,1,%AliasCount%) do CALL :CheckAlias "%%~i" "%PathLink%"
    	if defined Check Set "BrokenLink=TRUE"
    	
    	Set /a AliasCount+=1
    	Set "Alias%AliasCount%=%PathLink%"
    
    	for %%i in ("%PathLink%") do (
    		Set "Root=%%~nxi"
    		CALL :FindRoot "%%~dpi"
    	)
    	Set "PathLink=%Root%"
    	for %%i in ("%PathLink%") do Set "Check=%%~ai"
    	
    	if defined Check if "%Check:~8,1%:"=="l:" if not defined BrokenLink goto :IsSymLink
    
    :SymLinkInfo
    	if defined Pause_On (
    		echo Path ^:%1^: Is A Sybolic Link
    		if defined PathLink echo Target:  "%PathLink%"
    	)
    
    goto :end
    
    	
    :NotSymLink
    	
    	Set "IsSymLink="
    	Set "RootLink=%PathLink%"
    
    :END
    
    	if defined Pause_On if %AliasCount% GTR 0 (
    		echo Aliases:
    		for /l %%i in (1,1,%AliasCount%) do CALL echo Alias%%i:  "%%Alias%%i%%"
    	)
    	if Defined Alias1 Set Alias1="%Alias1%"
    	for /l %%i in (2,1,%AliasCount%) do call Set Alias1=%%Alias1%% "%%Alias%%i%%"
    	if NOT defined Alias1 Set "Alias1=:"
    	if NOT defined PathExist Set "PathExist=:"
    	if NOT defined DirExist Set "DirExist=:"
    	if NOT defined FileExist Set "FileExist=:"
    	if NOT defined IsSymLink Set "IsSymLink=:"
    	if NOT defined RootLink Set "RootLink=:"
    	if NOT defined PathLink Set "PathLink=:"
    	Set "FileSize=:"
    	Set "FileDate=:"
    	if NOT defined BrokenLink (
    		Set "BrokenLink=:"
    		if defined PathLink for %%i in ("%PathLink%") do (
    			Set "FileSize=%%~zi"
    			Set "FileDate=%%~ti"
    		)
    	)
    	(
    		endlocal
    		if "%PathExist%"==":" (
    			Set "%PathExistSet:~0,-1%PathExist="
    			Set "%PathExistSet:~0,-1%FileName="
    		) else (
    			Set "%PathExistSet:~0,-1%PathExist=%PathExist%"
    			Set "%PathExistSet:~0,-1%FileName=%FileName%"
    		)
    		if "%DirExist%"==":" (
    			Set "%PathExistSet:~0,-1%DirExist="
    		) else (
    			Set "%PathExistSet:~0,-1%DirExist=%DirExist%"
    		)
    		if "%FileExist%"==":" (
    			Set "%PathExistSet:~0,-1%FileExist="
    		) else (
    			Set "%PathExistSet:~0,-1%FileExist=%FileExist%"
    		)
    		if "%IsSymLink%"==":" (
    			Set "%PathExistSet:~0,-1%IsSymLink="
    		) else (
    			Set "%PathExistSet:~0,-1%IsSymLink=%IsSymLink%"
    		)
    		if "%PathLink%"==":" (
    			Set "%PathExistSet:~0,-1%RootLink="
    		) else (
    			Set "%PathExistSet:~0,-1%RootLink=%PathLink%"
    		)
    		if "%RootLink%"==":" (
    			Set "%PathExistSet:~0,-1%PathLink="
    		) else (
    			Set "%PathExistSet:~0,-1%PathLink=%RootLink%"
    		)
    		if "%FileSize%"==":" (
    			Set "%PathExistSet:~0,-1%FileSize="
    		) else (
    			Set "%PathExistSet:~0,-1%FileSize=%FileSize%"
    		)
    		if "%FileDate%"==":" (
    			Set "%PathExistSet:~0,-1%FileDate="
    		) else (
    			Set "%PathExistSet:~0,-1%FileDate=%FileDate%"
    		)
    		if "%FirstBroken%"==":" (
    			Set "%PathExistSet:~0,-1%BrokenLink="
    		) else (
    			Set "%PathExistSet:~0,-1%BrokenLink=%FirstBroken%"
    		)
    		if %AliasCount% GTR 0 (
    			Set %PathExistSet:~0,-1%Aliases=%Alias1%
    		) else (
    			Set "%PathExistSet:~0,-1%Aliases="
    		)
    	)
    	if defined Pause_On pause
    
    goto :eof
    
    :CheckAlias
    
    	CALL Set "Alias=%%Alias%~1%%"
    	if "%Alias%"=="%~f2" Set "Check=TRUE"
    
    goto :eof
    
    :GetPathLink
    
    	SetLocal DisableDelayedExpansion
    	Set "PathName=%~nx1"
    	CALL StringLength PathLen PathName
    
    	Set "PathLink="
    	Set "BrokenLink="
    
    	for /f "tokens=4*" %%i in ('dir "%~f1" ^| FIND ""') do CALL :GetPathLink_File_Sub "%%j"
    	
    	if NOT defined BrokenLink Set "BrokenLink=:"
    	
    	if defined PathLink if defined DirExist Set "BrokenLink=MISMATCH"
    	if defined PathLink for %%i in ("%PathLink%") do Set "attr=%%~ai"
    	if defined PathLink if "%attr:~0,1%"=="d" Set "BrokenLink=MISMATCH"
    	
    	if defined PathLink	(
    		EndLocal
    		Set "PathLink=%PathLink%"
    		if NOT "%BrokenLink%"==":" Set "BrokenLink=%BrokenLink%"
    		goto :eof
    	)
    	if "%BrokenLink%"==":" Set "BrokenLink="
    	
    	for /f "tokens=4*" %%i in ('dir /A:D "%~f1*" ^| FINDSTR " "') do CALL :GetPathLink_File_Sub "%%j"
    
    	if NOT defined BrokenLink Set "BrokenLink=:"
    	if defined PathLink if defined FileExist Set "BrokenLink=MISMATCH"
    	if defined PathLink for %%i in ("%PathLink%") do Set "attr=%%~ai"
    	if defined PathLink if NOT "%attr:~0,1%"=="d" Set "BrokenLink=MISMATCH"
    	echo %BrokenLink%
    	if NOT defined PathLink Set "PathLink=:"
    
    	(
    		EndLocal
    		if NOT "%PathLink%"==":" Set "PathLink=%PathLink%"
    		if NOT "%BrokenLink%"==":" Set "BrokenLink=%BrokenLink%"
    	)
    
    goto :eof
    	
    :GetPathLink_File_Sub
    
    	if DEFINED PathLink goto :eof
    	Set "TempName=%~1"
    	CALL Set "Temp=%%TempName:~0,%PathLen%%%"
    	if NOT "%Temp%"=="%PathName%" goto :eof
    	CALL Set "Temp=%%TempName:~%PathLen%%%"
    	if NOT "%Temp:~0,2%"==" [" goto :eof
    	Set "Temp=%Temp:~2,-1%"
    	Set "Check=0"
    	if NOT "%Temp:~1,1%"==":" Set /a Check+=1
    	if NOT "%Temp:~0,2%"=="\\" Set /a Check+=1
    	if %Check% EQU 2 goto :eof
    	Set "PathLink=%Temp%"
    	if NOT EXIST "%PathLink%" (
    		Set "BrokenLink=TRUE"
    	) else (
    		Set "BrokenLink="
    	)
    
    goto :eof
    	
    :FindRoot
    
    	Set "CheckPath=%~f1"
    :TrimSlash
    	if "%CheckPath:~-1%"=="\" Set "CheckPath=%CheckPath:~0,-1%" & goto :TrimSlash
    	if "%CheckPath:~-1%"=="/" Set "CheckPath=%CheckPath:~0,-1%" & goto :TrimSlash
    
    	for /d %%h in ("%CheckPath%") do if "%%~dh"=="%CheckPath%" (
    		Set "Root=%%~dh\%Root%"
    		goto :eof
    	)
    	for /d %%h in ("%CheckPath%") do Set "attr=%%~ah"
    	
    	if NOT "%attr:~8,1%"=="l" (
    		for /d %%h in ("%CheckPath%") do (
    			Set "Root=%%~nxh\%Root%"
    			CALL :FindRoot "%%~dph"
    		)
    		goto :eof
    	)
    
    	for /d %%h in ("%CheckPath%") do Set "PathName=%%~nxh"
    	CALL StringLength PathLen PathName
    
    	Set "PathLink="
    
    	for /f "tokens=4*" %%i in ('dir /A:D "%CheckPath%*" ^| FINDSTR " "') do CALL :GetPathLink_File_Sub "%%j"
    	
    	if defined BrokenLink (
    		echo ERROR:  Illogical Broken Symbolic Link ERROR
    		echo %~f1
    		pause
    		goto :eof
    	)
    	if not defined PathLink (
    		echo ERROR:  Illogical Missing Symbolic Link ERROR
    		echo %~f1
    		pause
    		goto :eof
    	)
    	Set /a AliasCount+=1
    	Set "Alias%AliasCount%=%PathLink%\%root%"
    	CALL :FindRoot "%PathLink%"
    	
    goto :eof
    Last edited by ndjamena; 1st Jan 2017 at 20:28.
    Quote Quote  
  8. Member
    Join Date
    Sep 2012
    Location
    Australia
    Search Comp PM
    This batch will prompt for UAC elevation.

    Name it Elevate.bat and put this at the top of any batch file that needs to be run elevated:

    CALL Elevate.bat "%~f0" %*

    I use it to enable the creation of Symbolic Links using MKLINK. I use Symbolic links to manage all my video metadata and command line executables.

    I stole most of the code from StackOverflow but I altered it so that it could be its own batch that could be called from any other batch.

    Code:
    @if not defined EMode (Set "EMode=OFF" & Set "Pause_On=")
    @ECHO %EMode%
    CLS
    
    :init
    setlocal DisableDelayedExpansion
    set "batchPath=%~f0"
    set "vbsGetPrivileges=%Temp%\OEgetPriv_%~n1.vbs"
    setlocal EnableDelayedExpansion
    
    :checkPrivileges
    NET FILE 1>NUL 2>NUL
    if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
    
    :getPrivileges
    ECHO.
    ECHO **************************************
    ECHO Invoking UAC for Privilege Escalation
    ECHO **************************************
    
    (
    	ECHO Set UAC = CreateObject^("Shell.Application"^)
    	ECHO args = "/c " ^& Chr^(34^) ^& Chr^(34^) ^& "%batchPath%" ^& Chr^(34^) ^& " "
    	ECHO For Each strArg in WScript.Arguments
    	ECHO args = args ^& Chr^(34^) ^& strArg ^& Chr^(34^) ^& " "
    	ECHO Next
    	ECHO args = args ^& " " ^& Chr^(34^) ^& "ELEV" ^& Chr^(34^) ^& Chr^(34^)
    	ECHO UAC.ShellExecute "cmd", args, , "runas", ^1
    )>"%vbsGetPrivileges%"
    
    "%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
    exit
    
    :gotPrivileges
    
    Set "Arguments="
    :GetArguments
    
    	if "%~1"=="ELEV" if "%~2"=="" (
    		del "%vbsGetPrivileges%" 1>nul 2>nul
    		cd /d "%BatchFolder:~0,-1%"
    		endlocal
    		endlocal
    		CALL %Arguments%
    		goto :eof
    	)
    	if defined Arguments (
    		Set Arguments=%Arguments% 
    	) else (
    		Set "vbsGetPrivileges=%Temp%\OEgetPriv_%~n1.vbs"
    		Set "BatchFolder=%~dp1"
    	)
    	Set Arguments=%Arguments%"%~1"
    	shift
    	if NOT "%~1"=="" goto :GetArguments
    	
    endlocal
    endlocal
    Last edited by ndjamena; 8th Jan 2017 at 09:19.
    Quote Quote  



Similar Threads