none
Can I delete *.xls but NOT *.xlsx from the same directory? RRS feed

Answers

All replies

  • Depends on what you want to achieve there.

    DEL command deletes all xls files, while just right after the REN command renames any xlsx extensions to xls. So if you run this batch for the second time it deletes everything.

    So what's the point or renaming xlsx to xls?

    If you remove the line with REN command then the script will be deleting only xls files every time.

    Friday, May 8, 2015 12:48 PM
  • Thanks for the reply! That'a a long story. Basically my program creates 1000's of .xls and .xlsx files. The .xlsx files cannot be opened until they are renamed to .xls. And the original .xls files don't contain all the data, the .xlsx files do.

    So if i can just delete all the .xls files and rename the .xlsx files to .xls everything works. Once I get this batch file to work i can just schedule the crons accordingly.

    I think when I converted the program to Access 2007 from 2003 it started creating the .xlsx files not sure really. But the fact that the program is generating corrupt .xlsx files is another issue altogether.
    • Edited by JasonAR1 Friday, May 8, 2015 1:07 PM
    Friday, May 8, 2015 12:54 PM
  • How about a "workaround"?

    1. move all xlsx files (with good content) to different folder

    MOVE D:\Dir1\*.xlsx D:\Dir2

    2. rename all xlsx files in that folder to xls

    REN D:\Dir2\*.xlsx *.xls

    3. eventually delete everything you have in source folder

    DEL D:\Dir1\* /Q

    This way you will have always a files with the good content and proper extensions in second directory. Though this may be a rather not the most elegant solution, but should work ;]





    • Edited by Andrew.Fr Friday, May 8, 2015 1:17 PM
    Friday, May 8, 2015 1:15 PM
  • dir *.xls
    dir *.xlsx

    They return different things.

    del *.xls  will not delete *.xlsx file.

    Try it.  You will see that you created a problem that doesn't exist.

    DO not use quotes on *.xls


    \_(ツ)_/


    • Edited by jrv Friday, May 8, 2015 1:22 PM
    Friday, May 8, 2015 1:21 PM
  • This deletes .xlsx files

    cd \data\product\xlsx\
    DEL *.xls
    EXIT

    Friday, May 8, 2015 1:27 PM
  • What you did there is with CD command you changed working directory to that where good renamed files are stored and with another command you deleted them there.

    My advice: Try not to use CD command here as it makes hard to track what your working directory for particular command is. Just use commands with absolute paths to directories which I posted above then you should be fine.



    • Edited by Andrew.Fr Friday, May 8, 2015 1:57 PM
    • Proposed as answer by jrv Friday, May 8, 2015 2:56 PM
    • Unproposed as answer by Bill_StewartModerator Friday, May 8, 2015 3:08 PM
    Friday, May 8, 2015 1:56 PM
  • Yeah thanks for the help, looks like I'm going with your workaround for now. It's odd that this simple task can't be done in windows...

    REM ALL *.xls NOT *.xlsx or something...

    • Edited by JasonAR1 Friday, May 8, 2015 2:04 PM
    Friday, May 8, 2015 2:02 PM
  • Use PowerShell. Get-ChildItem has -Include and -Exclude to specify what files you want to include and exclude. Once the output is correct, pipe to Remove-Item.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 8, 2015 2:45 PM
    Moderator
  • Oh nice! forfiles thanks!
    Friday, May 8, 2015 2:48 PM
  • This deletes .xlsx files

    cd \data\product\xlsx\
    DEL *.xls
    EXIT

    It absolutely does not delete xlsx files.  You are making a mistake and not seeing it,


    \_(ツ)_/

    Friday, May 8, 2015 2:56 PM
  • It absolutely does not delete xlsx files.  You are making a mistake and not seeing it,


    \_(ツ)_/

    jrv - I can reproduce this behavior as well:

    C:\Temp>cd 5-8-2015
    
    C:\Temp\5-8-2015>dir
    
     Directory of C:\Temp\5-8-2015
    
    05/08/2015  10:57 AM    <DIR>          .
    05/08/2015  10:57 AM    <DIR>          ..
    05/08/2015  09:32 AM                 0 test1.xls
    05/08/2015  09:32 AM                 0 test1.xlsx
    05/08/2015  09:32 AM                 0 test2.xls
    05/08/2015  09:32 AM                 0 test2.xlsx
                   4 File(s)              0 bytes
                   2 Dir(s)  361,629,847,552 bytes free
    
    C:\Temp\5-8-2015>del *.xls
    
    C:\Temp\5-8-2015>dir
    
     Directory of C:\Temp\5-8-2015
    
    05/08/2015  10:57 AM    <DIR>          .
    05/08/2015  10:57 AM    <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)  361,629,851,648 bytes free


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, May 8, 2015 2:59 PM
  • Mike I tested that and it does not do what you see.  What version of Windows?  Are you using PowerShell of CMD?

    Run the test from PowerShell and it works correctly.


    \_(ツ)_/


    • Edited by jrv Friday, May 8, 2015 3:02 PM
    Friday, May 8, 2015 3:01 PM
  • This is due to short filename matching.

    Try it in PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 8, 2015 3:05 PM
    Moderator
  • This is due to short filename matching.

    Try it in PowerShell.


    -- Bill Stewart [Bill_Stewart]


    This is also an example of the many reasons why I have not used the command shell for anything for years.  There re too many "gotchas" for to be usable for anything serious.

    \_(ツ)_/

    Friday, May 8, 2015 3:07 PM
  • There is a legacy/backward compatibility reason behind this quirk.

    How did wildcards work in MS-DOS?

    and

    Why does FindFirstFile find short names?


    -- Bill Stewart [Bill_Stewart]

    Friday, May 8, 2015 3:08 PM
    Moderator
  • Mike I tested that and it does not do what you see.  What version of Windows?  Are you using PowerShell of CMD?

    Run the test from PowerShell and it works correctly.


    \_(ツ)_/

    Win7, from a CMD console.

    If I run del in PowerShell (since it's just an alias for Remove-Item) it works as expected. However if I use cmd /c del *.xls the same behavior occurs.


    EDIT:

    This is due to short filename matching.

    Try it in PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Ah, interesting. Thanks Bill.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, May 8, 2015 3:10 PM
  • Ah ok that makes sense ..Powershell it is...Thanks everyone!

    -Jason


    • Edited by JasonAR1 Friday, May 8, 2015 3:15 PM
    Friday, May 8, 2015 3:13 PM
  • In Mike's post there's no Powershell prompt so the commands are executed there in legacy command prompt.

    And the reason while it all behaves differently is that del command in CMD uses short filenames as an input and for this scenario it won't work well. We can check this with dir /X

    w:\1>dir /X
     Volume in drive W is WRK
     Volume Serial Number is 169B-435B
    
     Directory of w:\1
    
    05/08/2015  05:14 PM    <DIR>                       .
    05/08/2015  05:14 PM    <DIR>                       ..
    05/08/2015  02:58 PM                 0 TEXTDO~2.TXT Text Document 1.txt
    05/08/2015  02:58 PM                 0 TEXTDO~1.TXT Text Document 2.txt
    05/08/2015  02:58 PM                 0 TEXTDO~3.TXT Text Document 3.txtx
                   3 File(s)              0 bytes
                   2 Dir(s)  190,600,486,912 bytes free

    And if you type DEL in Powershell that's not an actual command but an alias for Remove-Item :)

    PS W:\1> Get-Alias del
    
    CommandType     Name
    -----------     ----
    Alias           del -> Remove-Item


    • Edited by Andrew.Fr Friday, May 8, 2015 3:18 PM
    Friday, May 8, 2015 3:18 PM
  • Correct, as already noted. The blog posts by Raymond Chen that I posted (see above) give the background for this behavioral quirk. It is by design for backward compatibility. PowerShell doesn't use the same wildcard matching rules, so the easiest answer is "just use PowerShell."

    -- Bill Stewart [Bill_Stewart]

    Friday, May 8, 2015 3:32 PM
    Moderator
  • This deletes .xlsx files

    cd \data\product\xlsx\
    DEL *.xls
    EXIT

    This is an extremely risky way of going about it. What if the current drive does not contain the folder "\data\product\xlsx"? The script would delete all .xls files from the current folder, whatever it may be. Here is a safe way to do the same thing:

    @echo off
    del "C:\data\product\xlsx\*.xls"

    or even better (to avoid error messages):

    if  exist  "C:\data\product\xlsx\*.xls"  del "C:\data\product\xlsx\*.xls"

    You do not need the "exit" statement at the end.

    Friday, May 8, 2015 4:17 PM
  • I setup a scheduled task to run the powershell script ren_xlsx.ps1

    s:
    DEL \data\product\*.xls
    REN \data\product\*.xlsx *.xls



    -Jason

    Friday, May 8, 2015 4:23 PM
  • I setup a scheduled task to run the powershell script ren_xlsx.ps1

    s:
    DEL \data\product\*.xls
    REN \data\product\*.xlsx *.xls



    -Jason


    This script is as dangerous as the previous version. Some people like to take risks.
    Friday, May 8, 2015 4:47 PM
  • If that dir doesn't exist I have much bigger problems, but you're right ill add if exists. Thanks!

    -Jason


    • Edited by JasonAR1 Friday, May 8, 2015 5:24 PM
    Friday, May 8, 2015 5:23 PM
  • If that dir doesn't exist I have much bigger problems, but you're right ill add if exists. Thanks!

    -Jason


    Writing safe and robust code is a habit one acquires quickly after causing one or two diasters. Microsoft recognised this by introducing the "WhatIf" switch in PowerShell.
    Friday, May 8, 2015 5:38 PM
  • for /f %a in ('dir /s /b ^| findstr /E "xls"') do; del %a


    If you find this post helpful, spend a second to vote up. Smoking~~


    • Edited by SuperUzer Sunday, May 10, 2015 5:08 PM
    Sunday, May 10, 2015 5:07 PM