Batch script for compressing multiple files into separately output - batch-file

I'm having a little problem for compressing multiples files.
The problem is, if the file name is the same even not in the same folder, they are automatically compressing.
Here's the example of what I'm talking about.
Folder 1:
JOHN 1.psd
JOHN 2.psd
Folder 2
JOHN 1.psd
JOHN 2.psd
The problem output become like this, JOHN 1.psd.rar
and inside of that file, there is JOHN 1.psd, (from folder 1), and JOHN 1.psd, (from folder 2).
I want, to compress them in the same source folder even the same file from different folder. Because I'm doing so many files to compress.
Here's my script:
#echo off
:start
cls
set path="C:\Program Files\WinRAR\Rar.exe"
set ext=psd
echo.
#echo off
for /r %%A in (*.%ext%) do (
%path% a -r -ep -df "%%A.rar" "%%~nxA"
)
exit

Here's my solution:
#echo off
:start
cls
set WinrarDir="C:\Program Files\WinRAR\Rar.exe"
echo.
#echo off
for /r %%i in (*.psb) do (
%WinrarDir% a -r -ep -df "%%i.rar" "%%i"
)
exit

Related

Windows 10 batch unzip multiple files with filename the same as zip directory name

I have about 60 files to unzip as you can see below:
I know the 7zip option, which can unzip all of them, but the problem is, that the file inside doesn't match the zip directory name, which would be highly desirable here.
I found some solutions for the batch file here:
https://superuser.com/questions/371384/extract-all-zips-in-a-directory-incl-subfolders-with-a-bat-file-or-dos-comm
and prepared some batch code for this which looks like this:
#echo off
setlocal
cd /d %~dp0
for %%a in (*.zip) do (
Call :UnZipFile "C:\my\Desktop current\Occ KMZ\bat\Aldebaran" "C:\my\Desktop current\Occ KMZ\bat\Aldebaran"
)
exit /b
but it doesn't work at all. I can neither unzip it nor get files.
The 7zip software has a few options, which potentially could be good
but I don't know how to use the 7Zip command line in order to get the unzipped file with the same name as the zip directory.
What should I do to automatize this section?
Here's something (minimal) that should work. Some parts can be done better (error handling and so on). It unzips each archive into a dir named like the archive but without the (.zip) extension.
script00.bat:
#echo off
setlocal enableextensions enabledelayedexpansion
set EXE_7Z="c:\Install\pc064\7Zip\7Zip\Version\7z.exe"
for %%g in ("%~dp0*.zip") do (
call :unpackFile "%%g"
)
goto :done
:unpackFile
set _ARCH_DIR="%~dp1%~n1"
rmdir /q /s %_ARCH_DIR% 2>nul
%EXE_7Z% x -o%_ARCH_DIR% %1 >nul 2>&1
goto :eof
:done
echo Done.
goto :eof
Output:
[cfati#CFATI-5510-0:e:\Work\Dev\StackOverflow]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
[prompt]> tree /a /f q071380369
Folder PATH listing for volume SSD0-WORK
Volume serial number is 00000068 AE9E:72AC
E:\WORK\DEV\STACKOVERFLOW\Q071380369
file00.zip
file01.zip
script00.bat
No subfolders exist
[prompt]> q071380369\script00.bat
Done.
[prompt]> tree /a /f q071380369
Folder PATH listing for volume SSD0-WORK
Volume serial number is 0000006E AE9E:72AC
E:\WORK\DEV\STACKOVERFLOW\Q071380369
| file00.zip
| file01.zip
| script00.bat
|
+---file00
| kkt.py
|
\---file01
URLs.txt

CMD Batch script to sort files

I have an application which saves files to a FTP folder that I sync to my PC which contains multiple JPG and MP4 files, named in the following format:
ARC20170510151547.jpg
ARC20170510151549.mp4
What I'm trying to do is:
Copy the files from my FTP to my PC
Sort the files into folders based on the day they were created
Delete files from the FTP older than 14 days
Delete files from the PC older than 1 month
Using WinSCP to connect to FTP with the following code, I am able to download all the files to my local drive:
"c:\program files (x86)\winscp\winscp.com" /ini=nul /command ^
"open ftp://[username]:[password]#[ipaddress]/" ^
"synchronize local f:\[localpath]\ /[remotepath]/ " ^
"exit"
Then I need to sort the files. Here is where I am stuck. I'm think I know the commands but I am unsure how to use the 'tokens' and 'delims' to get it to work how I want.
#echo
for %%a in (*.*) do (
echo processing "%%a"
for /f "tokens=1 delims=" %%a in ("%%~nxa") do (
md "%%b-%%c" 2>nul
move "%%a" "%%b-%%c" >nul
)
)
pause
As I know the filename format isn't going to change, one thing I have considered is adding some special characters to the filename, maybe using the 'ren' command. I could then use those special characters as search delims but, again, I'm struggling how to best proceed.
Removing local files older than 30 days is easy using the following script
forfiles -p "f:\[localpath]" -s -m *.* -d <number of days> -c "cmd /c del #path"
However, the WinSCP 'RM' command I am using doesn't appear to be working. It returns an error "no file matching '*<14D' found"
"rm /[filepath]/*<14D" ^
Any help, advice and guidance would be very gratefully received!
Since there is no delimiter between the date elements you need substrings,
substrings do only work with normal variables and in a (code block) you need delayed expansion
It's unclear if you want folders with year-month or month-day, select the proper part in the batch and comment/uncomment the Rem:
With extensions enabled md can create the structure YY-MM\DDin one step.
So you can move directly to that folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~nA
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
Rem YY-MM\DD
md "!YY!-!MM!\!DD!" 2>nul
move "%%a" "!YY!-!MM!\!DD!" >nul
)
pause
#LotPings! Thank you for the script. This does almost what I want it to do. I have modified the script as below which moves the files into folders based on the Day.
So, each day, maybe 100 - 200 files are generated. So I do not mind having a folder for each day. Once the files are moved into their respective "Day" folder, what I'd then like to do is create a SubFolder "!YY!-!MM!" and then move the "!DD!" Folders into the "!YY!-!MM!" folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~a
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
md "!DD!" 2>nul
md "!YY!-!MM!" 2>nul
move "%%a" "!DD!" >nul
)
pause

