zip file to unzip and rename files and zip back - zip

I need help in creating a bat file that enables me to the following automatically.
I have a ZIp file by name abc.zip and it contains files-test.txt,dec.drl,tes.txt .What i need is that first it needs to unzip the file and then rename file names to parent zip file like abc.txt,abc.drl,abc.txt.
and then finally it need to zip it back.
Any inputs are highly appreciated.
Thanks in advance.
Best Regards,
Sekhar

OP, below is the code that should accomplish what you're trying to do. Copy and paste the code block into your favorite editor (notepad-mod, notepad++, programmer notepad, etc.) and save it as a batch file. I have included comment for each steps but if you still have a problem understanding certain command please refer to the link posted by #Alexej Magura for more information (especially variable expansion in the loop).
Note: I used 7zip instead of winzip. You can replace 7zip command with appropriate winzip command. This script was tested on Windows 7.
:: # creating test files
echo hello>files-test.txt
echo world>dec.drl
echo !!!!!>test.txt
:: # showing you the directory listing
dir
pause
:: # creating test zip file (abc.zip)
7z a -tzip abc files-test.txt
7z a -tzip abc dec.drl
7z a -tzip abc test.txt
:: # showing you the directory listing
dir
:: # showing you the content inside the zip file
7z l abc.zip
pause
:: # Finished prepping your scenario (abc.zip with 3 files inside)
:: # Core Logic
:: # Looping through all the zips
for %%c in (*.zip) do (
:: # Make a temporary folder with the same name as zip to house the zip content
if not exist %%~nc md %%~nc
:: # Extracting zip content into the temporary folder
7z e -o%%~nc %%c
if exist %%~nc (
:: # Jump into the temporary folder
pushd %%~nc
if exist *.* (
:: Loop through all the files found in the temporary folder and prefix it with the zip's name
for %%i in (*.*) do (
ren %%i %%~nc.%%i
)
:: # Zip all the files with the zip prefix with orginal zip name but with a number 2 (abc2.zip)
if exist %%~nc.* (
7z a -tzip %%~nc2 %%~nc.*
)
:: # Move the new zip back out of the tempory folder
if exist %%~nc2.zip move %%~nc2.zip ..
)
:: # Jump out of the temporary folder
popd
:: # Showing you the directory listing
dir
:: # Showing you the content inside the new zip
7z l %%~nc2.zip
:: # Remove the temporary folder (Clean up)
rd /s/q %%~nc
)
)

Related

grab a number after file extension

I have gigabytes of files with the following naming convention:
H__Flights_SCP_Log_Analysis_Log_Store_Extracted_File_Store_Aircraft_023_Logs_06Apr2021_164418_dtd_slotb_MDN_Gateway_Logs_audit_audit.log.1
The number at the end (1) is important and I need to grab it. Currently my code looks like this:
#!/bin/bash
#this grabs all the log files in the folder that will be converted and places it in a variable
files=$(find ./ -name "*.log*")
#I iterate through each file in files to convert each one
for file in $files
do
#this grabs the file name except the file extension and places it in a variable
name=${file%.*}
#this converts the file and places it in a file with the same name plus a csv extension
ausearch -if "file" --format csv>>$name.csv
done
This works fine to convert the logs and name them except that it does not grab the number at the end of the file extension. How could I grab that?
Continuing with OPs current use of parameter substitution ...
$ file='H__Flights_SCP_Log_Analysis_Log_Store_Extracted_File_Store_Aircraft_023_Logs_06Apr2021_164418_dtd_slotb_MDN_Gateway_Logs_audit_audit.log.1'
$ mynum="${file##*.}"
$ echo "${mynum}"
1
# or
$ mynum="${file//*./}"
$ echo "${mynum}"
1

Pass list of file paths to a batch file to be copied from Location A to Location B

