Here's my go with comments in the code.
I'm just brushing up by biatch skills so forgive any blatant errors.
I tried to write an all in one solution as best I can with a little modification where the user requires it.
Some important notes: Just change the variable recursive
to FALSE
if you only want the root directories files and folders processed. Otherwise, it goes through all folders and files.
C&C most welcome...
@echo off
title %~nx0
chcp 65001 >NUL
set "dir=c:\users\%username%\desktop"
::
:: Recursive Loop routine - First Written by Ste on - 2020.01.24 - Rev 1
::
setlocal EnableDelayedExpansion
rem THIS IS A RECURSIVE SOLUTION [ALBEIT IF YOU CHANGE THE RECURSIVE TO FALSE, NO]
rem By removing the /s switch from the first loop if you want to loop through
rem the base folder only.
set recursive=TRUE
if %recursive% equ TRUE ( set recursive=/s ) else ( set recursive= )
endlocal & set recursive=%recursive%
cd /d %dir%
echo Directory %cd%
for %%F in ("*") do (echo ? %%F) %= Loop through the current directory. =%
for /f "delims==" %%D in ('dir "%dir%" /ad /b %recursive%') do ( %= Loop through the sub-directories only if the recursive variable is TRUE. =%
echo Directory %%D
echo %recursive% | find "/s" >NUL 2>NUL && (
pushd %%D
cd /d %%D
for /f "delims==" %%F in ('dir "*" /b') do ( %= Then loop through each pushd' folder and work on the files and folders =%
echo %%~aF | find /v "d" >NUL 2>NUL && ( %= This will weed out the directories by checking their attributes for the lack of 'd' with the /v switch therefore you can now work on the files only. =%
rem You can do stuff to your files here.
rem Below are some examples of the info you can get by expanding the %%F variable.
rem Uncomment one at a time to see the results.
echo ? %%~F &rem expands %%F removing any surrounding quotes (")
rem echo ? %%~dF &rem expands %%F to a drive letter only
rem echo ? %%~fF &rem expands %%F to a fully qualified path name
rem echo ? %%~pF &rem expands %%A to a path only
rem echo ? %%~nF &rem expands %%F to a file name only
rem echo ? %%~xF &rem expands %%F to a file extension only
rem echo ? %%~sF &rem expanded path contains short names only
rem echo ? %%~aF &rem expands %%F to file attributes of file
rem echo ? %%~tF &rem expands %%F to date/time of file
rem echo ? %%~zF &rem expands %%F to size of file
rem echo ? %%~dpF &rem expands %%F to a drive letter and path only
rem echo ? %%~nxF &rem expands %%F to a file name and extension only
rem echo ? %%~fsF &rem expands %%F to a full path name with short names only
rem echo ? %%~dp$dir:F &rem searches the directories listed in the 'dir' environment variable and expands %%F to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string
rem echo ? %%~ftzaF &rem expands %%F to a DIR like output line
)
)
popd
)
)
echo/ & pause & cls