How to compress each subfolder in a folder into a separate RAR archive using WinRAR?

I am really new to batch file coding and need your help.
I've these directories:
c:\rar\temp1\xy.jpg
c:\rar\temp1\sd.jpg
c:\rar\temp1\dd.jpg
c:\rar\temp2\ss.jpg
c:\rar\temp2\aa.jpg
c:\rar\temp2\sd.jpg
c:\rar\temp3\pp.jpg
c:\rar\temp3\ll.jpg
c:\rar\temp3\kk.jpg
And I want to compress them to this
c:\rar\temp1\temp1.rar
c:\rar\temp2\temp2.rar
c:\rar\temp3\temp3.rar
How could this be done using WinRAR?
This can be done also with WinRAR without using a batch file, not exactly as requested, but similar to what is wanted.
Start WinRAR and navigate to folder c:\rar\.
Select the folders temp1, temp2 and temp3 and click on button Add in the toolbar.
As archive name specify now the folder for the RAR archives, for example c:\rar\.
Switch to tab Files and check there the option Put each file to separate archive.
Click on button OK.
WinRAR creates now three RAR archives with the file names temp1.rar, temp2.rar and temp3.rar in folder c:\rar\ with each archive containing the appropriate folder with all files and subfolders.
The list of files to add can be changed also on tab Files by entering for example *.txt in Files to exclude to ignore text files in the three folders on creating the archives.
And finally it makes sense to enter *.jpg on tab Files in edit field below Files to store without compression as JPEG files usually contain already compressed data and therefore WinRAR cannot really compress the data of the files further.
Here is also a batch file solution to move the files in all non-hidden subfolders of c:\rar\ and their subfolders into an archive file with name of the subfolder created in each subfolder as requested.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RAREXE=Rar.exe"
if exist "%RAREXE%" goto CreateArchives
if exist "%ProgramFiles%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles%\WinRAR\Rar.exe" & goto CreateArchives
if exist "%ProgramFiles(x86)%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles(x86)%\WinRAR\Rar.exe" & goto CreateArchives
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe Rar.exe 2^>nul') do set "RAREXE=%%I" & goto CreateArchives
echo ERROR: Could not find Rar.exe!
echo/
echo Please define the variable RAREXE at top of the batch file
echo "%~f0"
echo with the full qualified file name of the executable Rar.exe.
echo/
pause
goto :EOF
:CreateArchives
set "Error="
for /D %%I in ("c:\rar\*") do (
echo Creating RAR archive for "%%I" ...
"%RAREXE%" m -# -cfg- -ep1 -idq -m3 -msgif;png;jpg;rar;zip -r -s- -tl -y -- "%%I\%%~nxI.rar" "%%I\"
if errorlevel 1 set "Error=1"
)
if defined Error echo/& pause
endlocal
The lines after set "RAREXE=Rar.exe" up to :CreateArchives can be omitted on definition of environment variable RAREXE with correct full qualified file name.
Please read the text file Rar.txt in the WinRAR program files folder for an explanation of RAR command m and the used switches. The question does not contain any information with which options the RAR archives should be created at all.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~f0 ... full name of batch file
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
set /?
setlocal /?
where /?
See also single line with multiple commands using Windows batch file for an explanation of the operator &.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on the three FOR command lines to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg or where command line with using a separate command process started in background.
This script can work as well:
#echo off
for %%a in ("C:\rar\temp1" "C:\rar\temp2" "C:\rar\temp3") do (
pushd "%%~a"
"C:\Program Files\WinRAR\rar.exe" a -r temp.rar *
popd
)
In Python v3.x:
Tested on Python v3.7
Tested on Windows 10 x64
import os
# NOTE: Script is disabled by default, uncomment final line to run for real.
base_dir = "E:\target_dir"
# base_dir = os.getcwd() # Uncomment this to run on the directory the script is in.
# Stage 1: Get list of directories to compress. Top level only.
sub_dirs_raw = [os.path.join(base_dir, o) for o in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, o))]
# Stage 2: Allow us exclude directories we do not want (can omit this entire section if we wish).
dirs = []
for d in sub_dirs_raw:
if "legacy" in d or "legacy_old" in d:
continue # Skip unwanted directories
print(d)
dirs.append(d)
# Stage 3: Compress directories into .rar files.
for d in dirs:
os.chdir(d) # Change to target directory.
# Also adds 3% recovery record using "-rr3" switch.
cmd = f"\"C:\Program Files\\WinRAR\\rar.exe\" a -rr3 -r {d}.rar *"
print(cmd)
# Script is disabled by default, uncomment this next line to execute the command.
# os.system(cmd)
Notes:
Script will do nothing but print commands, unless the final line os.system(cmd) is uncommented by removing the leading # .
Run the script, it will print out the DOS commands that it will execute. When you are happy with the results, uncomment final line to run it for real.
Example: if there was a directory containing three folders mydir1, mydir2, mydir3, it would create three .rar files: mydir1.rar, mydir2.rar, mydir3.rar.
This demo code will skip directories with "legacy" and "legacy_old" in the name. You can update to add your own directories to skip.
To execute the script, install Python 3.x, paste the lines above into script.py, then run the DOS command python script.py from any directory. Set the target directory using the second line. Alternatively, run the script using PyCharm.
This should work it also checks if the files were compressed alright.
You may need to change this part "cd Program Files\WinRAR" depending on where winrar is installed.
#echo Off
Cd\
cd Program Files\WinRAR
rar a -r c:\rar\temp1\temp1.rar c:\rar\temp1\*.jpg c:\rar\temp1\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp2\temp2.rar c:\rar\temp2\*.jpg c:\rar\temp2\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp3\temp3.rar c:\rar\temp3\*.jpg c:\rar\temp3\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
Pause
Below Script will compress each folder as a RAR file within the current directory with very useful info while compressing a large size of data.
#echo off
#for /D %%I in (".\*") do echo started at %date% %time% compressing... "%%I" && #"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"
echo "Completed Successfully !!!"
pause

