Batch file that unzips and combines contents - batch-file

I am trying to unzip some folders and then recombine them, however when I run the batch file it get the following error:
'unzip' is not recognized as an internal or external command,
operable program or batch file.
I am guessing I need to point the batch file to where 7-zip is correct? Any insight would be appreciated thanks!
See below for code:
#echo off
cls
:start
:unzip
echo.
echo Unzipping files
echo.
unzip ELMDB-85308-PI003_OVA_2of5.zip
unzip ELMDB-85308-PI003_OVA_3of5.zip
unzip ELMDB-85308-PI003_OVA_4of5.zip
unzip ELMDB-85308-PI003_OVA_5of5.zip
echo.
:combining
echo.
echo Combining parts
echo.
copy /b ELMDB-85308-PI003_OVA_1of4+ELMDB-85308-PI003_OVA_2of4+ELMDB-85308-PI003_OVA_3of4+ELMDB-85308-PI003_OVA_4of4 ELMDB-85308-PI003.OVA
:done
echo.
echo Done!
echo.
pause
:end

Instead of unzip as a command, try 7z -e.
If you copy 7z.exe to some directory that is on your PATH, then you don't need to specify the directory.
You can see your PATH by executing
PATH
from the prompt. This displays a ;-separated list of directories that are searched after the current directory) for executables.
If 7z.exe is not on your PATH, then you'd need
"c:\wherever\it is\7z" -e ....
(more explaining)
If 7z is in your path, then
7Z -e ELMDB-85308-PI003_OVA_2of5.zip
will unzip ELMDB-85308-PI003_OVA_2of5.zip
And you'd then need to add a line for each of the other files (which you have done).
If 7z.exe is not on your path then you need
"C:\Program Files\7-Zip\7z" -e ELMDB-85308-PI003_OVA_2of5.zip
and reproduce that for each of your files.
You can tell that 7z.exe is in your path by executing 7z from the prompt. You will get either a report stating that it's not recognised (ie it's not on your path) or you'll get a commands-and-switches help report (which means it is on your path)
Since the command 7z did not work, then it's not on your path, so you need the other form.
If you were to copy /b C:\Program Files\7-Zip\7z.exe"c:\windows\system32\" then in all probability you'd have 7z.exe on your path and hence you could use the short form.

Related

How to compress each subfolder in a folder into separate RAR archives with rar.exe using a batch script?

I have multiple folders inside N:\working.
N:\working\1
N:\working\2
N:\working\3
I want to RAR compress them all individually on 64-bit Windows.
Here is the code I have so far:
#ECHO OFF
CD N:\working
SET rar_executable=C:\Rar.exe
ECHO Using WinRAR executable: %rar_executable%
ECHO.
%rar_executable% a -r -ep1 -m0 "Test.rar"
pause
It produces the file Test.rar containing all three folders. But I want to archive the three folders into the archive files 1.rar, 2.rar and 3.rar.
That task can be done with a batch file containing only a single command line.
#for /D %%I in ("N:\working\*") do #"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"
The same command line for usage in a command prompt window without using a batch file:
for /D %I in ("N:\working\*") do "%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%I.rar" "%I\"
There is no # left to for as that would be completely useless on running this command line in a command prompt window. # left to Rar.exe with full qualified file name is removed to see the command line output by cmd.exe when a new archive file creation starts.
FOR searches in specified directory N:\working for non-hidden subdirectories and assigns the full qualified directory name to loop variable I before executing Rar.exe for this subdirectory.
Rar.exe adds (a) recursively (-r) all directories and files in the subdirectory found by FOR to a RAR archive file with name of the current subdirectory and file extension .rar created in the specified directory N:\working using best compression (-m5) without any output to console window and ignoring all errors (-inul) which could occur during folder compression. The standard configuration is ignored (-cfg-) on compression. The name of the compressed subdirectory with its path is not added to the RAR archive file (-ep1 and folder to compress ends with a backslash).
Open a command prompt window, run for /? and read the output help explaining option /D (directory search).
Open text file Rar.rxt in program files folder of WinRAR with a double click which is the manual of Rar.exe explaining the command a and the switches used here.
This task could be also done with WinRAR.exe using a profile created before with starting WinRAR.exe with the profile name as only option on the command line stored in a shortcut file. This solution would not open a console window because of WinRAR.exe is a Windows GUI desktop application.
try this script as a .bat file which will compress each folder as a RAR file within the current directory and also will show some additional detail which is very useful while compressing a large size of data.
.Bat File Script
#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
Sample Outout

