Thrax
Thrax

Reputation: 1964

Forfiles command : Use slashes instead of backslashes in directory path

My script is used to remove log files older than n days. It is called in a specific context (Spring batch) which has previously initialized several environment variables.

Here's the content of the script :

set DAYS = 3
forfiles /p %LOG_PATH% /s /d -%DAYS % /m *.* /c "cmd /c del @FILE"

As you can see, I have no control of the value of LOG_PATH. It is initialized elsewhere and contains a path with slashes (instead of backslashes) such as this : D:/DATA/LOGS

When I launch this script I get, this error : The directory name is invalid.

If I manually write the path with backslashes, the command is successful.

So, would it be possible to espace the environment variable so that the command accepts slashes? Or how would you proceed to replace at runtime the slashes by backslashes?

Thanks.

Upvotes: 1

Views: 885

Answers (3)

MC ND
MC ND

Reputation: 70933

I would try with

set "DAYS=3"
forfiles /p "%LOG_PATH:/=\%." /s /d -%DAYS% /m *.* /c "cmd /c del @PATH"

Where

  • %LOG_PATH:/=\% will do the slash to backslash replacement.
  • Additional quotes are included to handle problems with spaces or special characters.
  • There is an additional dot at the end to avoid problems if the variable contains an ending slash, that will be converted to a backslash and will escape the quote.
  • Variable %DAYS% definition changed to remove spaces in name and value
  • forfiles /s asks for recursive search, and @FILE will only retrieve the name and extension of the file, so it is changed to @PATH to ensure the code will only remove found files out of current active directory.

If you don't have control on the contents of %LOG_PATH% and don't know if the variable contains quotes, quoting again the variable can lead to more problems. A safer version could be

set "DAYS=3"
for %%a in ("%LOG_PATH:"=%.") do (
    forfiles /p "%%~fa" /s /d -%DAYS% /m *.* /c "cmd /c del @PATH"
)

Where the quotes in the variable (if present) are removed and the path corrected when requesting the full path to the element referenced by the for command

Upvotes: 5

JosefZ
JosefZ

Reputation: 30123

Variable Edit/Replace

==> set "LOG_PATH=D:/DATA/LOGS"

==> echo %LOG_PATH%      %LOG_PATH:/=\%
D:/DATA/LOGS      D:\DATA\LOGS

==>

Some mistakes:

          v  this space
set DAYS = 3                                                
forfiles /p %LOG_PATH% /s /d -%DAYS % /m *.* /c "cmd /c del @FILE"
                          ^^^^^^^^^^^                       ^^^^^  
              results to  /d - 3                            use fully qualified @PATH
                              ^  this space                 

Hence, use (quotes and trailing dot in "%LOG_PATH:/=\%." by virtue of MC ND)

set "DAYS=3"
forfiles /p "%LOG_PATH:/=\%." /s /d -%DAYS% /m *.* /c "cmd /c del @PATH"

Upvotes: 2

Paul
Paul

Reputation: 2710

Try variable search/replace like:

set DAYS = 3
forfiles /p %LOG_PATH:/=\% /s /d -%DAYS % /m *.* /c "cmd /c del @FILE"

Upvotes: 1

Related Questions