How can I make my BATCH file affect subfolders? - windows

I have a BATCH file which deletes every other JPG file. I am looking to make it so that when it is run, it affect all subfolders as well.
I am not a coder by all means, so apologies if this is a rookie question!
Below is my current code:
#echooff
setlocal
for /r %%D in (.) do (
set "del="
for /f %%F in ('dir /b "%%D\*.jpg"') do if defined del (
del "%%D\%%F"
set "del="
) else set "del=1"
)
PAUSE

The /S switch in the dir makes you go through subdirectories, as you can see here:
dir /?
...
/S Displays files in specified directory and all subdirectories.
...
So I'd advise you to replace:
dir /b "%%D\*.jpg"
by:
dir /S /b "%%D\*.jpg"
Good luck

Based upon your clarifications, is this what you're trying to achieve:
#SetLocal EnableExtensions DisableDelayedExpansion
#For /F "EOL=?Delims=" %%G In ('Dir /B/S/A:D 2^>NUL') Do #(Set "_="
For /F "EOL=?Delims=" %%H In ('Dir /B/A:-D/O:N "%%G\*.jpg" 2^>NUL')Do #(
If Not Defined _ (Set "_=T")Else Del /A/F "%%G\%%H"&Set "_="))

Related

Getting Parent Directory for each file from Dir Output

I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause

Batch : Search Directory in every Directory and Subdirectory

I want to delete all directories with a specific name. E.g. "vendor"
D:\Data\Project1\Dir\vendor
D:\Data\Project2\AnotherDir\AnotherDir\vendor
D:\Data\Project3\vendor
This is what I have at this moment. This lists all the folders, and subfolders.
for /d %%a in (*) do dir /ad /on /s /b %%a
Now I wish to put one path in a variable, and check whether the directory is equal to a name (for example "vendor"). But I can't find how.
Can you help me?
Regards,
Demian
This is my working solution if you have RimRaf installed:
#ECHO off
setlocal enableDelayedExpansion
:: Folders where the batch program doesn't need to search.
set skipTheseFolders=Decleir Pinokkio
:: All the folders that need to be deleted
set foldersToDelete=vendor node_modules
for /d /r %%d in (*.*) do (
#ECHO %%d
set folder=%%~nxd
set canI="true"
:: For loop to skip folders
for %%s in (%skipTheseFolders%) do (
:: Need to be implemented
set canI="true"
)
IF !canI! =="true" (
for %%l in (%foldersToDelete%) do (
:: If the folder is one of the folders To Delete
IF "!folder!" == "%%l" (
#echo %%d
cd %%d
cd ..
:: Using Rimraf because when you delete node_modules, normal delete won't work
start /B rimraf !folder!
)
)
)
)
The DIR command has an unfortunate limitation that it cannot recursively list specific folder names. The simplest solution is to list all folders, and use FINDSTR to filter out all but the folders that match.
dir /ad /on /s /b | findstr /iec:"\vendor"
Then you simply iterate the results using FOR /F and add your RD command
for /f "eol=: delims=" %%F in
'dir /ad /on /s /b ^| findstr /iec:"\vendor"'
) do rd /s /q "%%F" 2>nul
You can try this :
#Echo OFF
set "Folder=D:\Data"
set "FolderString=vendor"
Setlocal Enabledelayedexpansion
FOR /f "tokens=*" %%F IN ('dir /b /s /ad %Folder%\ ^| find "%FolderString%"') DO (set var="%%F"
ECHO rd /s /q !var! && rd /s /q !var!)
EndLocal
pause

How to create file in partially known folder name using batch file

I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)

Iterate over a tree of folders and sub folders and find a specific file by name

I am new to batch scripting. Basically I want to iterate over a tree of folders and sub folders and find a specific file by name.
Until now I have this :
#echo off
SETLOCAL
for /F %%i in ('dir C:\Projects /s /b') do (
Set originalFileName = %%~ni
echo %originalFileName%
)
pause
basically now I want to compare with a string and copy that file to another folder.
#echo off
for /F "delims=" %%a in ('dir C:\Projects /s /b /a-d') do if /i "%%~na"=="string" copy "%%~fa" "x:\another folder\"
This is just another method to achieve the same aim, if only one filename.ext exists in the tree.
#echo off
for /F "delims=" %%a in ('dir "C:\Projects\filename.ext" /s /b /a-d') do copy "%%a" "x:\target-folder\"

Setting a start position in a Path(Batch processing)

i'm a newbie in batch scripting,started learning this from last week only and this is the first question that i'm asking here.here is my situation,
Consider this example,this lists all directories under D:/Jose/test1 and append this to a text file.
Code:
#echo off
SETLOCAL EnableDelayedExpansion
cd /d D:\Jose\test1
FOR /F "delims=" %%G in ('dir /ad /on /s /b') DO (
ECHO %%~pG%%~nG>>D:\test2\list.txt
)
ENDLOCAL
pause
Text file output :
\Jose\test1\1
\Jose\test1\2
\Jose\test1\1\12
\Jose\test1\1\13
\Jose\test1\1\12\131
\Jose\test1\1\12\Copy of 131
\Jose\test1\1\12\131\1311
\Jose\test1\1\12\131\1311\13111
\Jose\test1\1\12\131\1311\13112
\Jose\test1\1\12\Copy of 131\1311
\Jose\test1\1\12\Copy of 131\1311\13111
\Jose\test1\1\12\Copy of 131\1311\13112
\Jose\test1\1\13\132
\Jose\test1\1\13\132\1321
\Jose\test1\1\13\132\1321\13211
I want to remove '\jose' from all line ie, i want to set '\test1' as the starting path. Need help guys..Thanks in advance...
try it with sed for Windows:
for /d /r %%G in (*) do sed -r "s/^\\[^\]+(\\.*)/\1/" "%%~pnxG">>D:\test2\list.txt
..solution with pure batch:
#echo off &setlocal
(FOR /f "tokens=2* delims=\" %%a IN ('dir /ad /on /s /b') DO ECHO(\%%~b)>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE
..and more batch:
#echo off &setlocal
(FOR /d /r %%G in (*) DO (
SET "fname=%%~G"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fname=!fname:*\Jose=!"
ECHO(!fname!
ENDLOCAL
))>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE

Resources