CMD/FTP to create folder using to today date & connect ftp download into created folder

I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B

How to compress text files of N number sub folder which contains N number text files?

Below is a directory structure where I have to compress all *.txt files in the app* subdirectories into a RAR archive with current date in name with deletion of archived files and folders.
I am totally new on working on command line and writing batch scripts and know only some basics.
Are there any suggestions on how to solve this task using Rar or WinRAR?
E:
Data
Log
makeRar.bat
app1
1.txt
2.txt
3.txt
.
.
.
n.txt
app2
1.txt
2.txt
3.txt
.
.
.
n.txt
app3
1.txt
2.txt
3.txt
.
.
.
n.txt
.
.
.
appn
1.txt
2.txt
3.txt
.
.
.
n.txt
----------- After execution of makeRar.bat file -------------
E:
Data
Log
makeRar.bat
app1
4/1/2016_log.rar
app2
4/1/2016_log.rar
app3
4/1/2016_log.rar
I have an answer for this problem. But this solution is using a static path, and not a dynamic path. So when this batch file is executed, it produces the output exactly as wanted, but only for a specific app folder according to code below.
set loc="C:\Program Files\WinRAR"
cd /D %loc%
rar.exe a -r -agYYYY-MM-DD -df -ep %source%\app1\log_.rar %source%\app1\*.txt
source must be defined with path of source directory.
EDIT:
I found a better solution for this task for your directory structure by iterating two FOR loops where first is for getting current and subdirectories and second for getting files from subdirectories.
set "source=C:\Program Files\WinRAR"
for /R /D %%s in (.\*) do (
echo %%s
for %%F in (%%s\*.txt*) do (
"%source%\rar.exe" a -r -agYYYY-MM-DD -df -ep %%s\log_.rar %%F
)
)
pause
Based on your given information this should work. If RAR.exe is not in a folder in your PATH variable you will need to add that path to the PATH variable or provide the full path to the executable in the batch file.
#echo off
:: Get Date
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
SET "YYYY=%dt:~0,4%"
SET "MM=%dt:~4,2%"
SET "DD=%dt:~6,2%"
FOR /D %%G IN (app*) DO (
PUSHD "%%~G"
rar a %DD%%MM%%YYYY%_log.rar *.txt
del *.txt
POPD
)
There is the text file Rar.txt in program files folder of WinRAR which is the manual for console version Rar.exe. By viewing this file and first select the command to use – here it is a for add files to archive – and then reading from top to bottom about the switches and building the command line according to task requirements while reading makes it very easy to define the command line for compressing files into a RAR archive.
#echo off
for /D %%D in (app*) do (
echo Creating archive for %%D ...
"%ProgramFiles%\WinRAR\Rar.exe" a -agYYYY-MM-DD -cfg- -df -ep -idq -m5 -md4m -r- -s -y "%%D\log_.rar" "%%D\*.txt"
if errorlevel 1 pause
)
You can read about all the switches in text file Rar.txt.
Command PAUSE is only executed if an error occurred output by Rar.exe to console while all other messages are suppressed because of switch -idq.
The file name format for the RAR archives is log_YYYY-MM-DD.rar because this is much better than DD-MM-YYYY_log.rar once you have multiple such RAR archives in a directory because log_YYYY-MM-DD.rar displayed sorted alphabetically according to file name as by default on Windows results in getting the files also automatically sorted by date with oldest at top.
Rar deletes only the text files successfully added to the archive.
It is of course also possible to use WinRAR for compression:
#echo off
for /D %%D in (app*) do (
echo Creating archive for %%D ...
"%ProgramFiles%\WinRAR\WinRar.exe" a -agYYYY-MM-DD -cfg- -df -ep -ibck -m5 -md4m -r- -s -y "%%D\log_.rar" "%%D\*.txt"
)
WinRAR could create also ZIP archives instead of RAR archives which console version Rar does not support.
Rar switch -idq is replaced by WinRAR switch -ibck to run WinRAR minimized to system tray, i.e. in background. Error messages are displayed in a GUI window which is displayed automatically by WinRAR if an error occurs.
For help on WinRAR commands and switches which slightly differ from Rar switches click in WinRAR in menu Help on Help topics and click on tab Contents on item Command line mode and read the linked pages.
For understanding the used Windows 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.
echo /?
for /?
Look how easy it is to archive files using Rar or WinRAR. All really needed is a look into manual respectively help and little knowledge about standard commands of Windows listed on executing help in a command prompt window.
EDIT:
Both batch files above require that the batch file is in directory containing the subdirectories app* and this directory is the current directory. For executing this batch file for example as scheduled task with current directory being %SystemRoot%\System32 the batch code below would be better:
#echo off
for /D %%D in ("%~dp0app*) do (
echo Creating archive for %%~fD ...
"%ProgramFiles%\WinRAR\Rar.exe" a -agYYYY-MM-DD -cfg- -df -ep -idq -m5 -md4m -r- -s -y "%%~fD\log_.rar" "%%~fD\*.txt"
if errorlevel 1 pause
)
The command FOR searches for subdirectories app* now in directory of the batch file independent on what is the current directory. And passed to Rar is now name of archive file with full path and the file name pattern *.txt also with full path instead of using a path relative to current directory.
Well, the line if errorlevel 1 pause should be removed on using this batch file as scheduled task.

