I have the following batch-file code to convert some video files with HandBrakeCLI:
for /R .\test %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
To apply this code on videos, I should copy the batch-file and Handbrake.exe (and also its relative folders) and paste them beside the folder named test (in the code above) then change the name "test" in the batch file to the name of that folder, then run the batch-file.
Could you write the batch-file in a way we run it in an arbitrary folder, so it prompt for a folder containing videos and we write the path (or just simply drag and drop the folder to the command line and press enter) without moving the files and renaming the "test"?
Use "%~1" to read a drag-and-drop argument.
for /R "%~1" %%F in (etc...)
If the batch script is launched without drag and drop (if "%~1"==""), you could use a folder chooser to let the user browse for a folder.
#if (#CodeSection == #Batch) #then
:: based on fchooser2.bat
:: https://stackoverflow.com/a/15906994/1683264
#echo off
setlocal
if "%~1"=="" (
call :chooser dir || goto usage
) else if exist "%~1" (
set "dir=%~1"
) else goto usage
for /R "%dir%" %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
:: end main runtime. Pause if dragged-and-dropped or double-clicked.
if /i "%cmdcmdline:~0,6%"=="cmd /c" pause
goto :EOF
:chooser <var_to_set>
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0"') do (
if not "%%~I"=="" if exist "%%~I" (
endlocal & set "%~1=%%~I"
exit /b 0
)
)
exit /b 1
:usage
echo Usage: %~nx0 [directory]
goto :EOF
#end
// JScript portion
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 0x00);
WSH.Echo(folder ? folder.self.path : '');
You should create a variable to be read from user input
set /p path=Videos path :
for /R %path% %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
set /p will work fine either writing down the path or with folder drag-and-drop
Related
I have a batch file, that constantly checks to see if there are any files in a directory:
#echo off
cls
mode 15,5
cd C:\Users\Toni\Downloads\
goto mark
:mark
set var=2
dir /b /a "Downloads\*" | >nul findstr "^" && (goto exin) || (goto mark1)
goto mark
:mark1
cls
#ping -n 10 localhost> nul
goto mark
:exin
start /B C:\Users\Toni\Downloads\Test\download.bat
exit
if ther are any files in this folder, it moves them.
#echo off
cls
cd C:\Users\Toni\Downloads\Downloads
xcopy /Y C:\Users\Toni\Downloads\Downloads\*.rar C:\Users\Toni\Downloads\Archive
xcopy /Y C:\Users\Toni\Downloads\Downloads\*.zip C:\Users\Toni\Downloads\Archive
xcopy /Y C:\Users\Toni\Downloads\Downloads\*.exe C:\Users\Toni\Downloads\Setups_usw
xcopy /Y C:\Users\Toni\Downloads\Downloads\*.msi C:\Users\Toni\Downloads\Setups_usw
xcopy /Y C:\Users\Toni\Downloads\Downloads\*.mp3 E:\-_MUSIC_-\Musik
xcopy /Y C:\Users\Toni\Downloads\Downloads\*.wav E:\-_MUSIC_-\Musik
xcopy /S /E /Y /EXCLUDE:C:\Users\Toni\Downloads\Test\excludedfileslist.txt C:\Users\Toni\Downloads\Downloads\*.* C:\Users\Toni\Downloads\Sonstiges
goto err
:err
if errorlevel 1 ( dir /arashd >> "C:\Users\Toni\Downloads\Test\somefile.txt" 2>&1 ) else ( del /[!*.part] * )
goto end
:end
start /B C:\Users\Toni\Downloads\Test\run.cmd
exit
However, I do not want to move files that are in the process of downloading (ie. I don't want to move partial files with a .part extension).
I tried using an argument to the del command like so:
del /[!*.part] *
but it doesn't seem to work.
How can I avoid moving partial files with the .part extension?
I would probably look at the file extension (using "substitution of FOR variables").
SET "TARGET_DIR=C:\Users\Toni\Downloads\Downloads"
FOR /F "delims=" %%f IN ('dir /b "%TARGET_DIR%"') DO (
REM Ensure it doesn't have '.part' as an extension.
IF NOT "%%~xf"==".part" (
REM Ensure there's not a corresponding ".part" file.
IF NOT EXIST "%TARGET_DIR%\%%~f.part" (
DEL "%TARGET_DIR%\%%~f"
)
)
)
This will delete any file in TARGET_DIR that doesn't have ".part" as a filename extension or have a corresponding ".part" file. (In my experience downloaders that do the ".part" thing also reserve the name of the "finished" file as well, which you'd probably not want to delete.)
Another possible solution (shorter than #mojo's one):
#echo off
cd /d C:\Users\Toni\Downloads\Downloads
attrib +h *.part
for /f "delims=" %%A IN ('dir /b /A:-H') do del %%A
attrib -h *.part
This, will hide all .part files, delete all other files and remove again the hidden attribute.
I am looking for a batch script which will move files from a sub-directory to its parent if their extension matches the sub-directory extension.
Examples:
Move any .txt file from directory parent\files.txt\
"parent\files.txt\test.txt" will become "parent\test.txt"
Move any .zip file from directory parent\files.zip\
"parent\files.zip\test.zip" will become "parent\test.zip"
I want only to move the file if its extension is the same as that of its sub-directory name. The sub-directory and any other content has to be left alone.
This is what I have now, but it only removes my files:
#echo off
md temp
set view=
if "%view%"=="1" #echo on
color 72
mode con cols=30 lines=8
setlocal enableDelayedExpansion
set /p location_with_dirs=location:
echo:
type nul> ".\temp\folderlist.txt"
FOR /D %%G IN ("%location_with_dirs%\*") DO (
echo process file:
echo %%G
choice
if "%errorlevel%"=="1" echo %%G >> ".\temp\folderlist.txt"
cls
)
FOR /f "delims=" %%G in (".\temp\folderlist.txt") DO (
for %%F in (%%G\*.*) do move /Y %%F "%location_with_dirs%\"
rd %%G
)
del /f /q ".\temp\folderlist\*.txt"
I'm sure there will be many ways of achieving this, here's one:
#CD /D "Parent" 2>Nul || Exit /B
#For /D %%A In (*) Do #If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul
Just change Parent on line 1 to the full or relative path of your parent directory.
Just as a courtesy, heres a version which hopefully improves on the more important part of your provided code, (it ignores the progress bar and color stuff):
#Echo Off
:AskDir
Set "dir_with_files="
Set /P "dir_with_files=location: "
If "%dir_with_files%"=="" GoTo AskDir
If "%dir_with_files:~-1%"=="\" Set "dir_with_files=%dir_with_files:~,-1%"
If Not Exist "%dir_with_files%\" GoTo :AskDir
Set "_rand=%dir_with_files%\%random%.organizer.lock.txt"
Type Nul>"%_rand%" 2>Nul && (Del "%_rand%") || GoTo :AskDir
If "%dir_with_files:~-1%"==":" Set "dir_with_files=%dir_with_files%\"
CD /D "%dir_with_files%" 2>Nul || Exit /B
For /D %%A In (*) Do If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul 2>&1
Exit /B.
I have been using this script to extract and delete archives but it's not handling archives named .part##.rar correctly for some reason. What am I doing wrong?
for /r %%r in (*.zip *.7z *.rar *.ace) do 7z x -y "%%r" >nul && del "%%r" && echo unpacked "%%~nxr"
edit: I decided to build this into a separate script that handles all archives in a single folder %1
:security
cd /d "%~1" || echo no valid directory defined && exit /b
attrib -s -h *.* >nul
dir *.rar *.zip *.7z *.ace >nul 2>nul || exit /b
:extract
for %%r in (*.zip.001 *.7z.001 *.tar.001) do (
7z x -y "%%r" >nul && recycle -f "%%r" && echo unpacked "%%~nxr"
if exist "%%r" echo unpack failed, maybe the disk is full? && exit /b
recycle -f "%%~dpn.???" && echo multipart archives "%%~dpn.???" recycled
)
for %%r in (*.part1.rar *.part01.rar *.part001.rar) do (
7z x -y "%%r" >nul && recycle -f "%%r" && echo unpacked "%%~nxr"
if exist "%%r" echo unpack failed, maybe the disk is full? && exit /b
rem need a way to delete multipart volumes here
)
for %%r in (*.rar *.zip *.7z *.ace *.tar) do (
7z x -y "%%r" >nul && recycle -f "%%r" && echo unpacked "%%~nxr"
if exist "%%r" echo unpack failed, maybe the disk is full? && exit /b
if /i "%%~xr"==".rar" if exist "%%~dpnr.r00" recycle -f "%%~dpnr.r??" && echo multipart archives "%%~dpn.r??" recycled
)
goto security
I need help with deleting the remaining files in the middle stack.
For your second code with expanded loops, here is how to delete the partial .rar archives (loop 2):
setlocal enableDelayedExpansion
for %%R in (*.part1.rar *.part01.rar *.part001.rar) do (
REM notice uppercase R used!
REM process first archive "name.part01.rar"...like posted
REM now delete all partial archives of this name
REM isolate archive name
for %%F in ("%%~nR") do set "files=%%~nF"
if exist "!files!.part*.rar" ECHO del /Q "!files!.part*.rar"
)
As usual please test this and then remove the ECHO statement.
Notes:
1- in your first loop (handling .zip.001 etc.), you need to use %%dpnr instead of %%dpn. Using uppercase loop variables help spotting this kind of error (%R instead of %r).
2- dir *.rar *.zip *.7z *.ace >nul 2>nul || exit /b should be dir *.rar *.zip *.7z *.ace *.tar >nul 2>nul || exit /b to check for tarfiles as well.
This will attempt to narrow down the exception as much as possible to the pattern used, using a Regular Expression:
for /F %%f in ('dir /b /a-d *.zip *.7z *.rar *.ace ^|findstr /REIV /C:"\.part[0-9][0-9]*\.rar"') do #(7z x -y "%%f" >nul && del "%%f" && echo unpacked "%%~nxf")
This batch code is to search all the PDF files in the path specified by the user and copy them all in the home folder and then deleting everything that is inside that folder (subfolders and files). How can I force my code to look for multiple files like pdf,txt and else.
#echo off
setlocal enabledelayedexpansion
goto :main
:main
setlocal
cls
echo.
echo Enter the home directory path where you want to apply the cleaning
set /p path=
echo %path%
cd %path%
for /d %%g in (*) do (
echo %%g
cd %%g
for /r %%p in (*.pdf) do (
set dest=!cd!
set app=/
copy %%p !%dest%%app%!
echo %%p
echo !cd!
)
for /d %%z in (*) do (
rmdir %%z /s /q
)
cd ..
)
pause
endlocal
goto :eof
Nothing prevents you from doing
for /r %%p in ( *.jpg *.png ) do (
REM do something with %%p
)
The catch is that you can use any number of extensions separated by space withing parenthesis.
hi guys i'm a trying to move text files one by one with a timeout of 8 seconds from one folder to another using a batch script. i have this script so far;
move /-y "D:\example\original\*2007*.txt" "D:\example\New folder\"
what should i add so that it doesn't move the files at once?
As #npocmaka recommend to you the user confirmation for example like this batch script : Movies wrapper script
#ECHO OFF
SETLOCAL
SET "ROOT=%~dp0"
SET "FORCE=0"
IF /I "%~1"=="/f" (
SET "FORCE=1"
SHIFT
)
IF "%~1"=="" GOTO :END_PARSE
SET "ROOT=%ROOT%%~1\"
:END_PARSE
ECHO -----------------------------------------------
ECHO WRAPPER - "%ROOT%"
ECHO -----------------------------------------------
SET "COUNT_SUCC=0"
SET "COUNT_FAIL=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
FOR %%f IN ("%ROOT%\*.*") DO (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
IF !FORCE! NEQ 1 (
rem echo force=!Force!
SET /P "INPUT=Do you want to wrap the file "%%~nxf" ? (Y/[N])"
IF /I "!INPUT!"=="Y" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\">NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\">NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
) Else (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
)
)
)
ECHO.
SET /a "COUNT_TOT=COUNT_SUCC+COUNT_FAIL"
ECHO Total of %COUNT_TOT% files(s) : %COUNT_SUCC% file(s) wrapped and %COUNT_FAIL% file(s) failed.
ECHO.
PAUSE
use a simple for loop to process each file individually:
for %%a in ("D:\example\original\*2007*.txt") do (
move /-y "%%~fa" "D:\example\New folder\"
timeout /t 8
)
For more information, see for /?