Batch zipping multiple files in single directory - batch-file

I need to make batch script, which will work this way:
There is a folder with one hundred files. I need to make a script which is zipping every 4 files into single archive. Then all created archives must be zipped into one, big archive file. That must be all done by one script.
I tried to make it, but I haven't idea how to make it this way.
This is what I wrote first:
#ECHO ON
SET SourceDir=C:\Users\Ridaan\Documents\sobol
SET DestDir=C:\folder\Destination
CD /D "C:\Program Files\7-Zip"
FOR /F "TOKENS=*" %%F IN ('DIR /B /A-D "%SourceDir%"') DO (
7z.exe a "%DestDir%\%%~NF.zip" "%SourceDir%\%%~NXF"
)
EXIT
And second:
#echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
robocopy C:\folder\Destination /COPY:DAT /V /XO /NJH /NP /R:1000 /W:10
7z u -mx9 "C:\folder\End.7z" "C:\folder\Destination"
rmdir C:\folder\End\ /Q /S
:END
pause

Just to give you an idea of counting files and
calculating the int of count/4 for the zip number
the modulus%4 for the file number (0..3) to determine if it's a new zip to create or to append to the zip.
:: Q:\Test\2018\11\11\SO_53251220.cmd
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set Cnt=0
Set "SrcDir=X:\Your\Path"
For /F "delims=" %%A in ('Dir /B/A-D "%SrcDir%\*"') do (
Set /A "File=Cnt%%4,Zip=Cnt/4,Cnt+=1"
Echo Cnt=!Cnt! File=!File! Zip=!Zip! File=%%A
)

Related

Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)

Batch script to delete files in folder that does not contain certain word

I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.

Batch file - copy two last modified files

I would like to write a .bat file to move the two last modified files of a specific extension *.bak in directory a to a different directory.
I used this line to copy files:
robocopy D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups *.bak /S
I'm new with this and have no idea how to tweak this to get the result I need.
Thanks
not tested:
#echo off
for /f "tokens=* delims=" %%# in (' dir /a:-d /o:-d /t:a /b "D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups\*.bak"') do (
if not defined last set "pre_last=%%~f#"
set "last=%%~f#"
)
copy /y "%last%" "c:\new_dir"
copy /y "%pre_last%" "c:\new_dir"
#echo off
setlocal EnableDelayedExpansion
cd "D:\DailyBackup\IDMRObjects\SQLBackups"
set copied=0
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W *.bak') do (
copy "%%a" "other\dir"
set /A copied+=1
if !copied! equ 2 goto break
)
:break

Create BAT file to get directory listing up to 2nd level only

I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.

File Retention Script

I have a directory that has a 10 sub-directories. Each of these holds different .bak files. I'm trying to create a script that will check to see if X number of files are there and if the number of files exceeds X, it deletes the oldest file. In other words, I want 20 iterations of a .bak file. When the 21st one shows up, I want the batch file to delete the oldest one.
Is this possible?
If so, can I create a single script that looks in all the sub-directories?
Thanks in advance.
Two options included. The first one will leave %maxFiles% bak files under each of the folders. The second one (windows Vista or later OS is required as robocopy is used to obtain the sorted list of files) will leave %maxFiles% in total
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=%cd%"
set "maxFiles=20"
rem Option 1 - Keep %maxFiles% inside each of the subfolders
for /d /r "%rootFolder%" %%z in (*) do for /f "skip=%maxFiles% delims=" %%a in (
'dir /tc /o-d /a-d /b "%%~fz\*.bak" 2^>nul'
) do echo del "%%~fz\%%~nxa"
echo ------------------------------
rem Option 2 - Keep %maxFiles% in total under all the subfolders
for /f "skip=%maxFiles% tokens=2,*" %%a in ('
robocopy "%rootFolder%" "%rootFolder%" *.bak /l /nocopy /is /s /njh /njs /ndl /nc /ns /ts
^| findstr /v /r /e /i /c:"%rootFolder:\=\\%\\[^\\]*"
^| sort /r
') do echo del "%%b"
del commands are only echoed to console. If the output is correct, remove the echo command to remove the files
assumes that you want to check the number of files from the parent directory.Can be done also for each sub-directory.
#echo off
setlocal
set "max_number_files=20"
set "parrent_dir=c:\whatever_you_need"
set "extension=.bak"
pushd "%parrent_dir%"
set "count=0"
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir /s /b /a:-d /o:-d /t:c *%extension%') do (
set /a count=count+1
if 1!count! GTR 1!max_number_files! (
rem --- remove the echo to activate deletion
echo del /q /f "%%~a"
)
)
popd
endlocal
endlocal
This will check each folder under d:\base\folder and if there are more than 20 *.bak files it will remove the oldest ones so only 20 *.bak file remain, in each folder.
Test it on some sample folders.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
pushd "%%a"
for /f "skip=20 delims=" %%b in ('dir /b /a-d /o:-d *.bak ') do del "%%b"
popd
)

Resources