windows batch script to unzip a file

I am trying to create a batch file that gets a zipped folder for that particular date (v_date) from the sftp site and then unzip them. The zip file contains five files that are text documents. I have written batch scripts that successfully get the zip file from the remote site and save a copy of it on the local drive. I need to incorporate the unzip part in to my script.
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
echo option batch continue>FTP_File_Get.txt
echo option confirm off>>FTP_File_Get.txt
echo open Target>>FTP_File_Get.txt
echo lcd "M:\Development\Data History\File" >> FTP_File_Get.txt
echo cd /Export/File >> FTP_File_Get.txt
echo get /Export/File/Filename_%v_date%.zip "M:\Development\DataHistory\Filename_%v_date%.zip">>FTP_File_Get.txt
echo exit>>FTP_File_Get.txt
M:\temp\apps\WinSCP\winscp.com/script="M:\Development\SFTPBatchFiles\FTP_File_Get.txt"
del FTP_File_Get.txt
This is my code to UNZIP:
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
cd "M:\Development\Data History\"
::SET v_file="M:\Development\Data History\Filename_%v_date%.zip"
::unzip -o %v_file%
"C:\Program Files\7-Zip\7z.exe" e "Filename_%v_date%.zip"
I need to move the extracted files (6 Files) into their respective folders, Any help is greatly appreciated
To unzip the files you can use this command line:
"C:\Program Files\7-Zip\7z.exe" e "filename.zip"
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Use the above code by saving it to .bat file extension
Remember %userprofile% is for the directory, %programfiles% for the program files set as a variable in windows
hope that helps

bzip2 - Bzipping all files inside folders (Windows)

I have a bzipping tool on my computer, but it only bzips files that are inside the "compress" directory. How would I make it so files inside all directories inside the compress directory are zipped?
Example
compress/image.png goes to compress/image.png.bz2
however
compress/folder/image.png stays as compress/folder/image.png
My batch file is as follows:
#echo off
title bzip
echo bzip
echo All files within /compress will be compressed as a .bz2
echo.
echo Compressing file(s)...
bzip2.exe -z compress/*.*
echo.
echo Compression Completed!
pause
I hope somebody can help me!
Edit:
When running the process with directories inside the compress directory, it says "permission denied".
Use for /r compress %%i in (*) do bzip2.exe "%%i" in your batch file instead of the call to bzip2.exe directly. bzip2 almost certainly doesn't know how to recurse through subfolders -- standard wildcard globbing libs on Windows generally don't.
Run for /? from a Command Prompt to see more about the syntax of the for command. If you want to test the command from a prompt instead of a batch file, use 1 percent sign for the variable instead of 2.

Resources