-1

I'm trying to find all files older than 30 days in several directories using this command:

[Directory] && forfiles /d -30 /c "cmd /c echo @path"

The output is a .txt file.

The text file contains the path to the directory: C:\Directory1 C:\Directory2 C:\Directory3 etc

I'm trying to loop through several directories using a text file but I need to provide 2 commands: cd (to change to the directory whose files I need info on) and the actual command to get the information)

If I create a batch file entering the directory names manually I have something like this:

cd "C:Directory1" && forfiles /d -30 /c "cmd /c echo @path"
cd "C:Directory2" && forfiles /d -30 /c "cmd /c echo @path"
cd "C:Directory3" && forfiles /d -30 /c "cmd /c echo @path"

How do I enter the "cd" command at the beggining of the loop, then the directory which is in the txt file and the rest of the command (forfiles /d -30 /c "cmd /c echo @path")

What I have so far is:

for /f "usebackq tokens=*" %%A in ("C:\list.txt") do forfiles /d -30 /c "cmd /c echo @path %%A

Thanks!

HDA
  • 13
  • 5
  • 1
    Why wouldn't you use the `/P` option with `FORFILES`? – Squashman Sep 14 '20 at 19:45
  • The paths would be listed line by line in the txt file I'm trying to loop from. How would I give it the path in the /P option if all the paths are in the txt file? – HDA Sep 14 '20 at 19:59
  • You are already using it in your `ECHO` command. – Squashman Sep 14 '20 at 20:23
  • Thanks! That worked better but I'm not getting the output with the files I need, the ones older than 30 days. The output I'm getting is the same list of directories from the text file plus the ````forfiles /p @path /d -30 /c "cmd /c echo @path```` line. – HDA Sep 14 '20 at 20:54
  • Thanks @Squashman. That was of a lot of help as well! The ````"%%~A"```` fixed the whole thing!!! Everything works as it did when I did the directory names manually! – HDA Sep 14 '20 at 21:29

1 Answers1

1

The FOR command with the /F option is used to read a file and it assigns each iteration of the file to the variable %%A. So use the that variable with the /P option of the FORFILES command.

for /f "usebackq tokens=*" %%A in ("C:\list.txt") do forfiles /P "%%~A" /d -30 /c "cmd /c echo @path"
Squashman
  • 13,649
  • 5
  • 27
  • 36