batch file use parameter and string to create path - variables

I know there's a ton of stuff out there on this but I just can't make it work. What I want to do is
#ECHO OFF
FOR /D %%D IN (*) Do (
ECHO %%D
if not exists %%D.mp4 (
rem cd %%D
rem "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "VIDEO_TS" -o "..\%%D.mp4" --preset="High Profile" --x264-preset slow
echo not exists
)
rem cd ..\
)
I left the extra stuff in there so you could see that I actually need the /D rather than a /R. basically I'm converting the VIDEO_TS folder that is in an other folder into an mp4 of the parents name and putting it in the root.
the problem is checking if the file already exists. I can't figure out how to create a path that "exists" will recognize out of the var %%D and the suffix .mp4
Any help would be greatly appreciated.
Thanks,
R

There are two errors here
The keyword is EXIST, not EXISTS.
You need to enclose the variable in double-quotes, due to spaces in filenames.
Example:
#ECHO OFF
FOR /D %%D IN (*) DO (
ECHO %%D
IF NOT EXIST "%%D.mp4" (
ECHO not exists
)
)
Further, if you uncomment those other lines, you'll have issues because the cd .. is not at the same branching level as the cd "%%D" (note I added double-quotes here). You should move that into the IF block.
However, instead of changing directory, you could simply pass "%%D\VIDEO_TS" to Handbrake.
FOR /D %%D IN (*) DO (
IF NOT EXIST "%%D.mp4" (
echo Encoding %%D ...
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "%%D\VIDEO_TS" -o "%%D.mp4" --preset="High Profile" --x264-preset slow
)
)
Or you could use pushd and popd:
FOR /D %%D IN (*) DO (
pushd .
IF NOT EXIST "%%D.mp4" (
cd "%%D"
echo Encoding %%D ...
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "VIDEO_TS" -o "..\%%D.mp4" --preset="High Profile" --x264-preset slow
)
popd
)

Related

Retry a sql query in case of failure in batch

This script processes all the sql files in a given folder and outputs the result to a csv. Do you have any ideas how could I adapt it to retry the sql file in case of error or failure?
#ECHO OFF
SET SQLCMD="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
SET PATH="C:\Users\username\Desktop\Scripts\Reports\sql"
SET SERVER="localhost"
SET DB="database"
SET LOGIN="username"
SET PASSWORD="password"
SET OUTPUT="C:\Users\username\Desktop\Scripts\Reports\output_%date%-%time:~0,2%-%time:~3,2%-%time:~6,2%.csv"
CD %PATH%
ECHO %date% %time% > %OUTPUT%
for %%f in (*.sql) do (
%SQLCMD% -S %SERVER% -d %DB% -E -i %%~f >> %OUTPUT% -W -w 1024 -s";")
Thank you!
You cant test the ERRORLEVEL environment variable. typically a zero ERRORLEVEL value means success. also you need to enable delayed expansion to check it inside a block.
something like this may help,
SetLocal EnableDelayedExpansion
for %%f in (*.sql) do (
Set /a success=1
for /L %%w in (1,1,5) do ( rem retry five times
if !success! NEQ 0 (
%SQLCMD% -S %SERVER% -d %DB% -E -i %%~f >> %OUTPUT% -W -w 1024 -s";"
if !ERRORLEVEL! EQU 0 set /a success=0
)
)
if !success! NEQ 0 (
rem sql failed, log or advise
)
)
EndLocal
another important point, be careful with the PATH environment variable. you should better use another name as SQL_PATH or MY_PATH.
or you can use PUSHD & POPD to change your working dir
#echo off
SET ...
rem save current dir and jump to...
pushd "C:\Users\username\Desktop\Scripts\Reports\sql"
for %%f in (*.sql) do (
...
...
)
rem restore dir saved by pushd
popd

New to batch and having issues with multiple variables and for loops

Trying to find out which users have google chrome by searching the appdata folder of each user for the executable and then a series of actions to take if found. On our system some of the user folders are on a persistent disk, D:. I used a for loop for both instances but I'm sure there is a better way.
If I run it from the computer it seems to work but when ran as a start up nothing seems to happen. Wondering if someone could point out issues or inefficiencies, I know there in there.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%H IN ("D:\Users\*") DO (
IF EXIST "%%H\appdata\local\google\chrome\application\chrome.exe" (
DEL /Q "%%H\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%H Replaced >> "ChromeInstalls.txt"
)
)
FOR /D %%G IN ("C:\Users\*") DO (
IF EXIST "%%G\appdata\local\google\chrome\application\chrome.exe" (
DEL /Q "%%G\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%G Replaced >> "ChromeInstalls.txt"
)
)
EXIT /b
Any help is appreciated.
The problem is that you are parsing invalid path to the for body . Try using /r switch to lock on the parent drive containing the chome file to process all subdirectories inside the specified drive so that you dont have to use a full path in your set () eg ..
#echo off
For /r d: %%a in (*) do (
If "%%~nxa"=="Chrome.exe" (
DEL /Q "%%G\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%G Replaced >> "ChromeInstalls.txt"
)
)
For /r c: %%a in (*) do (
If "%%~nxa"=="Chrome.exe" (
DEL /Q "%%G\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%G Replaced >> "ChromeInstalls.txt"
)
)
Try that out . Hpe this helps .

batch rename directories from "NOTE" to "NOTES"

I would like to rename directories whose name contains the word "NOTE", but not "NOTES", to "NOTES". I first experiment with the echo command.
for /f "tokens=1-7" %%i in ('dir d:\mydirectory /s /b /ad ^|find "NOTE" ^|find "NOTES" /v') do #echo %%i %%j %%k %%l %%m %%n %%oS
Because directory names have different spaces in them, the above command may leave spaces between "NOTE" and S. Anyway to overcome this problem?
Give this a burl. If it echo's the right rename command then remove the echo and the pause to activate it. It's untested. Paths containing ! and % characters will cause an issue.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (*) do (
set "f=%%~nxa"
if not "!f:NOTE=!"=="%%~nxa" (
if "!f:NOTES=!"=="%%~nxa" (
echo ren "%%a" "!f:NOTE=NOTES!"
pause
)
)
)

Batch for loop through variables, can't cd to each?

I have a for loop in a batch file:
#echo off
set logpath1=C:\path\to\first\log
set logpath2=C:\path\to\second\log
FOR %%G IN (%logpath1% %logpath2%) DO (
pushd %%G
pushd ..\
for %%D IN (%CD%) DO SET "dirname=%%~nxD"
popd
echo Will prefix files in %%G with %supername%
:: do operations.
popd
)
However, this doesn't seem to change my working directory. If I run the script from
C:\tmp\path\to
then I get
Will prefix files in C:\path\to\first\log with tmp
Will prefix files in C:\path\to\second\log with tmp
My purpose is to zip up log files in multiple directories. I've tried chdir, cd /d, and &~pd0 instead of %CD%, and searched high and low. Can anyone tell my why this doesn't work??
Put
setlocal enabledelayedexpansion
at the top of your batch file and use !CD! instead of %CD% in the loop. Depending on where %supername% is set you may need to use ! there too.

Batch file that returns folder size

I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.
I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0
Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/