Reputation: 23
I have to send several commands (del
, compressing files, etc.) In a folder and its subfolders.
I'm using a for loop and now I'm able to send all the commands in all the subdirectory, but not in the actual path.
In the following what I'm doing (loops in the subfolder, enternig in them, extracting the extension of the files called my-file.*
) and then doing several operations inside each subfolder (....),
for /f "delims=" %%a in ('dir /s /b /o:n /ad') do (
REM "delims=" to deal with path containing spaces
cd /d "%%a"
for %%i in (my-file.*) do set EXTENSION=%%~xi
....
)
Upvotes: 0
Views: 59
Reputation: 5504
My suggestion would be to use a for /F
loop with dir /S
command which searches through all subfolders:
@echo off
setlocal EnableDelayedExpansion
for /F "delims= eol=" %%A IN ('dir /S /B /A-D "my-file.*"') do (
pushed "%%~dpA"
rem do random stuff here
popd
)
Note that you don't need to set to a variable the extension, you can access it with %%~xA
immediately.
I have enabled delayed expansion since you may set a variable inside the for
loop, so you will need to access it with !var!
rather than %var%
.
Upvotes: 1
Reputation: 2007
instead of ('dir /s /b /o:n /ad')
use ('cd ^& dir /s /b /o:n /ad')
Upvotes: 2
Reputation: 17493
You might also use forfiles
as in following example:
forfiles /s /M my_file.* /C "cmd /c echo @file:@path,@fname.@ext"
"my_file.log":"C:\tralala\my_file.log","my_file"."log"
"my_file.txt":"C:\tralala\my_file.txt","my_file"."txt"
Upvotes: 0