Delete a file inside subfolder using bat - search

I know the name of the subfolder inside which the file I want to delete is located. The name of the subfolder is "My Subfolder" and the file I want to delete inside the subfolder is "The-Bad File.txt" . I am at "D" drive. "My Subfolder" is located inside "Folder-2" "Folder-4" "Folder-54" and "Folder-543". I want to search these folders only to delete "Bad File.txt" using .bat file inside "My Subfolder".

This will delete "Bad File.txt" in every place it is found below the current folder.
#echo off
del /s "Bad File.txt"

for %%a in (2 4 54 543) do echo del "D:\Folder-%%a\My Subfolder\The-Bad File.txt"
Remove echo if it should work.
€dit:
If the start folder of the batch is located in one of the "My Subfolder" folders, this should work:
for /d %%a in (..\*) do echo del "%%~a\My Subfolder\The-Bad File.txt"

I'm assuming you don't always now exactly all locations where "My Subfolder" exists, and you want to delete the file from all such locations.
for /f "delims=" %%F in ('dir /b /ad /s "d:\My Subfolder"') do del "%%F\The-Bad File.txt"
To delete the file from all folders beneath "My Subfolder", all you need to do is add the /S option to the DEL command.
for /f "delims=" %%F in ('dir /b /ad /s "d:\My Subfolder"') do del /s "%%F\The-Bad File.txt"

Related

Extract part of a variable

I'm trying to extract the name of the file that is on loop as a string. to organize many software outputs in different folders, named by the current process file.
So far i got this, but i can't get the string on the variable:
set DirecoryT=%%G
FOR /R D:\LiDAR_Data\LiDAR_DATA_EBA\ %%G in (*.laz) do (
echo Directory %DirecoryT%
set File=%DirecoryT:~29,9%
echo Processing file %File%
pause
)
pause
Have a go at this:
#echo off
for /R "D:\LiDAR_Data\LiDAR_DATA_EBA\" %%G in (*.laz) do (
echo File Extension only: "%%~xG"
echo FileName with Extension: "%%~nxG"
echo FileName without Extension: "%%~nG"
echo Full path and name: "%%G"
echo Directory to file only: "%%~pG"
echo Drive and directory is: "%%~dpG"
)
pause
Notice how we get the various parts of the string. For more on variable handling, simply run for /? from command line and read the help.
So a little closer to what you actually want, based on your attempt.
#echo off
for /R "D:\LiDAR_Data\LiDAR_DATA_EBA\" %%G in (*.laz) do (
echo FileName "%%~nxG"
echo Directory "%%~dpG"
echo Processing file "%%~nxG"
)
pause
EDIT
As per your last comment to see only the last folder where the file exists:
#echo off
for /R "D:\LiDAR_Data\LiDAR_DATA_EBA\" %%G in (*.laz) do (
echo FileName "%%~nxG"
echo Directory "%%~dpG"
echo Processing file "%%~nxG"
for %%i in ("%%~dpG\.") do echo Last Folder "%%~nxi"
)
pause
This should do the trick
for %%a in ("%pathToFile%") do set "newvariable=%%~na"

Need to send All Folders Names that apply forfiles condition to Excel file

My forfiles command is:
forfiles /p "%%~a" /d %days% /c "cmd /c if /i #isdir==true echo Deleting... #path & rd /s /q #path"
If I write:
echo #path>>Folders_that_deleted.xls
Inside the 'Forfiles' command It will produce Excel file in every subfolder
that apply the condition! And what I need is to send all Folders names prior delete to ONE excel file.
Thanks in advance!!
You can include the full path to where the file should be
echo #path>>d:\somewhere\Folders_that_deleted.xls
Or, better, redirect the full forfiles
>> folders_that_deleted.xls (
forfiles /p "%%~a" /d %days% /c "cmd /c if /i #isdir==true echo Deleting... #path & rd /s /q #path"
)

Improving Windows Batch Script

