Reputation: 45
My problem: Every day we send our call recordings over to a third party vendor as one zip file via FTP transfer for an analysis service they provide. This process was automated via .bat file for a good long while until suddenly the transfer started failing.
Our vendor has suggested breaking the one large zip file into several smaller ones to resolve the problem.
Therefore, the goal is to write a .bat file that will take the recording files 500 at a time and zip them. Obviously the script must also zip whatever random quantity of files is left at the end.
I have spent 5 days researching this online and I found a solution posted on stack exchange which appeared to be exactly what I need.
However, I cannot get it to work and would appreciate some assistance in determining what needs to be modified for it to execute correctly.
Here is the script in it's current form:
@echo off
Setlocal EnableDelayedExpansion
:: Folder containing files
set input=C:\Temp\CallMiner\Downloads
:: Folder for zips
set output=C:\Temp\CallMiner\Uploads
:: Temp filename to hold list of 500
set listfile=%temp%\listfile
:: Zip counter
set z=1
:: Files per zip
set n=500
cd /d %input%
if exist %listfile% del %listfile%
set i=0
for %f in (*) do (
echo [!i!] & set /a i+=1
echo %f >> %listfile%
if !i!==%n% (
7z a %output%\!z!.zip @%listfile%
set i=0
set /a !z!+=1
del %listfile%
)
)
:: Process remaining files, if any
if exist %listfile% (
7z a %output%\!z!.zip @%listfile%
del %listfile%
)
Here is the original script posted to answer another user with a similar issue:
@echo off
Setlocal EnableDelayedExpansion
:: Folder containing files
set input=D:\temp
:: Folder for zips
set output=D:\output
:: Temp filename to hold list of 10
set listfile=%temp%\listfile
:: Zip counter
set z=1
:: Files per zip
set n=10
cd /d %input%
if exist %listfile% del %listfile%
set i=0
for %%f in (*.*) do (
set /a i=!i! + 1
echo %%f >> %listfile%
if !i!==%n% (
rar a %output%\!z!.rar @%listfile%
set i=0
set /a z=!z! + 1
del %listfile%
)
)
:: Process remaining files, if any
if exist %listfile% (
rar a %output%\!z!.rar @%listfile%
del %listfile%
)
When I executed the original script having only modified file location, quantity, zip program, and removal of a single % symbol so I could execute it manually and watch the results, I received the error "missing operator" repeatedly (presumably once for each file in the folder). As far as I can tell, that error is referencing this line of code:
set /a i=!i! + 1
Every modification I have made to it has resulted in a different result, none of which are correct.
I cannot say for sure that this is the true cause of the problem, or the only problem and I would really appreciate some assistance from more experienced windows command line users.
Upvotes: 0
Views: 1557
Reputation: 80003
Your problem appears to be here:
for %f in (*) do (
echo [!i!] & set /a i+=1
echo %f >> %listfile%
if !i!==%n% (
7z a %output%\!z!.zip @%listfile%
set i=0
set /a z+=1
del %listfile%
)
)
A for
loop in a batch file requires that the metavariable
(in this case f
) be specified as %%f
.
Replace %f
with %%f
throughout this section of the code and all should work.
Removal (or commenting-out) the @echo off
line will give you a blow-by-blow trace. You can stop the trace (temporarily) by using ctrls (simultaneously) and again to restart. or add in PAUSE
statements.
Upvotes: 1
Reputation: 45
OK, I figured it out. Two things were holding it up. First, I modified the "if" statement to mirror the change I had made to the "for" statement:
for %%f in (*.vox) do (
echo [!i!] & set /a i+=1
echo %%f >> %listfile%
if !i!==%n% (
7z a %output%\!z!.zip @%listfile%
set i=0
echo [!z!] & set /a z+=1
del %listfile%
)
)
Second, I had to relocate 7zip's 7z.exe file. I had followed recommendations that it be added to the user profile to avoid issues. This was not working. I had to add it directly to the source folder where the files to be zipped reside. Fortunately, I only need to zip one file type from that folder, so this is a non-issue for me having the executable there.
Upvotes: 0