batch extract and delete archives, how to delete .part*.rar - batch-file

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

Related

Batch deleting every file except partial files

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.

"Delete files script" continue to next pc if a pc on list is turned off issue

I have a script to delete all files in my Epson Scans Folder. In my pclist.txt i have each pc name listed on each line. I am trying to figure out how to adjust this script to continue to the following pc if a pc on the list is not on. The script runs perfect if all pcs are turned on. But if a pc is turned off it stops running and does not move on to the next pc in the list. Does anyone know how I can adjust for that?
Note: pclist.txt is just a list of pc names
#echo off
IF "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"
for /F %%G in (pclist.txt) do (
pushd "\\%%G\C$\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
)
There is an exit /B command, so it is no surprise that the script terminates in case pushd fails. The || constitutes a conditional command concatenation operator which lets the following command execute only in case the previous one fails (that is, it returns a non-zero exit code).
So I would remove the || exit /B 1 part and simply reverse the logic, so that the deletions are accomplished only in case pushd succeeds to connect to the drive. There is also an && operator that lets the following command execute in case the preceding one succeeded, which I would use:
#echo off
for /F "usebackq delims=" %%G in ("%~dp0pclist.txt") do (
pushd "\\%%G\C$\Epson Scans" && (
rem Perform the actual deletions in case of successful connection:
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
) || (
rem Do something in case of failure, like printing an error message:
>&2 echo Could not connect "%%G"!
)
)
Try using ping command (with -n 1 tries only 1 time) and test using ERRORLEVEL variable
ping -n 1 MYSERVER > nul
if NOT ERRORLEVEL 1 (
echo "MYSERVER IS OK"
)
In your script:
#echo off
IF "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"
for /F %%G in (pclist.txt) do (
ping -n 1 %%G > nul
if NOT ERRORLEVEL 1 (
pushd "\\%%G\C$\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
)
)

Open multiple folders with batch script

I have on my computer an partition named H:
On that partition i have diffent folders with each a folder called "bin"
Example:
H:MyFolder\Bin
H:AnotherFolder\Bin
I know that I can delete bin with the follow command:
set folder="H:\Bin"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
Is it possible to empty all the bin folders from all folders in partition H: with a command?
This will remove the content of all the bin folders under H:\ while keeping the folders.
for /r "H:\" /d %%a in (bin) do #if exist "%%~fa\" ( pushd "%%~fa" && ( echo "%%~fa" & echo rmdir . /s /q & popd ))
There is an echo command that precedes the rmdir. This is included for testing and must be removed to perform the content removal.

batch-file for drag-and-drop path assigning to HandBraking videos

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

Batch - loop misses commands after few passes

I tried to write simple program. It should find .pak files (zip in this case but with changed etension), extract them and pack them using arc.
#echo off
for /r %%i in (*.pak) do ren %%~i %%~ni.arc
for /r %%i in (*.arc) do (
mkdir %%~ni
cd %%~ni
..\arc.exe x -o+ "%%~i" //extract archive at current location//
del "%%~i"
..\arc.exe a -m9 -r "%%~i" *.* //pack files and folders in current folder and create
archive at specific location//
cd..
RD /s /q %%~ni
)
pause
Arc command works i'm sure about that. Everything works perfect untill few passes later. It seems that it suddenly stop doing cd.. command because it starts creating folder then folder inside then again and again.
Locations of files:
...\arc.exe
...\program.bat
...\file1.pak
...\folder1\file2.pak
...\folder2\file3.pak
etc for pack
I tried also
#echo off
setlocal enableextensions enabledelayedexpansion
for /r %%i in (*.pak) do ren %%~i %%~ni.arc
for /r %%i in (*.arc) do (
mkdir %%~ni
cd %%~ni
..\arc.exe x -o+ "%%~i"
del "%%~i"
..\arc.exe a -m9 -r "%%~i" *.*
cd..
RD /s /q %%~ni
)
pause
The same result
You definitely do not want to enable delayed expansion since it will corrupt file names if they happen to contain ! character.
I believe your problem is simply you haven't quoted your path/file names. Spaces and special characters in path/name will cause problems unless they are quoted.
This is completely untested, but I think it may fix you problem:
#echo off
for /r %%i in (*.pak) do ren "%%i" "%%~ni.arc"
for /r %%i in (*.arc) do (
mkdir "%%~ni"
cd "%%~ni"
..\arc.exe x -o+ "%%~i" //extract archive at current location//
del "%%i"
..\arc.exe a -m9 -r "%%i" *.* //pack files and folders in current folder and create archive at specific location//
cd..
rd /s /q "%%~ni"
)
pause

Resources