Special characters cause the Windows Batch File to close - windows

So I created a windows batch script to convert videos using the HandbrakeCLI
Batch script:
#echo off
::SET TO CURRENT CODE PAGE:::::::::::::::::::::::::::
FOR /F "tokens=4 delims= " %%G in ('chcp') DO (
chcp %%G >nul
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal EnableExtensions
title AniCoder v2.5 by Nightsanity
color 0a
cd "%~d0%~p0"
::PREVENT MULTIPLE HANDBRAKE PROCESSES::::::::::::::::
set ignore=INFO:
for /f "usebackq" %%A in (`tasklist /nh /fi "imagename eq HandBrakeCLI.exe"`) do if not %%A==%ignore% (
exit
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::PREREQUISITES CHECK:::::::::::::::::::::::::::::::::
if not exist "HandBrakeCLI.exe" (
echo HandBrakeCLI is missing!
pause
exit
)
if exist "Jobs.txt" del "Jobs.txt"
if not exist "ToConvert" mkdir "ToConvert"
if not exist "Converted" mkdir "Converted"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::CREATE A LIST OF VIDEOS TO CONVERT:::::::::::::::::::
for /F %%i in ('dir /s /b "ToConvert\*.*"') do (
dir/s/b "ToConvert\*" >> "Jobs.txt"
goto MAIN
)
goto NOFILES
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::LOOP THROUGH JOBS LIST AND CONVERT FILES:::::::::::::
:MAIN
for /f "tokens=* delims= " %%a in (Jobs.txt) do (
HandBrakeCLI -i "./ToConvert/%%~nxa" -f mp4 -o "./Converted/%%~na.mp4" -q 22 -e x264 -x cabac=0:ref=2:me=hex:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0 -E faac --mixdown mono -B 64 -X 480 -l 272 -s 1 --subtitle-burn -a 1
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::CLEAN UP FILES:::::::::::::::::::::::::::::::::::::::
if exist "Jobs.txt" del "Jobs.txt"
:Question
cls
echo Encode Done!
echo.
set DELFILES=
set /p DELFILES="Do you want to delete original files (Y/N)?:"
if "%DELFILES%" == "y" goto DELNOW
if "%DELFILES%" == "Y" goto DELNOW
if "%DELFILES%" == "n" goto ENDNOW
if "%DELFILES%" == "N" goto ENDNOW
goto Question
:DELNOW
if exist "ToConvert\*.*" del /Q "ToConvert\*.*"
:ENDNOW
endlocal
exit
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::WHEN NO FILES ARE INSIDE TOCONVERT FOLDER::::::::::::
:NOFILES
echo No video files found in:
echo ToConvert folder
echo.
pause
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
HandbrakeCLI reference:
trac.handbrake.fr/wiki/CLIGuide
I have ran into a problem where if there is a special character in the path for example a German Umlaut it will not interpret it or something as it wont convert and close the window.
How can I workaround this issue?
Running Windows 7 Home Premium Sp1

I'd suggest you change
for /F %%i in ('dir /s /b "ToConvert\*.*"') do (
dir/s/b "ToConvert\*" >> "Jobs.txt"
to
for /F "delims=" %%i in ('dir /s /b "ToConvert\*.*"') do (
echo(%%~dspnxi >> "Jobs.txt"
to output the shortname to jobs.tst.
or perhaps
echo(%%~dspi%%~nxi >> "Jobs.txt"
may be better.
(can't test - no test directories available to me)

Related

Find string in multiple .txt files

I have a folder with many .txt files. I would like to find string "X" in all of these files then I would like to copy the found strings into .txt files into a different folder.
So far I have tried :
#echo on
findstr /m "X" "%userprofile%\Desktop\New_Folder\New_Folder\*.txt"
if %errorlevel%==0 do (
for %%c in (*.txt) do (
type %%c >> "%UserProfile%\Desktop\New_Folder\%%~nc.txt"
pause
I do not understand the output %%~nc.txt part it's suppost to copy the changed .txt files to a new folder with the same name.
I would like to point out that string "X" is found in different places in the .txt file.
This batch file can did the trick (-_°)
So, just give a try : ScanfilesWordSearch_X.bat
#ECHO OFF
::******************************************************************************************
Title Scan a folder and store all files names in an array variables
SET "ROOT=%userprofile%\Desktop"
Set "NewFolder2Copy=%userprofile%\Desktop\NewCopyTxtFiles"
SET "EXT=txt"
SET "Count=0"
Set "LogFile=%~dp0%~n0.txt"
set "Word2Search=X"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder and its subfolders.
REM And Populate the array with existent files in this folder and its subfolders
For %%a in (%EXT%) Do (
Call :Scanning "%Word2Search%" "*.%%a"
FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.%%a"') DO (
( find /I "%Word2Search%" "%%f" >nul 2>&1 ) && (
SET /a "Count+=1"
set "list[!Count!]=%%~nxf"
set "listpath[!Count!]=%%~dpFf"
)
) || (
( Call :Scanning "%Word2Search%" "%%~nxf")
)
)
::***************************************************************
:Display_Results
cls & color 0B
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 20
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO **********************************************************
ECHO Folder:"%ROOT%"
ECHO **********************************************************
If Exist "%LogFile%" Del "%LogFile%"
rem Display array elements and save results into the LogFile
for /L %%i in (1,1,%Count%) do (
echo [%%i] : !list[%%i]!
echo [%%i] : !list[%%i]! -- "!listpath[%%i]!" >> "%LogFile%"
)
(
ECHO.
ECHO Total of [%EXT%] files(s^) : %Count% file(s^) that contains the string "%Word2Search%"
)>> "%LogFile%"
ECHO(
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo Type the number of file that you want to explore
echo(
echo To save those files just hit 'S'
set /p "Input="
For /L %%i in (1,1,%Count%) Do (
If "%INPUT%" EQU "%%i" (
Call :Explorer "!listpath[%%i]!"
)
IF /I "%INPUT%"=="S" (
Call :CopyFiles
)
)
Goto:Display_Results
::**************************************************************
:Scanning <Word> <file>
mode con cols=75 lines=3
Cls & Color 0E
echo(
echo Scanning for the string "%~1" on "%~2" ...
goto :eof
::*************************************************************
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
::*************************************************************
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\"
Copy /Y "%~1" "%~2\"
goto :eof
::*************************************************************
:CopyFiles
cls
mode con cols=80 lines=20
for /L %%i in (1,1,%Count%) do (
echo Copying "!list[%%i]!" "%NewFolder2Copy%\"
Call :MakeCopy "!listpath[%%i]!" "%NewFolder2Copy%">nul 2>&1
)
Call :Explorer "%NewFolder2Copy%\"
Goto:Display_Results
::*************************************************************
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "mystring=x"
FOR %%a IN ("%sourcedir%\*.txt") DO FINDSTR "%mystring%" "%%a">nul&IF NOT ERRORLEVEL 1 FINDSTR "%mystring%" "%%a">"%destdir%\%%~nxa"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances and set mystring appropriately, noting that you may have to adjust the findstr switches to accomodate case, literal and space-in-target-string.
Naturally, you could code sourcedir etc. directly as literals, but doing it this way means that the relevant strings need only be changed in one place.
You are close, but checking the ErrorLevel of findstr does not make sense here as this reflects the overall result, that is, ErrorLevel is set to 0 in case any of the files contain the search string.
I would parse the output of findstr /M using a for /F loop and copy the returned files in the body:
for /F "eol=| delims=" %%F in ('
findstr /M /I /C:"X" "%USERPROFILE%\Desktop\New_Folder\New_Folder\*.txt"
') do (
copy "%%F" "%USERPROFILE%\Desktop\New_Folder\"
)
This copies all those files which contain the literal search string (in a case-insensitive manner).

How to test if a Zip file is valid in a windows batch file

I am using the command line version of 7-zip and I can use this to test if a zip file is valid by using the t command.
But I'm trying to create a batch file which will cycle through a bunch of zip files in a directory and do one thing if the zip is empty, and another thing if the zip has some files archived in it.
Any pointers how you do this using a batch file?
this may help
#echo off
setlocal enabledelayedexpansion
set "zipPath=C:\temporal\"
set "zipProg=C:\Program Files (x86)\7-Zip\7z.exe"
call:shortIt "%zipPath%", zipPath
call:shortIt "%zipProg%", zipProg
pushd %zipPath%
for /F "tokens=*" %%a in ('dir /b "%zipPath%" ^| find ".zip"') do (
set/a numFiles=0, isOk=0, size=0, compressed=0
for /F "tokens=*" %%i in ('%zipProg% t "%zipPath%%%a"') do (
echo %%i | find /I "ok" >NUL && set/a isOK=1
echo %%i | find /I "files" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a numFiles=%%n
echo %%i | find /I "size" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a size=%%n
echo %%i | find /I "compressed" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a compressed=%%n
)
if !isOk! neq 0 if !numFiles! equ 0 if !size! neq 0 set/a numFiles=1
if !isOk! equ 0 (
echo(
echo(%zipPath%%%a is not an archive or an error ocurred
echo(
) else (
if !numFiles! neq 0 (
echo(%zipPath%%%a contains !numFiles! files [!size! to !compressed!]
) else (
echo(%zipPath%%%a is empty
)
)
)
popd
endlocal
exit/B 0
:shortIt
SetLocal & set "token=%~s1"
EndLocal & set "%2=%token%"

Batch script help, script exiting after parentheses

I wrote a very simple script to output the host machine's MAC addresses to a text file.
The script is exiting right after line 3 - 'IF DEFINED WRITEOK ('.
#echo off
cls
copy /Y NUL "%CD%\.writable" > NUL 2>&1 && set WRITEOK=1
IF DEFINED WRITEOK (
rem ---- we have write access ----
set DIR=%CD%\interfaces
set FILE=%DIR%\%USERNAME%.txt
IF NOT EXIST "%DIR%" (
MKDIR "%DIR%"
echo DIR '%DIR%' was created
) else (
echo DIR '%DIR%' already exists
) for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (
echo %%i >> "%FILE%"
echo OUTPUT written to '%FILE%'
)
) else (
rem ---- we don't ----
echo DIR '%DIR%' is not writable
)
echo.
echo DONE!
pause
Try to put the FOR one line after the closing parenthesis :
...)
for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (...
you can't start a FOR with a closing parenthesis in front :
This will not work :
(echo 1
) for /l %%a in (1,1,10) do echo %%a
and this will work :
(echo 1
)
for /l %%a in (1,1,10) do echo %%a
EDIT 1 :
For the path variables containing space use double quote :
"%cd%"
when using it.

Windows batch script issue

I am having an issue with the following Windows batch script. I have multiple XML files in the %indir% that I want to import one at a time and email the report. Basically the section starting with "if /I %v_continue% == y" is not executing as expected. I did some debugging step by step with echo on, and it goes through the script till the %BLAT% command without executing any of the steps, and then it starts execution with the first "copy %script_path%.....". It executes the import.exe correctly, but then nothing gets assigned to %subj% and subsequently the %BLAT% command fails. Any advice?
Thanks!
#echo off
set environment=%1
set domain=%2
if [%environment%] == [] goto :endofscript
rem - Get the script path
set script_path=%~dp0
rem - Get the script name without the extension
set script_name=%~n0
rem - Get the script name with the extension
rem set script_name=%~nx0
rem - get the script extension
set script_ext=%~x0
rem - Set environment variables
call %script_path%\setenv.cmd
set cnt=0
set filemask=*.xml
for /f %%a in ('dir /b /a-d %indir%\%filemask%') do call :procfile %%a
goto :EOF
:procfile
set impfile=%1
set v_continue=n
set emailyn=y
set trset=%impfile:~0,3%
set /A cnt + = 1
if 1%cnt% lss 100 set cnt=0%cnt%
if %trset% == RCT (
set "subtxt=Receipt Confirmation"
set v_continue=y
)
if %trset% == SHP (
set "subtxt=Shipment Confirmation"
set v_continue=y
setlocal EnableDelayedExpansion
set MOFound=
for /f "tokens=3 delims= " %%f in ('find /i /c "<RefID>MO-ORD</RefID>" %indir%\%impfile%') do (set MOFound=%%f)
if !MOFound! GTR 0 (
copy %indir%\%impfile% %inarchdir%
move %indir%\%impfile% %S_INDIR%\FX%dttmstamp%%cnt%.xml 2>NUL
goto :EOF
)
endlocal
)
if /I %v_continue% == y (
copy %script_path%\%script_name%.dat %infile%
cscript %REPLACEVBS% %infile% "DOMAIN" "%domain%" 1>NUL 2>&1
cd /d %rptdir%
%DLC%\bin\import.exe -b -T d:\tmp -p %pfile%
rem - Check for errors
find /i /c "ERROR:" %rptfile% > NUL
if %ERRORLEVEL% NEQ 0 (
set "subj=SUCCESS: %subtxt% Import Report (%environment%/%domain%)"
set emailyn=y
) else (
set "subj=ERROR: %subtxt% Import Report (%environment%/%domain%)"
set emailyn=y
)
move %rptfile% %logdir%\%script_name%_%datestamp%_%cnt%.prn 2>NUL
move %outfile% %logdir%\%script_name%_%datestamp%_%cnt%.out 2>NUL
if /I %emailyn% == y (
echo Report location: %logdir%\%script_name%_%datestamp%_%cnt%.prn > %msgfile%
%BLAT% %msgfile% -server abc-com.mail.protection.outlook.com -f donotreply#abc.com -s "%subj%" -t %INBEMAIL% -attachi %logdir%\%script_name%_%datestamp%_%cnt%.prn
)
)
del /f /q %infile%
del /f /q %pfile%
del /f /q %msgfile%
:delfiles
rem - Delete log files that are older than 10 days.
PushD "%logdir%" && (
forfiles /M %script_name%_*.prn /D -10 /C "CMD /C del /f /q #PATH" 2>NUL
) & PopD
:endofscript
exit /B

windows batch, setting a variable inside a nested loop

I have the below batch that reads data from a text file, the problem is in the inner loop; it should get the lat created file in the destination folder, and it just gets nothing!
here's my code:
:: Delete Files from folder
#echo off
:: Delete Files from folder
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.bak"
echo Deleting previous bak files...
set destdir=E:\HIS_Data_Consolidation\HIS_Backups
setlocal
FOR /F "tokens=1,2,3 delims=," %%G IN (clinics.txt) DO (
pushd "%%G"
for /F "tokens=*" %%a in ('dir *.* /b /a-d /o:e 2^>NUL') do (
set lfile=%%a
)
echo copying "%%G\%lfile%" to "%destdir%" ,,,%%H
copy /y "%%G\%lfile%" "%destdir%
E:
cd "%destdir%
E:\HIS_Data_Consolidation\HIS_Backups\unrar.exe e "%destdir%/%lfile%"
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.rar"
echo Deleting RAR file...
SET v_test=%lfile%
SET v_result=%v_test:rar=bak%
ren "%v_result%" "%%I"
echo Ready ...
popd
)
pause
appreciate you help.
thanks.
If you'are using a set inside for body you'll need enabledelayedexpansion:
http://www.robvanderwoude.com/variableexpansion.php
edit:
:: Delete Files from folder
#echo off
:: Delete Files from folder
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.bak"
echo Deleting previous bak files...
set destdir=E:\HIS_Data_Consolidation\HIS_Backups
setlocal enabledelayedexpansion
FOR /F "tokens=1,2,3 delims=," %%G IN (clinics.txt) DO (
pushd "%%G"
for /F "tokens=*" %%a in ('dir *.* /b /a-d /o:e 2^>NUL') do (
set lfile=%%a
)
echo copying "%%G\!lfile!" to "!destdir!" ,,,%%H
copy /y "%%G\!lfile!" "!destdir!"
E:
cd "!destdir!"
E:\HIS_Data_Consolidation\HIS_Backups\unrar.exe e "!destdir!/!lfile!"
echo y | del "E:\HIS_Data_Consolidation\HIS_Backups\*.rar"
echo Deleting RAR file...
SET v_test=!lfile!
SET v_result=!v_test:rar=bak!
ren "!v_result!" "%%I"
echo Ready ...
popd
)
endlocal
pause
#npocmaka
Thank you! You solved my problem I was working on for hours!
I had a similar problem.
I was using a nested for loop and tried to split the for loop into another batch file, but didn't work. So I used 'npocmaka's advice:
This is the batch being called which is inside another for loop.
#ECHO OFF
SET dir=%~1
SET suf=%~2
ECHO IN dir:%dir%
ECHO IN suf:%suf%
PAUSE
SET /A count=1
SETLOCAL EnableDelayedExpansion
pushd %dir%
FOR /R . %%A IN (*.%suf%) DO (
ECHO File: %%~nxA count:!count!
PAUSE
REN %%~nxA !count!.txt
CALL :increment RESULT count
)
popd
ENDLOCAL
:increment
SET /A count+=1

Resources