Beginner to batch files and running commands in cmd or PowerShell. Need help in writing a script and/or producing a batch file that will allow me to do the following:
1) Pull list (List1) of file paths (including the file name and where the file is saved currently) from ColumnA of Excel123.xslx and a list (List2) of new destination file paths from ColumnB of Excel123.xlsx (if needed, the file with the old path and destination path can be in CSV or any txt format)
2) Pass through the file paths from List1 and copy the file from the location in List1 to the new destination location in List2
In fewer words, I need to copy a file from the current location in List1 to the new destination location in List2. And, if the new destination location includes folders and sub-folders that do not exist yet, they need to be created in the process.
Any advice/insight? Thank you!
I believe you are looking to essentially do as follows:
#(SETLOCAL
ECHO OFF
SET "_CSV_File=C:\Some\Path\FileList.csv"
)
FOR /F "Tokens=1-2 Delims=," %%A IN ('
Type %_CSV_File% ') DO (
ECHO: Copying FROM: "%%~A" TO: "%%~B"
COPY /B /V /Y /Z "%%~A" "%%~B"
)
ENDLOCAL

what is Batch file (.bat) equivalent for if [-f $path ] [duplicate]

I have to create a .BAT file that does this:
If C:\myprogram\sync\data.handler exists, exit;
If C:\myprogram\html\data.sql does not exist, exit;
In C:\myprogram\sync\ delete all files and folders except (test, test3 and test2)
Copy C:\myprogram\html\data.sql to C:\myprogram\sync\
Call other batch file with option sync.bat myprogram.ini.
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You can use IF EXIST to check for a file:
IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
If you do not need an "else", you can do something like this:
set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
Here's a working example of searching for a file or a folder:
REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y means:
/C Continues copying even if errors occur.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/H Copies hidden and system files also.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
`To see all the commands type`xcopy /? in cmd
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
Path/sync.bat
Path/myprogram.ini
If it was in the Bash environment it was easy for me, but I do not
know how to test if a file or folder exists and if it is a file or
folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this:
Type IF /? to get help about if, it clearly explains how to use IF EXIST.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini

move files name in excel to a specific folder

I am trying to write a batch file that reads from excel file. then move them to a specique folder.
lets say in the excel , there are:
111.txt
222.txt
333.txt
512.txt
I want a batch file that move files into a specific directory that have the above names.
how can I achieve that ?
(I am using xp , and I the directory is D:)
cd /d "d:\files"
for /f %%i in ("C:\where the file resides\file.csv") do (
echo move "%%i" "D:\New\"
)
remove the echo if output seems ok

How to add files/folders to multiple zip files at once with WinRAR?

Is there any way to add files/folders to multiple zip files at once. For example:
Updates:
folder1
folder2
file1
file2
Archives:
archive1.zip
archive2.zip
archive3.zip
I need to add updates to all archives without opening all of them and pasting updates in there.
Any way to do this?
Or is there another archive program that can do this?
This can be done using a batch file and for example WinRAR.
#echo off
if not exist archive*.zip (
echo There are no archive*.zip files to update in
echo.
echo %CD%
echo.
pause
goto :EOF
)
set "ErrorCount=0"
for %%I in (archive*.zip) do (
"%ProgramFiles%\WinRAR\WinRar.exe" u -afzip -cfg- -ep1 -ibck -inul -r -y "%%I" folder1 folder2 file1 file2
if errorlevel 1 (
echo Error on updating %%I
set /A ErrorCount+=1
)
)
if not "%ErrorCount%" == "0" (
echo.
pause
)
set "ErrorCount="
For each archive*.zip file WinRAR is called to update the ZIP file with the two folders and the two files.
The batch processing finishes without printing any message and without pausing if all ZIP files found could be updated successfully. Otherwise the batch file outputs which ZIP file could not be updated for example because of read-only attribute set, and pauses the batch processing before finishing so that the user can read the error message(s).
For details on the WinRAR command u and the used switches open in WinRAR from menu Help the Help topics, open on tab Contents the item Command line mode and read at least the help pages:
Command line syntax
Commands - Alphabetic commands list
Switches - Alphabetic switches list
For understanding the other 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.
echo /?
for /?
goto /?
if /?
pause /?
set /?
You can do it by using 7zip command line version
7z a archive1.zip dir\ -v10k -v15k -v2m #listfile.txt
this will do archive all files and folder is in that folder named 'dir'
and first volume will be 10kb, second will be 15kb and others will be 2mb each except last
and you can use a file that has list of all files named.

Resources