Windows Batch Lame Encode syntax

I need to get working properly a tiny Windows batch file (convert.cmd) that changes bitrate of all MP3 files in a specified folder. I need to pass 2 parameters to the batch file:
the path to the folder with MP3 files
bitrate I want to change them to.
I am using Lame.exe encoder. The lame.exe is in one place, convert.cmd can be in the same folder with lame.exe, but the folder with MP3 files can be anywhere. The original version (without parameters) was (and it's working fine if I place convert.cmd in the folder wit MP3 files):
#ECHO OFF
FOR %%f IN (*.mp3) DO (
D:\Apps\Lame\lame.exe -h -b 128 "%%f" "%%f.temp"
DEL "%%f"
REN "%%f.temp" "%%f"
)
PAUSE
Instead of 128 I need to pass "%2" and it will be a second command-line parameter, the bitrate, and for MP3 files folder path I need to pass "%1". So, I got this, but it's not working.
#ECHO OFF
FOR %%f IN (%1\*.mp3) DO (
D:\Apps\Lame\lame.exe -h -b %2 "%%f" "%%f.temp"
DEL "%%f"
REN "%%f.temp" "%%f"
)
PAUSE
How to make it work as described?
How can I ensure that my batch file convert existing files, and not creating new converted copies of them somewhere? Thanks a bunch ;) Cheers.
UPDATE
The location of convert.cmd is:
d:\Apps\Lame\convert.cmd, same folder with lame.exe
The location of MP3 files is:
d:\temp\xxx\
File1.mp3
File2.mp3
When I execute convert.cmd from the command line like this:
convert.cmd d:\temp\xxx\ 64
what I get in d:\temp\xxx\ is this:
File1.mp3.temp
File2.mp3.temp
Where did the converted files go?
Thanks.
Thanks, I have already figured out how to write this script.
If someone needs this type of conversion, here we go:
1 param - full path to the folder with mp3 files
2 param - bitrate to convert to
(remember, lame.exe does not preserve mp3 tags)
p.s. who needs mp3 tags anyways ? :)
#ECHO OFF
ECHO.
ECHO BEGIN CONVERSION
ECHO.
CD %1
DIR *.mp3
ECHO -------------------------------------------------------------------------------
ECHO THESE MP3 FILES WILL BE CONVERTED TO BITRATE %2 KBPS
ECHO -------------------------------------------------------------------------------
PAUSE
FOR %%f IN (*.mp3) DO (
ECHO -------------------------------------------------------------------------------
ECHO CONVERTING: %%f
ECHO -------------------------------------------------------------------------------
D:\Apps\Lame\lame.exe -h -b %2 "%%f" "%%~nf.wav"
DEL "%%f"
REN "%%~nf.wav" "%%f"
)
ECHO.
ECHO END CONVERSION
ECHO.
PAUSE

