Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

All Other Software All Other Software
Search Search
Search for:
Tech Support Guy > > >

Solved: Help With Command Line Batch Script


(!)

pma083's Avatar
pma083 pma083 is offline
Junior Member with 5 posts.
THREAD STARTER
 
Join Date: Feb 2009
23-Feb-2009, 01:51 PM #1
Question Solved: Help With Command Line Batch Script
Hi all, i use this script for compress all bak files into the backup folder in separated files with 7zip.
  • for %%I in (*.bak) do 7z a "%%~nI.7z" "%%I"

The thing i have to add here is when finish compressing each file deletes the source .bak file, is this posible?

Thanks a LOT!!!! & Thanks TheOutcast for the original code from here !!!!

Regards,
Squashman's Avatar
Trusted Advisor with 19,645 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
23-Feb-2009, 03:47 PM #2
Code:
for %%I in (*.bak) do (
   7z a "%%~nI.7z" "%%I"
   del %%I
   )
pma083's Avatar
pma083 pma083 is offline
Junior Member with 5 posts.
THREAD STARTER
 
Join Date: Feb 2009
23-Feb-2009, 03:55 PM #3
Thanks Squashman,
I change the code, but i receive this error, "del: WARNING: The system cannot find the file specified." , and the batch finish.
Thanks for your help!!!!
Squashman's Avatar
Trusted Advisor with 19,645 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
23-Feb-2009, 04:00 PM #4
Probably because your filenames have spaces. Need to put that last %%I in quotes. I changed the for loop as well.
Code:
for /F "tokens=*" %%I in (*.bak) do (
   7z a "%%~nI.7z" "%%I"
   del "%%I"
   )
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
24-Feb-2009, 06:19 AM #5
Welcome to TSG!
Wasn't ignoring your email pma083, just haven't had time. Looks like Squashman has got ya all set though.

Those pesky quotes will get ya every time, just noticed in the original script you linked, I forgot to put quotes around both the %_source%\*.* part of the For statement and both instances of %_dest% in the If statement.

Jerry
pma083's Avatar
pma083 pma083 is offline
Junior Member with 5 posts.
THREAD STARTER
 
Join Date: Feb 2009
24-Feb-2009, 09:16 AM #6
Hi Guys Thanks a lot for your answers, still having error, the File names don't have spaces... paste you the full code here and the error, don't know why the del command don't work...

Code:
 
@echo off
setlocal
set _source=C:\Temp\Backups\sumtotal
set _dest=C:\Temp\Backups\sumtotal
set _wrpath=C:\C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
for /f %%I in (%_source%\*.bak) do (7z a "%_dest%\%%~nI.7z" "%%I" del "%%I")
pause
This is the error:
Code:
The system cannot find the file C:\Temp\Backups\sumtotal\*.bak.
Deleting Bak files from: C:\Temp\Backups\sumtotal.
Press any key to continue . . .
If i remove the del command Works...

Maybe something i'm doing wrong....
Thanks a Lot AGAIN!!!!
Pablo
Squashman's Avatar
Trusted Advisor with 19,645 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
24-Feb-2009, 12:22 PM #7
In my example I assumed you were running the batch file from the directory the files were located. It is trying to delete the file from the directory you are located in. If you did a change directory to the source directory before the loop starts it wouldn't have been a problem. So you have to give it the path to the filenames you want to delete because you are not executing the batch file from the source directory.

Code:
 
@echo off
setlocal
set _source=C:\Temp\Backups\sumtotal
set _dest=C:\Temp\Backups\sumtotal
set _wrpath=C:\C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
for /f %%I in (%_source%\*.bak) do (
     7z a "%_dest%\%%~nI.7z" "%%I"
     del "%_source%\%%I"
    )
pause
__________________
.

Last edited by Squashman; 24-Feb-2009 at 12:31 PM..
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
24-Feb-2009, 06:34 PM #8
There are 3 (maybe 4) errors in the file.
  1. _wrpath has C:\ twice.
  2. You added the /F switch to the For loop in my original code. It shouldn't be there
  3. You don't have the _wrpath variable to find the 7z file, so unless you are in the 7zip folder, or it's on your path, it won't work
  4. The lines in Squashman's code are on separate lines for a reason.
    Did you actually put them on one line, or is that just the way it got posted?
    If you want to combine them, you have to use the & symbol.
The main problem is the /F switch. With that switch, wildcards are not allowed in a fileset (they can be used in a command), so it tries to open a file with an * in the name. As that's not a valid character for a file name, it fails, giving this error:
The system cannot find the file C:\Temp\Backups\sumtotal\*.bak.
The next line in the error is not a normal message from the Del command, at least on Win2K, XP, Vista, and Win7.
Do you have an echo statement you didn't post that is displaying this:
Deleting Bak files from: C:\Temp\Backups\sumtotal.

On my system, it never tries to execute the Del command. I'm not sure why it is executing it on yours.
Quote:
Originally Posted by pma083 View Post
Code:
 
