开发者

Batch zipper - need to delete the old files

I have a batch script that actually does what i want (Takes in a number of files and puts them in the same directory that they came from as .bz2 files). I need a bit of help though, because i need to delete the original files. In addition, when the input size gets too large (can't give an exact number, probably somewhere around 40 or so) the batch crashes. I tried to solve this myself, unfortunately, batch is gibberish to me. If this could delete the original fi开发者_StackOverflow社区le, I could deal with the current input size. Any help that anyone can give me would be greatly appreciated. Here is my current code:

@Echo Off
:Start
If [%1==[ Goto :EOF
PushD %~pd1
"C:\Program Files\7-Zip\7z.exe" u -tbzip2 %~nx1.bz2 %1
Shift
PopD
Goto Start

I would also like to mention that I am not the original author of this code, it was posted on a forum.


I would make a FOR loop instead.

::start batchfile
@echo off
setlocal
::set local variables

set input=C:\input
set 7zip=C:\Program Files\7-Zip

::The reason I would use this forloop is that when you start your loop,
::you can say for each files in a recursive way do the following: 
::IF (test) do (<command1> <command2>)
::The file that is being processed is called %%i during the whole loop (for that file)
::so at the end of your loop you just 'del <file>' your source

for /f %%i in (%input%\*.bz2) do (
%7zip% u -tbzip2 "%input%\%%i" "%%i"
del "%input%\%%i"
)

goto: eof

I'm not really expert but I hope this helps you on your way


Change the line where the unzipping utility is called like this:

"C:\Program Files\7-Zip\7z.exe" u -tbzip2 %~nx1.bz2 %1 && del %~nx1.bz2

The part in bold means 'delete the file specified if the command to the left of && completed successfully'. So, if for some reason the file wasn't unzipped or was incompletely unzipped, del would not fire and the file would preserve. That is, if you would like it to be like that.

And if Rps's solution suits you well, you may want to change it accordingly.

But before using del I would first try to find out (by running, probably, 7z.exe /? or 7z.exe -?) whether the unzipper utility has a parameter to tell it to delete the archive automatically if unzipped successfully. That way you wouldn't have to implement the feature manually.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