user5201880
user5201880

Reputation: 1

bat which runs command lines on several directories in a directory

Following the suggestion by Foxidrive, I have edited my question for clarity.

I would like to apply a list of command lines to files in all subfolders in a main folder called "Jan 1". Instead of changing folders manually by "cd", I am trying to make a batch file which applies command lines to the files in every single subfolders. What I have is 30 folders containing a bunch of files in each folder. They are data sets from 30 different participants. what I want to know is how to go into a subfoder one after the other.

My first attempt was to use "For" loop.

"C:\Users\u00854141\Documents\PES\Record2016\Jan 1". I would like to run command A, B, and C on files in those 30 folders.

@ECHO OFF
CD C:\Users\u00854141\Documents\PES\Record2016\Jan 1
For /D %%g in (*) do (
    command A
    command B
    command C
)

As can be seen in the below, I've been getting generous help from several people. But because I didn't make myself clear, still I haven't solve the problem. Thank you everyone for your patience. Best regards,

Upvotes: 0

Views: 85

Answers (2)

aschipfl
aschipfl

Reputation: 34909

Fixed code, using for /R:

@echo off
pushd .
cd /D "C:\Users\u00854141\Documents\PES\Record2016\Jan 1"
for /R %%F in (*) do (
    pushd "%%~dpF"
    rem command_A "%%~nxF"
    rem command_B "%%~nxF"
    rem command_C "%%~nxF"
    popd
)
popd

The for /R loop walks through all files in all (sub-)directories recursively.
In the loop body, pushd changes to the current directory temporarily before coming to the 3 commands; popd restores the original directory.
The "%%~nxF" portion in the command place-holders refers to the pure file name of the current item; if you want to pass the full path to the command, give "%%~fF" instead.

Upvotes: 0

Happy Face
Happy Face

Reputation: 1061

You can change the command after cmd /c, I make it understandable for you to see what's going on.

@ECHO OFF

forfiles /p "C:\Users\u00854141\Documents\PES\Record2016\Jan 1" /s /c "cmd /c if @isdir == FALSE (echo FILE - DO COMMAND HERE) else (echo. & echo DIR - @path & echo.)"

pause >nul

Upvotes: 1

Related Questions