The following script search thru a folder and its sub-folders and then compares each file it gets to files in the 2nd folder. If found it echo's a message to say that the file was found.
My question is how do I enhance this script to search the 2nd folder and its sub-folders for the file found in 1st dir? I only care about the file-name with extension (I do not care in which folder/sub-folder it was found, just that a duplicate file is present and causing compiler errors)
I thought one of the ways would be to output all file names to a file and then take the file as input in 2nd part and loop thru the 2nd folder, but I am sure there must be a cleaner way.
O/S: Windows 2003
:bof
rem #echo off
cls
setlocal
:init
set dirA=X:\tst\pfsrc\
set dirB=X:\tst\cbsrc\
if not exist "%dirA%" echo dirA not found & goto :EOF
if not exist "%dirB%" echo dirB not found & goto :EOF
for /f "delims=" %%I in ('dir /b /a:-d /s "%dirA%" 2^>NUL') do if exist "%dirB%%%~nxI" echo %%~nxI does exist in "%dirB%"
:eof
this excerpt will try to find %%a in all dirs of %dirB% recursively
for /d /r %%d in (%dirB%\*) do (
if exist "%%d\%%~nxa" echo %%d\%%~nxa
)
Thank you for putting me on the correct track :-) Code now works
:bof
#echo off
cls
setlocal
:init
set dirA=X:\tst\pfsrc\
set dirB=X:\tst\cbsrc\
if not exist "%dirA%" echo dirA not found & goto :EOF
if not exist "%dirB%" echo dirB not found & goto :EOF
for /f "delims=" %%I in ('dir /b /a:-d /s "%dirA%" 2^>NUL') do for /r "%dirB%" %%d in (%%~nxI) do if exist "%%d" echo %%d
:eof

Batch Script to find string in directory and all subdirectories

I want to write a batch file which used the find command to find a string in the parent directory and all sub directories, and prints that output to a text file, then opens the text file when done. My code so far looks like this:
#echo off
set /p "var1= Enter the String to Find: "
for /F "delims=" %a in ('dir /B /S *.txt') do #(find /i "%var1" "%a" 1>nul 2>&1 && find /i "%var1" "%a") >> result.txt
start result.txt
But this is currently not even writing anything to result.txt, even though I am sure that where I am searching the string appears in multiple .txt files. I know it must be something syntax-wise but I can't seem to figure it out.
You have a few mistakes in your script:
There should not be a space after the equals sign of a set command. Specifically, remove the space after set /p "var1=.
To expand variables, you have to put a percent sign before and after the variable name, so instead of %var use %var%.
Not directly related to your problem, but why are you invoking find twice?
I've also made use of a temporary file, so that result.txt won't be searched by find.
Note that for if you're running the batch script from a file, You need to use double percent signs when you use loop variables, for example: %%a
Anyway, here's the fixed script, hopefully doing what you intended to do:
#echo off
set RESULT_FILE="result.txt"
set /p "var1=Enter the String to Find: "
pushd %~p0
type NUL > %RESULT_FILE%.tmp
for /f "delims=" %%a in ('dir /B /S *.txt') do (
for /f "tokens=3 delims=:" %%c in ('find /i /c "%var1%" "%%a"') do (
for /f "tokens=*" %%f in ('find /i "%var1%" "%%a"') do if %%c neq 0 echo %%f
)
) >> "%RESULT_FILE%".tmp
move %RESULT_FILE%.tmp %RESULT_FILE% >nul 2>&1
:: Open the file
"%RESULT_FILE%"
popd
just findstr abc *
in contrast, findstr /v will print all without abc

Getting substring of a token in for loop?

I have this for loop to get a list of directory names:
for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do (
echo %%g
)
Output:
C:\WINDOWS\Assembly\gac_msil\policy.5.0.A.D
C:\WINDOWS\Assembly\gac_msil\policy.5.0.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.5.20.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.5.25.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.5.35.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.5.55.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.5.60.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.5.70.A.D.O
C:\WINDOWS\Assembly\gac_msil\policy.6.0.A.D.O
I want to get the folder names starting with "policy" but echo %%g:~29 doesn't work.
I also tried to set x=%%g and then echo %x:~29% and still doesn't work.
So, how do I get substring from token in for loop?
Of course that set x=%%g and a substring extraction of x should work, but be aware that if the substring is taken inside a FOR loop, it must be done with ! instead of % (Delayed Expansion):
setlocal EnableDelayedExpansion
for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do (
set x=%%g
echo !x:~29!
)
On the other hand, if you want to know "How to get the last part (name and extension) of a token in for loop", the answer is: use the ~Name and ~eXtension modifiers in %%g replaceable parameter:
for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do (
echo %%~NXg
)
I recently had a similar question, and I wanted to compile here the 3 working solutions:
Use EnableDelayedExpansion in your batch file, as in the accepted answer
#echo off
setlocal EnableDelayedExpansion
for /d %%g in (%windir%\Assembly\gac_msil\*) do (
set x=%%g
echo !x:~29!
)
Use for /f to process the output of a dir command
for /f "tokens=*" %%g in ('dir /ad /b %windir%\Assembly\gac_msil\*') do #echo %%g
And finally, the optimal solution in this case, using enhanced variable substitution. See the end of the help at for /? for more information on this syntax.
for /d %%g in (%windir%\Assembly\gac_msil\*) do #echo %%~nxg
A simple
dir /B %windir%\Assembly\gac_msil\*policy*A.D*
should do the trick. If you want to loop over it:
for /f %%g in ('dir /B %windir%\Assembly\gac_msil\*policy*A.D*') do (
echo %%g
)

Resources