Windows cmd cd into newest directory - windows

I have a deployment directory that contains subdirectories, one for each deployment. I'm trying to write a batch script that, among other things, performs a cd into the newest one of these directories.
I know how to do this in bash (has already been ansered here as well), but I don't know how to accomplish the same thing in Windows cmd. Can anyone help me?

In a batch file following lines can be used to changed to the subdirectory with newest modification date:
#echo off
for /F "eol=| delims=" %%I in ('dir * /AD /B /O-D 2^>nul') do cd "%%I" & goto DoneCD
echo No subdirectory found in: "%CD%"
:DoneCD
The command FOR with option /F starts a new command process with %ComSpec% /c and the command line specified between ' as further arguments in background. So executed by FOR is with usual Windows installation path:
C:\Windows\System32\cmd.exe /c dir * /AD /B /O-D 2>nul
DIR executed by background command process searches with the specified arguments
in current directory
for directories because of option /AD (attribute directory)
matching the wildcard pattern * (all)
and outputs
in bare format because of option /B just the directory names without path never enclosed in "
ordered reverse by last modification date because of option /O-D and not using option /TC (creation date) or /TA (last access date) which means first the newest modified directory and last the oldest modified directory.
The output by DIR is written to handle STDOUT of the started background command process.
2>nul redirects the error message output by DIR on not finding any directory in current directory from handle STDERR to device NUL to suppress this error message.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR captures everything written by DIR to handle STDOUT of started command process and processes this output line by line after started cmd.exe terminated itself.
FOR ignores empty lines which do not occur here because of DIR outputs the list of directory names without empty lines because of using /B.
FOR would split up by default a line into substrings (tokens) using normal space and horizontal tab character as delimiters. After this substring splitting is done FOR would by default check if the first substring starts with default end of line character ; in which case the line would be ignored like an empty line. Otherwise FOR would assign first space/tab delimited string to the specified loop variable I and would execute the command line with CD and GOTO.
A directory name could be for example  ;Test Folder, i.e. a directory name starting with a space and a semicolon and containing one more space. Such a directory name would be split up to ;Test (without space at beginning) and Folder and next ignored by FOR because of ;Test starts with a semicolon.
For that reason the end of line character is redefined from default semicolon to a vertical bar with eol=| which is a character no file or folder name can contain according to Microsoft documentation about Naming Files, Paths, and Namespaces. And line splitting behavior is disabled with delims= at end of options argument string after for /F which defines an empty list of delimiters. So the directory name as output by DIR is assigned to loop variable I without any modification even on being a very unusual name for a directory.
FOR executes command CD which changes current directory to the last modified subdirectory of the current directory and next command GOTO is executed to continue the processing of the batch file on the line below the label line :DoneCD. So the FOR loop execution is broken already after processing first directory name with command GOTO.
It is of course possible to use other commands after the FOR command line and the label line :DoneCD than just the ECHO line reporting that no subdirectory was found in current directory as shown by referencing dynamic environment variable CD like a command line to exit batch processing on this unusual use case or error condition case.
This FOR command line with the command GOTO to exit FOR loop after CD cannot be used in a Windows command prompt window. A solution for Windows command prompt window would be:
set "DoneCD=" & (#for /F "eol=| delims=" %I in ('dir * /AD /B /O-D 2^>nul') do #if not defined DoneCD cd "%I" & set "DoneCD=1") & set "DoneCD="
In a batch file this single line with multiple commands would be written as
#set "DoneCD=" & (#for /F "eol=| delims=" %%I in ('dir * /AD /B /O-D 2^>nul') do #if not defined DoneCD cd "%%I" & set "DoneCD=1") & set "DoneCD="
or better readable in its multi-line version with an additional echo as
#echo off
set "DoneCD="
for /F "eol=| delims=" %%I in ('dir * /AD /B /O-D 2^>nul') do (
if not defined DoneCD (
cd "%%I"
set "DoneCD=1"
)
)
if not defined DoneCD echo No subdirectory found in: "%CD%"
set "DoneCD="
First the environment variable DoneCD is deleted if it is defined by chance.
Next FOR runs cmd.exe with DIR as described above and processes the first output directory with newest modification date. The IF condition is true on newest directory as the environment variable was definitely undefined before execution of FOR. So command CD is executed to change the current directory to newest subdirectory. Then the environment variable DoneCD is defined with value 1. Any other value would be also possible like on using set "DoneCD=%%I". Important here is that for the other subdirectories output by DIR the environment variable DoneCD is now defined and so the IF condition is always false. So there is no attempt made to change in current subdirectory of initial current directory into a subdirectory not existing here or existing by chance also in the subdirectory.
Finally the environment variable DoneCD is deleted again if defined at all during execution of FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
echo /?
for /?
goto /?
if /?
set /? ... explaining on last help page dynamic environment variable CD.

Related

How to list the names of all the files and directories in a folder using for loop in a batch file

I want to list all the files and directories inside a directory using a for loop in a batch script. How can I do it?
I used below but it didn't work :
for /r %%I in (".") do ( ls -ltr '%%I') ## Listing only filenames and not directories name
Any help is appreciable.
Thanks!
If you just want a list of dirs and files, recursively, what about:
dir /b/s "."
If you want to do something special with each of the stream item, using a for loop, you could do something like:
for /f "tokens=* delims=" %%i in ('dir /b/s "."') do ( echo "%%i" )
There I used echo for echoing, but you can put whatever you need.
"to list all the files and directories inside a directory using a for loop in a batch script." you should use the DIR command.
If you open a Command Prompt window, type dir /? and press the ENTER key you should see its usage information.
One important thing to note is the /A option. What is not mentioned specifically is that using it alone, (without additional parameters D, R, H, A, S, I, L or O), enables all attributes.
Therefore to list all items in the current directory recursively in bare format you'd use:
DIR /A /B /S
or
DIR . /A /B /S
If you wanted to list them in a specific location relative to the current directory, you'd use:
DIR "Location" /A /B /S
or:
DIR ".\Location" /A /B /S
And For a specific absolute path:
DIR "L:\ocation" /A /B /S
And if you wanted it to be in the same location as the batch file itself, you can use the special variable for the current script %0:
DIR "%~dp0." /A /B /S
To perform that command within a For loop, you should first open a Command Prompt window, type for /? and press the ENTER key, to read its usage information.
You should note that you are running a command, and should therefore use a FOR /F loop, i.e.
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
But should also note that:
To use the FOR command in a batch program, specify %%variable instead of %variable.
So:
FOR /F ["options"] %%variable IN ('command') DO command [command-parameters]
As you have your command already, the options now become important. The first you need to understand is eol which whilst it seems to mean End Of Line, is specific to only one end, the beginning! What this does it does not pass any result of 'command' to the DO if it begins with a single specific character. The defualt for eol is the semicolon ;, (probably because historically it was a common line comment marker in many files). Generally, a file or directory name could include, and begin with a semicolon, so in order to include all files, you would specify a character which cannot be included in a filename, for me the simplest is ?, although I've seen many examples using |. However, when you perform a recursive DIR command, every returned line is a fully qualified path, none of which can begin with a semicolon, so you can for this task ignore eol. You clearly want everything returned, so do not require skip any lines returned. tokens and delimiters, are adjusted according to what you want to do with the results, in this case, you want the entire content of each line returned by your 'command' with no splitting on specific characters. You should note that tokens by default is 1 and delims by default is both the space and a horizontal tab characters. You should stipulate therefore that you do not want any delimiters, so that the first token is everything returned on each line of 'command'. You rarely require the usebackq option, so for the purposes of this answer, and your task, just ignore it.
Now put it all together:
FOR /F "delims=" %%G IN ('DIR "Location" /A /B /S') DO command
Finally you can use your wanted DO command with each result from your parenthesized DIR command. That result will be held within your variable %%G.
For the purposes of just viewing each result, we'll use the ECHO command, (you would just replace that with your chosen command). Please note that as each result of the DIR command is a file or directory name string, you should generally doublequote it.
allObjects.cmd
FOR /F "delims=" %%G IN ('DIR "Location" /A /B /S') DO ECHO "%%G"
Please remember to replace "Location" as needed, before running the Windows Command Script
Create two loops, one for files
for /r %%i in (*.*) do <something>
and one for directories
for /r %%i in (.) do <something>
and use the same command after do
But, since you have Cygwin installed anyway, why not use that power and do
find . | xargs -L1 ls -ltr
where find . finds all files and directories, | xargs passes the output to xargs which -L1 splits the output after each line and passes each line to ls -ltr.

Removing first n characters from folder names

I'm fairly new to batch, my problem is the following:
I have a long list of folders and need to delete the first 3 characters from each of their names. Think 01_Folder1, 02_Folder2, 03_Folder3 and so on. I've tried patching together pieces of CMD commands I've found on the web but could not come up with a script that does what I want it to do. I've even tried using VBScript as I'm more familiar with VB in general but failed to find a solution as well.
Is there an easy way to solve this?
Edit:
Here's my attempt; it's giving me a syntax error but as I am not versed enough in CMD, I cannot really see why:
setlocal enableextensions enabledelayedexpansion
for /d %%i in ("%~dp0*") do (set name=%%i && ren "!name!" "!name:~3!")
endlocal
The FOR command line does not work because of assigned to loop variable i is the name of a directory with full path and so removing the first three characters results in removing drive letter, colon and backslash from path of the directory and not the first three characters from directory name. Further the full qualified directory name is assigned with an additional space to environment variable name because of the space between %%i and operator &&.
One solution would be:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /d %%i in ("%~dp0*") do set "name=%%~nxi" && ren "%%i" "!name:~3!"
endlocal
The disadvantage of this solution is that directory names with one or more exclamation marks in name or path are not processed correct because of enabled delayed expansion resulting in interpreting ! in full directory name as begin/end of a delayed expanded environment variable reference.
Another solution is quite simple with using just the command line:
#for /D %%i in ("%~dp0*_*") do for /F "tokens=1* delims=_" %%j in ("%%~nxi") do #ren "%%i" "%%k"
The outer FOR searches in directory of the batch file for non-hidden subdirectories matching the pattern *_*.
For each directory name assigned with full path to loop variable i one more FOR command is used which processes just the string after last backlash (directory name without path) and splits the string up into substrings (tokens).
The string delimiter is an underscore as defined with option delims=_. The option tokens=1* tells FOR to assign first underscore delimited string to specified loop variable j and everything after one or more underscores after first underscore delimited string to next but one loop variable k according to ASCII table.
The inner FOR would ignore a directory name on which first substring starts with a semicolon as being the default end of line character. But in this case no directory has ; at beginning of its name.
There is one problem remaining with this command line. It does not work on drives with FAT32 or exFAT as file system, just by chance on drives with NTFS file system. The reason is that the list of non-hidden directories changes in file system while the outer FOR iterates over the directory entries matching the pattern.
A better solution loads first the list of directories to rename into memory of Windows command process which is processing the batch file before starting with renaming the directories.
#for /F "delims=" %%i in ('dir "%~dp0*_*" /AD-H /B 2^>nul') do for /F "tokens=1* delims=_" %%j in ("%%i") do #ren "%~dp0%%i" "%%k"
FOR executes in this case in background one more command process with %ComSpec% /c and the command line within ' appended as additional arguments. So executed in background is with Windows installed to C:\Windows:
C:\Windows\System32\cmd.exe /c dir "C:\Batch\File\Path\*_*" /AD-H /B 2>nul
DIR searches in directory of the batch file for
non-hidden directories because of option /AD-H (attribute directory and not hidden)
matching the wildcard pattern *_*
and outputs just the directory names in bare format because of option /B without path to handle STDOUT (standard output) of background command process.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR respectively the Windows command process processing the batch file captures everything written to standard output handle of background command process and starts processing it as described above after started cmd.exe terminated itself. So there is in memory already a list of directory names and so the executed REN command does not result anymore in a changed list of directory names on processing one after the other.
Please note that a directory with name 01__Underscore_at_beginning is renamed to Underscore_at_beginning and not to _Underscore_at_beginning by both single line solutions.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
ren /?
set /?
setlocal /?

Using FOR /R for recursive search only in a subset of folder hierarchy

I want to create a batch file able to apply some processing on each JPG file in a folder hierarchy. The following script file works very well for that case (here I only echo the name of each file, but this should be replaced by some more complex statements in the real application):
:VERSION 1
#echo off
set "basefolder=C:\Base"
for /r %basefolder% %%f in (*.jpg) do echo %%f
Actually, I don't want to explore all the folder hierarchy under %basefolder%, but only a given list of subfolders. This modified script is able to deal with that case :
:VERSION 2
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
pushd %basefolder%\%%~s"
for /r %%f in (*.jpg) do echo %%f
popd
)
Is there a solution to remove the pushd/popd pair of statements, to get something closer to the initial script. I thought that one of the following scripts would do the job:
:VERSION 3
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
for /r %basefolder%\%%~s" %%f in (*.jpg) do echo %%f
)
or, using delayed expansion:
:VERSION 4
#echo off
setlocal enabledelayedexpansion
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
set "folder=%basefolder%\%%~s"
echo !folder!
for /r !folder! %%f in (*.jpg) do echo %%f
)
but none of them is working. When running the second one, the echo !folder! command in the external loop shows C:\Base\A, C:\Base\B and C:\Base\C as expected, but the inner loop doesn't echo any JPG file, so I guess that the recursive for /r command does not run correctly.
What am I doing wrong ?
Final edit after answers :
Thanks to #aschipfl who provided a link to the answer posted by #jeb on another question, quoted below:
The options of FOR, IF and REM are only parsed up to the special character phase. Or better the commands are detected in the special character phase and a different parser is activated then. Therefore it's neither possible to use delayed expansion nor FOR meta-variables in these options.
In other words, my versions 3 and 4 do not work because when defining the root folder of the FOR /R command, neither the %%~s nor the !folder! are correctly expanded by the expression parser. There is no way to change that, as this is a parser limitation. As I said in a comment below: the root folder option in the FOR /R command is basically only syntactic sugar to avoid the use of pushd/popd before and after the command. As this syntactic sugar is incomplete, we have to stick to the original syntax for some specific use cases, as the one presented here. The alternatives proposed by #Gerhard (using a subroutine CALL) or by #Mofi (parsing the result of a DIR command) are working, but they are neither more readable nor more efficient than the simple pushd/popd version I proposed initially.
My Approach for this would be really straight forward:
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do for /R "%basedir%" %%a in ("%%~i\*.jpg") do echo %%~fa
The double quotes inside of the subfolders variable is important here, it will ensure that folder names with whitespace are not seen as separators for the folder names. For instance:
set "subfolders="Folder A","Folder B","Folder C""
Edit
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do call :work "%%~i"
goto :eof
:work
for /R "%basedir%\%~1" %%a in (*.jpg) do echo %%~fa
It is in general not advisable to assign the value of a loop variable to an environment variable and next use the environment variable unmodified without or with concatenation with other strings being coded in batch file or defined already above the FOR loop within body of a FOR loop. That causes just problems as it requires the usage of delayed expansion which results in files and folders with one or more ! are not correct processed anymore inside body of the FOR loop caused by double parsing of the command line before execution, or command call is used on some command lines, or a subroutine is used called with call which makes the processing of the batch file much slower.
I recommend to use this batch file for the task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "basefolder=C:\Base"
set "subfolders=A B C "Subfolder D" SubfolderE"
for %%I in (%subfolders%) do for /F "delims=" %%J in ('dir "%basefolder%\%%~I\*.jpg" /A-D /B /S 2^>nul') do echo %%J
endlocal
The inner FOR loop starts for each subfolder defined in subfolders in background one more command process with %ComSpec% /c and the DIR command line appended as additional arguments. So executed is with Windows installed to C:\Windows for example for the first subfolder:
C:\Windows\System32\cmd.exe /c dir "C:\Base\A\*.jpg" /A-D /B /S 2>nul
The command DIR searches
in specified directory C:\Base\A and all it subdirectories because of option /S
for files because of option /A-D (attribute not directory) including those with hidden attribute set
matching the pattern *.jpg in long or short file name
and outputs to handle STDOUT of background command process just the matching file names because of option /B (bare format)
with full path because of option /S.
The error message output by DIR on nothing found matching these criteria is redirecting from handle STDERR to device NUL to suppress it.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The output to handle STDOUT of background command process is captured by FOR respectively the command process which is processing the batch file. FOR processes the captured output line by line after started cmd.exe terminated itself. This is very often very important. The list of files to process is already in memory of command process before processing the first file name. This is not the case on using for /R as this results in accessing file system, getting first file name of a non-hidden file matching the wildcard pattern, run all commands in body of FOR and accessing the file system once again to get next file name. The for /R approach is problematic if the commands in body of FOR change a file to process like deleting, moving, modifying, copying it in same folder, or renaming a found file because of the entries in file system changes while for /R is iterating over these entries. That can easily result in some files are skipped or some files are processed more than once and it could result also an endless running loop, especially on FAT file system like FAT32 or exFAT. It is never good to iterate over a list of files on which the list changes on each iteration.
Command FOR on usage of /F ignores empty lines which do not occur here. A non-empty line is split up into substrings using a normal space and a horizontal tab as string delimiters by default. This line splitting behavior is not wanted here as there could be full qualified file names containing anywhere inside full name one or more spaces. For that reason delims= is used to define an empty list of delimiters which disables the line splitting behavior.
FOR with option /F would also ignore lines on which first substring starts with ; which is the default end of line character. This is no problem here because of command DIR was used with option /S and so each file name is output with full path which makes it impossible that any file name starts with ;. So the default eol=; can be kept.
FOR with option /F assigns by default just first substring to specified loop variable as tokens=1 is the default. This default can be kept here as splitting the lines (full file names) into substrings is disabled already with delims= and so there is always the full file name assigned to the loop variable.
This example uses just echo %%I to output the file names with full path. But it is now safe to replace this single command by a command block which does more with the JPEG files because of the list of JPEG files for each specified subfolder tree in base folder is always already completely in memory of command process processing the batch file.

