Is there away to only display files in a folder called "Export", when such a subfolder is in multiple locations?
This will display all .xlsx
files below where I'm running the batch file from, however I don't want to display all, I only want to display things in a folder called "Export":
forfiles -p "%~dp0\" -s -m *.xlsx -d -365 -c "cmd /c ECHO @relpath"
I have attempted things like:
forfiles -p "%~dp0\*\Export" -s -m *.xlsx -d -365 -c "cmd /c ECHO @relpath"
However it doesn't recognise syntax like this. Says invalid argument/option
, but they are valid until I add *\
to the path.
This is an example of the structure I'm working with and what I what results to display:
%~dp0\1\Exports\Excel\ - (Do display .xlsx files) %~dp0\1\Do Not Delete\Excel\ - (Don't display .xlsx files) %~dp0\2\Exports\Excel\ - (Do display .xlsx files) %~dp0\2\Do Not Delete\Excel\ - (Don't display .xlsx files)
The number folders would be a variable somehow, which is why the *\
is in my attempt.
I will then edit this to delete the files when I know it's picking up the right ones.
You could use two nested forfiles
loops:
forfiles /S /P "%~dp0\" /M "Export*" /C "cmd /C if @isdir==TRUE forfiles /P @path\Excel /M *.xlsx /D -365 /C 0x22cmd /C echo 00x7840path0x22"
This command line walks through the given root directory (%~dp0
) recursively and iterates through all items matching Export*
(you might replace this pattern by Export
or Exports
, depending on your needs); the hierarchy depth is not checked in order to avoid the need of three nested loops. Anyway, if the iterated item is a directory, the sub-directory Excel
is enumerated and searched for items matching *.xlsx
.
The above command line may display many error messages like ERROR: The specified directory does not exist.
or ERROR: Files of type "*.xlsx" not found.
, depending on your data; to avoid them, redirect STDERR to the nul
device:
2> nul forfiles /S /P "%~dp0\" /M "Export*" /C "cmd /C if @isdir==TRUE forfiles /P @path\Excel /M *.xlsx /D -365 /C 0x22cmd /C echo 00x7840path0x22"
The forfiles
command returns an empty line to STDOUT, so the above command line will contain many of them; to avoid such too, redirect STDERR and STDOUT to the nul
device and redirect only the wished data to the console con
:
1> nul 2>&1 forfiles /S /P "%~dp0\" /M "Export*" /C "cmd /C if @isdir==TRUE forfiles /P @path\Excel /M *.xlsx /D -365 /C 0x22cmd /C 1> con echo 00x7840path0x22"