@echo off
setlocal
set _source=C:\Temp\Backups\sumtotal
set _dest=C:\Temp\Backups\sumtotal
set _wrpath=C:\C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
for /f %%I in (%_source%\*.bak) do (7z a "%_dest%\%%~nI.7z" "%%I" del "%%I")
pause
So use this:
Code:
@echo off
setlocal
set _source=C:\Temp\Backups\sumtotal
set _dest=C:\Temp\Backups\sumtotal
set _wrpath=C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
for %%I in (%_source%\*.bak) do (
  "%_wrpath%\7z" a "%_dest%\%%~nI.7z" "%%I"
  del "%%I"
)
pause
If you want the For statement all on one line, use this (note the ampersand, and no parentheses):
Code:
@echo off
setlocal
set _source=C:\Temp\Backups\sumtotal
set _dest=C:\Temp\Backups\sumtotal
set _wrpath=C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
for %%I in (%_source%\*.bak) do "%_wrpath%\7z" a "%_dest%\%%~nI.7z" "%%I" & del "%%I"
pause
With either of these, %%I does contain the full path, so you don't need to add it to the Del command.

HTH

Jerry
Squashman's Avatar
Trusted Advisor with 19,645 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
24-Feb-2009, 06:39 PM #9
Quote:
Originally Posted by TheOutcaste View Post
There are 3 (maybe 4) errors in the file.
[LIST=1][*] You added the /F switch to the For loop in my original code. It shouldn't be there
That was my bad. I told him to do that. I don't know what I was thinking with that batch file. I really hosed it up. Ignore everything I posted.

I do something similar with winzip but I create a file list first.
Code:
if exist filelist.txt del filelist.txt
Dir /b /a-d *.cpt > filelist.txt
Echo filelist.txt created.


Echo ----------------
Echo Zipping maildats
Echo ----------------

For /F "tokens=*" %%a IN (filelist.txt) DO start wzzip -x"%%~na.txt" -x"%%~na.zip" "%%~na_maildats.zip" "%%~na.*"
Echo Maildats Zipped!
Pause
cls
Squashman's Avatar
Trusted Advisor with 19,645 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
24-Feb-2009, 07:11 PM #10
Quote:
Originally Posted by TheOutcaste View Post
The main problem is the /F switch. With that switch, wildcards are not allowed in a fileset (they can be used in a command), so it tries to open a file with an * in the name. As that's not a valid character for a file name, it fails, giving this error:
The system cannot find the file C:\Temp\Backups\sumtotal\*.bak.
So they should be able to do this as well.
Code:
FOR /F "tokens=*" %%I IN ('dir /b /a-d %_source%\*.bak')
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
24-Feb-2009, 07:44 PM #11
Quote:
Originally Posted by Squashman View Post
So they should be able to do this as well.
Code:
FOR /F "tokens=*" %%I IN ('dir /b /a-d %_source%\*.bak')
Yup
EDIT: Corrected my For /R examples
Some others:
Code:
For /R %_source% %%I in (*.bak) Do
This is equivalent to
Code:
For /F "tokens=*" %%I IN ('dir /s /b %_source%\*.bak')
It will find all *.bak files in subfolders as well.
And,
Code:
For /R %_source% %%I in (.) Do
is equivalent to
Code:
For /F "tokens=*" %%I IN ('dir /s /b /ad %_source%')
except \. is added to each folder name, which doesn't really change anything, as it just means "Current Folder". Doesn't look so nice in a listing though.
The order displayed is different as well. The For /F with the Dir command lists all top level folders first, then lists subfolders.
The For /R groups the subfolders with the top level folders:
Code:
 C:\Test>For /R "C:\Test" %I In (.) do @Echo %I
C:\Test\.
C:\Test\Folder1\.
C:\Test\Folder1\sub1\.
C:\Test\Folder1\sub2\.
C:\Test\Folder2\.
C:\Test\Folder2\sub1\.
C:\Test\Folder2\sub2\.
C:\Test\Folder3\.
C:\Test\Folder3\sub1\.
C:\Test\Folder3\sub2\.
C:\Test\Folder4\.
C:\Test\Folder4\sub1\.
C:\Test\Folder4\sub2\.

C:\Test>For /F "tokens=*" %I IN ('dir /s /b /ad "c:\Test"') do @Echo %I
c:\Test\Folder1
c:\Test\Folder2
c:\Test\Folder3
c:\Test\Folder4
c:\Test\Folder1\sub1
c:\Test\Folder1\sub2
c:\Test\Folder2\sub1
c:\Test\Folder2\sub2
c:\Test\Folder3\sub1
c:\Test\Folder3\sub2
c:\Test\Folder4\sub1
c:\Test\Folder4\sub2
For example, Dir C:\test\*.txt and Dir C:\test\.\*.txt give the same result.

Last edited by TheOutcaste; 24-Feb-2009 at 10:07 PM.. Reason: Corrected my For /R examples
pma083's Avatar
pma083 pma083 is offline
Junior Member with 5 posts.
THREAD STARTER
 
Join Date: Feb 2009
25-Feb-2009, 06:48 AM #12
Thank you guys for your explanation, I will test today.
Tell you later!
and thanks again!
pma083's Avatar
pma083 pma083 is offline
Junior Member with 5 posts.
THREAD STARTER
 
Join Date: Feb 2009
25-Feb-2009, 10:42 AM #13
You guys are a GENIUS!!! WORKS GREATTT!!!!
THANK YOU VERY VERY MUCH!!!!
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


Tags
7zip, batch files, command line, compression

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