How to identify/get the file by its timestamp in a batch file?

I have a list of csv files with date and time appended like "Account_data_yyyymmdd.csv" which are added daily along with its timestamp to source dir .I have to identify latest file ie.'Account_data_2020_08_05.csv' and set the value in variable . so i can pass it as argument
Files in source dir
Account_data_2020_08_05.csv
Account_data_2020_08_04.csv
Account_data_2020_08_03.csv
I have to find the recently placed file based on its timestamp & pass it as input for calling another batch process. Highlighted text is the argument to batch file.How to find latest file based on its timestamp and pass it as argument for
echo "start"
call process.bat "C:\CSVDataLod" AccntDataloadprocess ***"dataAccess.name=C:\SourceDir\ Account_data_%year%_%month%_%date%.csv"***
That's surprisingly easy. Use dir with the /on switch to sort by name (see dir /? for that switch and the others I used, if you are not familiar with them) and put a for /f loop around to capture the output. The following code sets the variable %last% to each line of the output, keeping the last one only:
for /f "delims=" %%a in ('dir /a-d /on /b Account_data_*.csv') do set "last=%%a"
echo %last%
The easiest and fastest method to get name of CSV file with newest date in file name is using command DIR with option /O-N to get the CSV file names output ordered by name in reverse order. The file name with newest name is output first by DIR in this case. The output of DIR has to be captured and processed with FOR. The FOR loop is exited after running the other batch file with first file name output by DIR.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileFound="
set "FileNamePattern=Account_data_20??_??_??.csv"
if /I "%~x1" == ".csv" set "FileNamePattern=%~nx1"
for /F "delims=" %%I in ('dir "C:\SourceDir\%FileNamePattern%" /A-D /B /O-N 2^>nul') do (
echo Processing file %%I ...
call process.bat "C:\CSVDataLod" AccntDataloadprocess "dataAccess.name=C:\SourceDir\%%I"
if /I not "%~1" == "/A" goto EndBatch
set "FileFound=1"
)
if not defined FileFound echo There is no file "%FileNamePattern%" in directory "C:\SourceDir".
:EndBatch
endlocal
I recommend to open a command prompt and run
dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B /O-N
Then you know which lines are processed by FOR. Next run
dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B
dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B /ON
to see how DIR outputs the CSV file names without specifying a specific order resulting in printing the file names as returned by the file system and explicitly ordered by name in alphabetical order instead of reversed alphabetical order.
The file system NTFS returns a list of file names matched by a wildcard pattern in local specific alphabetic order while FAT file systems like FAT16, FAT32, exFAT return the file names not ordered at all. In real all file systems return the file names in order as stored in the table of the file system. The file systems use just different methods on how to add a file name to table of the file system. The FAT file systems append a new file name always at end of the table of a directory while NTFS inserts a new file name in table of a directory using a local specific alphabetic sort algorithm.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.
Edit:
The batch file can be run with /a or /A as argument to process all CSV files matching the wildcard pattern from newest to oldest instead of just the newest. The batch file can be also run with name of a .csv file in source directory to process this specific CSV file instead of the newest CSV file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
dir /?
echo /?
endlocal /?
for /?
goto /?
setlocal /?