Using 7zip to archive file by year

I have a batch script that runs 7zip and allows me to zip all the files in one folder. I only want to zip files that have a Date Modified of 2010 or another date of my choosing. I want to delete the files after I get them archived into a zip folder. This is my logic.
Find files that are from 2012 and archive those files into a folder called 2012. Zip the folder and delete the files. 0
This is what I have so far.
#ECHO OFF
REM This sets name of the zip file
Set choice=
set /p choice=What is the year of the files?
PAUSE
REM This sets the path of the file
Set path=
set /p path=What is the path of the files on C:\'path'\?
REM Use 7Zip to Zip files in folder c:\path and place the zip archive in C:\path
ECHO.
ECHO Zipping all files in C:\%path% and moving archive to c:\%path%
ECHO.
PAUSE
C:\7z a -tzip "C:\%path%\%choice%.zip" "C:\%path%\*.*" -mx5
ECHO.
PAUSE
ECHO Would you like to Delete the files in orginal folder?
DEL "C:\%path%\*.*"
PAUSE
ECHO File is now zipped
Seems pretty straight forward. By the way, I'm not familiar with 7zip's command-line switches. I'm taking it for granted that you already have the syntax of 7z.exe the way it needs to be.
#echo off
set 7z=C:\7z.exe
if not exist %7z% set /p 7z="Path to 7-zip executable? "
set /p year="What year do you wish to archive? "
set /p dir="What is the path of the files you wish to archive? "
mkdir "%year%"
rem output of dir is date time am/pm size filename
rem filename = 5th + additional tokens
for /f "tokens=5*" %%I in ('dir "%dir%" ^| find "/%year%"') do move "%dir%\%%I %%J" "%year%"
%7z% a -tzip "%dir%\%year%.zip" "%year%" -mx5
set /p I="I'm about to delete the %year% folder. Hit Ctrl-C if this is a bad idea, or hit Enter to continue."
rmdir /q /s "%year%"
echo Done. w00p.
I think what you were missing is the for loop. As I've written it, it performs a directory listing, disregards anything not matching the year as the user enters it, then moves token 5* to the %year% directory for zipping.

Resources