Forfiles Batch Script (Escaping @ character)

后端 未结 8 938
闹比i
闹比i 2020-12-15 05:35

I\'m working on a batch script that will let me delete files older then a set period using forfiles. For now, I\'m aiming at printing the files that will be deleted.

相关标签:
8条回答
  • 2020-12-15 06:21
    dir *.* > C:\path\dummy%date:~4,2%%date:~7,2%%date:~10,4%.DAT
    
    dir *.* > C:\path\dummy%date:~4,2%%date:~7,2%%date:~10,4%.csv
    
    forfiles -p C:\path\ -m *.DAT /d -50 /c "cmd /c del /Q @path"
    forfiles -p C:\path\ -m *.csv /d -50 /c "cmd /c del /Q @path"
    
    1. Replace the .dat and .csv files what u want to delete.

    2. -50 delete older then 50 days

    3. This is the windows batch file

    0 讨论(0)
  • 2020-12-15 06:23

    DID NOT WORK

    FORFILES /P %deletepath% /M *.%extension% /D -%days% /C "cmd /c del @PATH"

    DID WORK

    FORFILES /P %deletepath% /M *.%extension% /D -%days% /C "cmd /c del @path"

    @path in lower case works.

    After searching everywhere I came across the answer in my own testing. Using the latest version on server 2012 R2 I tried changing the @PATH to lower case. This fixed it for me.

    Good luck!

    0 讨论(0)
  • 2020-12-15 06:24

    Put forfiles.exe to get it to work right, otherwise it will not pass the @variables when you use a batch file. Forfiles will work if you are at the command prompt, but when you run it in a batch file the variables don't work right unless you put: forfiles.exe.

    Here is an example that deletes some txt files older than 30 days forfiles.exe /P c:\directory\ /M *.txt /C "cmd /c del @path" /d -30

    0 讨论(0)
  • 2020-12-15 06:25

    I found there are two versions of FORFILES, one is 1998 version (thanks to Emmanuel Boersma), and the other one is 2005 version (modified date time show it).

    FORFILES v 1.1 - by Emmanuel Boersma - 4/98
    
    Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-dDDMMYY] [-s]
    
     -pPath             Path where to start searching
     -mSearch Mask      Search files according to <Search Mask>
     -cCommand          Command to execute on each file(s)
     -d[+|-][DDMMYY|DD] Select files with date >= or <=DDMMYY (UTC)
                        or files having date >= or <= (current date - DD days)
     -s                 Recurse directories
     -v                 Verbose mode
    
    The following variables can be used in Command :
     @FILE, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE, @FTIME
    
    Default : <Directory : .> <Search Mask : *.*>  <Command : "CMD /C Echo @FILE">
    Examples :
    FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo @FILE is a batch file"
    FORFILES -pc:\ -s -m*.* -c"CMD /C if @ISDIR==TRUE echo @FILE is a directory"
    FORFILES -pc:\ -s -m*.* -d-100 -c"CMD /C Echo @FILE : date >= 100 days"
    FORFILES -pc:\ -s -m*.* -d-010193 -c"CMD /C Echo @FILE is quite old!"
    

    Each version have their unique syntax.

    FORFILES [/P pathname] [/M searchmask] [/S]
             [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]
    
    Description:
        Selects a file (or set of files) and executes a
        command on that file. This is helpful for batch jobs.
    
    Parameter List:
        /P    pathname      Indicates the path to start searching.
                            The default folder is the current working
                            directory (.).
    
        /M    searchmask    Searches files according to a searchmask.
                            The default searchmask is '*' .
    
        /S                  Instructs forfiles to recurse into
                            subdirectories. Like "DIR /S".
    
        /C    command       Indicates the command to execute for each file.
                            Command strings should be wrapped in double
                            quotes.
    
                            The default command is "cmd /c echo @file".
    
                            The following variables can be used in the
                            command string:
                            @file    - returns the name of the file.
                            @fname   - returns the file name without
                                       extension.
                            @ext     - returns only the extension of the
                                       file.
                            @path    - returns the full path of the file.
                            @relpath - returns the relative path of the
                                       file.
                            @isdir   - returns "TRUE" if a file type is
                                       a directory, and "FALSE" for files.
                            @fsize   - returns the size of the file in
                                       bytes.
                            @fdate   - returns the last modified date of the
                                       file.
                            @ftime   - returns the last modified time of the
                                       file.
    
                            To include special characters in the command
                            line, use the hexadecimal code for the character
                            in 0xHH format (ex. 0x09 for tab). Internal
                            CMD.exe commands should be preceded with
                            "cmd /c".
    
        /D    date          Selects files with a last modified date greater
                            than or equal to (+), or less than or equal to
                            (-), the specified date using the
                            "MM/dd/yyyy" format; or selects files with a
                            last modified date greater than or equal to (+)
                            the current date plus "dd" days, or less than or
                            equal to (-) the current date minus "dd" days. A
                            valid "dd" number of days can be any number in
                            the range of 0 - 32768.
                            "+" is taken as default sign if not specified.
    
        /?                  Displays this help message.
    
    Examples:
        FORFILES /?
        FORFILES
        FORFILES /P C:\WINDOWS /S /M DNS*.*
        FORFILES /S /M *.txt /C "cmd /c type @file | more"
        FORFILES /P C:\ /S /M *.bat
        FORFILES /D -30 /M *.exe
                 /C "cmd /c echo @path 0x09 was changed 30 days ago"
        FORFILES /D 01/01/2001
                 /C "cmd /c echo @fname is new since Jan 1st 2001"
        FORFILES /D +3/19/2012 /C "cmd /c echo @fname is new today"
        FORFILES /M *.exe /D +1
        FORFILES /S /M *.doc /C "cmd /c echo @fsize"
        FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
    

    Have a nice time making "Batch File" more sophisticated. :)

    0 讨论(0)
  • 2020-12-15 06:26

    Try trimming the trailing \ from your /P path. Then you should be able to use quotes to encapsulate a path that includes a space.

    0 讨论(0)
  • 2020-12-15 06:29

    This an old question but I've got a different answer... in case anyone needs it.

    When using 'forfiles', the path (written after /p) CAN be between quotation marks. However, it must not end with a slash.

    If you want to run 'forfiles' for the root directory of a drive:
    forfiles /p "C:" /c "cmd /c echo @file"

    If you want to process files in a different directory...
    forfiles /p "C:\Program Files" /c "cmd /c echo @file"

    In other words, the safest approach is:

    • Always use quotation marks (because folders with spaces, like 'Program Files', will still work)
    • Always omit the last trailing slash

    forfiles /p "C:\Path\Without\Trailing\Slash"

    0 讨论(0)
提交回复
热议问题