Check if there are any folders in a directory

CMD. How do I check if a directory contains a folder/s (name is not specified)? Other files are ignored.
If this was in the case of any .txt file, it would kind of look like this :
if exist * .txt
How do I do it with "any" folder?
There are multiple solutions to check if a directory contains subdirectories.
In all solutions below the folder for temporary files referenced with %TEMP% is used as an example.
Solution 1 using FOR /D:
#echo off
set "FolderCount=0"
for /D %%I in ("%TEMP%\*") do set /A FolderCount+=1
if %FolderCount% == 0 (
echo Temporary files folder has no non-hidden subfolder.
) else if %FolderCount% == 1 (
echo Temporary files folder has one non-hidden subfolder.
) else (
echo Temporary files folder has %FolderCount% non-hidden subfolders.
)
pause
The problem with this solution is that FOR with option /D to search for directories matching the wildcard pattern * in specified directory for temporary files ignores the directories with hidden attribute set. For that reason the command SET with the arithmetic expression to increment the value of environment variable FolderCount by one on each each directory is not executed for a directory with hidden attribute set.
The short version of this solution without counting the folders:
#echo off
for /D %%I in ("%TEMP%\*") do goto HasFolders
echo Temporary files folder has no non-hidden subfolder.
goto EndBatch
:HasFolders
echo Temporary files folder has non-hidden subfolders.
:EndBatch
pause
The loop is exited with command GOTO on FOR has assigned first name of a non-hidden directory to the loop variable.
Solution 2 using FOR /F and DIR:
#echo off
set "FolderCount=0"
for /F "eol=| delims=" %%I in ('dir "%TEMP%" /AD /B 2^>nul') do set /A FolderCount+=1
if %FolderCount% == 0 (
echo Temporary files folder has no subfolder.
) else if %FolderCount% == 1 (
echo Temporary files folder has one subfolder.
) else (
echo Temporary files folder has %FolderCount% subfolders.
)
pause
FOR with option /F and a set enclosed in ' results in starting in background one more command process with %ComSpec% /c and the command line within ' appended as additional arguments. So executed is with Windows installed to C:\Windows:
C:\Windows\System32\cmd.exe /c dir "C:\Users\UserName\AppData\Local\Temp" /AD /B 2>nul
DIR executed by background command process searches
in specified directory for temporary files
just for directories because of option /AD (attribute directory)
with including also directories with hidden attribute set because of option /AD overrides the default /A-H (all attributes except attribute hidden)
and outputs them in bare format because of option /B which results in ignoring the standard directories . (current directory) and .. (parent directory) and printing just the directory names without path.
The output of DIR is written to handle STDOUT (standard output) of the started background command process. There is nothing output if the there is no subdirectory in the specified directory.
There is an error message output to handle STDERR (standard error) of background command process if the specified directory does not exist at all. This error message would be redirected by the command process executing the batch file to own STDERR handle and would be output in console window. For that reason 2>nul is appended to the DIR command line to suppress the error message in background command process by redirecting it from handle STDERR to device NUL.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR with option /F captures the output written to handle STDOUT of started background command process and processes the output line by line after started cmd.exe terminated itself after finishing execution of internal command DIR.
Empty lines are ignored by default by FOR which do not occur here.
FOR would split up the line by default into substrings using normal space and horizontal tab character as string delimiters and would assign just first space/tab separated string to specified loop variable I. This line splitting behavior is unnecessary here and is disabled for that reason by using option delims= which defines an empty list of string delimiters.
FOR would ignore also lines on which first substring after splitting a line up into substrings starts with default end of line character ;. The line splitting behavior is already disabled, but the name of directory can start unusually with a semicolon. Such a directory name would be ignored by FOR. Therefore the option eol=| defines the vertical bar as end of line character which no directory name can have and so no directory is ignored by FOR. See also the Microsoft documentation page Naming Files, Paths, and Namespaces.
The directory name assigned to loop variable I is not really used because of FOR executes for each directory name just command SET with an arithmetic expression to increment the value of the environment variable FolderCount by one.
The environment variable FolderCount contains the number of subfolders in specified directory independent on hidden attribute.
The short version of this solution without counting the folders:
#echo off
for /F "eol=| delims=" %%I in ('dir "%TEMP%" /AD /B 2^>nul') do goto HasFolders
echo Temporary files folder has no subfolder.
goto EndBatch
:HasFolders
echo Temporary files folder has subfolders.
:EndBatch
pause
The loop is exited with command GOTO on FOR has assigned first name of a directory to the loop variable.
Solution 3 using DIR and FINDSTR:
#echo off
dir "%TEMP%" /AD /B 2>nul | %SystemRoot%\System32\findstr.exe /R "^." >nul
if errorlevel 1 (
echo Temporary files folder has no subfolder.
) else (
echo Temporary files folder has subfolders.
)
pause
The output of DIR as explained above executed by cmd.exe processing the batch file is redirected from STDOUT of command process to STDIN (standard input) of FINDSTR which searches for lines having at least one character. The found lines are all lines with a directory name output by DIR. This search result is of no real interest and therefore redirected to device NUL to suppress it.
FINDSTR exits with 1 if no string could be found and with 0 on having at least one string found. The FINDSTR exit code is assigned by Windows command processor to ERRORLEVEL which is evaluated with the IF condition.
The IF condition is true if exit value of FINDSTR assigned to ERRORLEVEL is greater or equal 1 which is the case on no directory found by DIR and so FINDSTR failed to find any line with at least one character.
This solution could be also written as one command line:
dir "%TEMP%" /AD /B 2>nul | %SystemRoot%\System32\findstr.exe /R "^." >nul && echo Temporary files folder has subfolders.|| echo Temporary files folder has no subfolder.
See single line with multiple commands using Windows batch file for an explanation of the operators && and || used here to evaluate the exit code of FINDSTR.
Additional hints:
It would be good to first check if the directory exists at all before checking if it contains any subdirectories. This can be done in all three solutions above by using first after #echo off
if not exist "%TEMP%\" (
echo Folder "%TEMP%" does not exist.
pause
exit /B
)
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cmd /?
dir /?
echo /?
exit /?
findstr /?
for /?
goto /?
if /?
pause /?
set /?
DIR "your directory" /ad, for example DIR C:\Users /ad brings out all folders that are inside C:\Users
Displays a list of files and subdirectories in a directory.
DIR [ drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points
If you just want to use the cmd.exe shell console to see if there are any directories:
DIR /A:D
If you want to check for it in a .bat file script:
SET "HASDIR=false"
FOR /F "eol=| delims=" %%A IN ('DIR /B /A:D') DO (SET "HASDIR=true")
IF /I "%HASDIR%" == "true" (
REM Do things about the directories.
)
ECHO HASDIR is %HASDIR%

Resources