1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Help With Command Line Batch Script

Discussion in 'All Other Software' started by pma083, Feb 23, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. pma083

    pma083 Thread Starter

    Joined:
    Feb 23, 2009
    Messages:
    5
    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,
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Code:
    for %%I in (*.bak) do (
       7z a "%%~nI.7z" "%%I"
       del %%I
       )
    
     
  3. pma083

    pma083 Thread Starter

    Joined:
    Feb 23, 2009
    Messages:
    5
    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!!!!
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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"
       )
     
  5. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    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
     
  6. pma083

    pma083 Thread Starter

    Joined:
    Feb 23, 2009
    Messages:
    5
    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...:confused:

    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...:confused:

    Maybe something i'm doing wrong....
    Thanks a Lot AGAIN!!!!
    Pablo
     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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
     
  8. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    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.
    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" [COLOR=Blue][B]&[/B][/COLOR] 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
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731

    1. 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
     
  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    So they should be able to do this as well.
    Code:
    FOR /F "tokens=*" %%I IN ('dir /b /a-d %_source%\*.bak')
     
  11. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    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.
     
  12. pma083

    pma083 Thread Starter

    Joined:
    Feb 23, 2009
    Messages:
    5
    Thank you guys for your explanation, I will test today.
    Tell you later!
    and thanks again!
     
  13. pma083

    pma083 Thread Starter

    Joined:
    Feb 23, 2009
    Messages:
    5
    You guys are a GENIUS!!! WORKS GREATTT!!!!
    THANK YOU VERY VERY MUCH!!!!
     
  14. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/803562