Search code examples
batch-filesubdirectorygetdirectories

How to only access one additional subdirectory and access their files utilizing batch?


I usually program in Java, but for reasons I won't specify I am trying my hand at solving a problem utilizing batch scripting. I have a couple directories that hold sub directories of days. For example I may have a folder called January and inside that folder are folders for each day (1, 2, 3, etc.) and inside those folders are the text files that I need to access. However, this data is constantly updated so I never have a set number of folders. I am wondering how I can access all the 'day' folders through a batch script?

I have been trying to iterate through the directories and figuring out what folders are inside the overall folder (ie: the month folder) by using the /d /r commands in a for loop. However this only gives me the files in the directory.

Some code snippets that I've tried are:

FOR /D /r %%P IN (..\JAN\Processed\) DO (
copy filex.txt ..\JAN\Processed\%%~nxP
/* run a correlation program using the file located in the directory specified with the copy command */
)

I've also tried to use

for /r "..\JAN\Processed\" %%P in (.) do (/*similar to code above*/)

When I run this code, it will access every directory and not just the ones that I want (because I have additional directories in the 'day' folders).

To test my code I have also used the ECHO command a lot, just to see if it's actually finding the directories I need by putting echo %%~nxP, but it doesn't seem to find the directories I want to access. It'll just return ECHO IS ON/OFF. Your help is much appreciated.


Solution

  • Get a for /d to get the subfolders (only depth=1) and a second nested for to get the files in that subfolder:

    @echo off
    for /d %%P in ("..\JAN\Processed\*") do (
      for %%F in ("%%~fP\*") do echo %%~fF
    )