find through command prompt - windows

How to find all the *.txt files in any directory(i.e. c:\,d:\ etc.) through command prompt?

c:
cd \
dir /s *.txt
d:
cd \
dir /s *.txt

Following will search from root directory and its all accessible sub folders regardless of the folder you currently in.
dir \*.txt /s
or
dir c:\*.txt /s
dir d:\*.txt /s
etc

Try using dir *.txt

setlocal ENABLEEXTENSIONS
FOR %%A IN (a b c d e f g h i j k l m n o p q r s t u v w x y z) DO #call :dumpdrive %%A
echo Done...
goto :EOF
:dumpdrive
FOR /R "%1:\" %%B IN (*.txt) DO #echo.%%~fB
goto :EOF

Related

batch file to search all drives for a specific file and delete that file

I'm trying to make a batch file to find a file (for example Raihan.txt) and delete it or forced delete it(if its running).
In this case I want it to search all drives including USB drives.
I actually don't know much about batch coding, I searched it on internet and came up with those line and problem is I can't search all drives. I need some help here.
#echo off
set file_to_delete=Raihan.txt
set dir_to_look_in=C:\
:: Starting loop
for /f "tokens=1* delims=" %%f in ('dir /s /o /b "%dir_to_look_in%" ^| findstr "%file_to_delete%"') do (
echo INFO: Deleting: %%f
del /q "%%f"
)
I haven't run it because it's not even complete, one wrong move and I will be in big trouble.
Here is one example
#echo off
set file_to_delete=Raihan.txt
:: Starting loop to search through all drives
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist "%%d:\%file_to_delete%" (
echo INFO: Deleting: "%%d:\%file_to_delete%"
del /q "%%d:\%file_to_delete%"
)
)
Here is another
#echo off
setlocal enabledelayedexpansion
set "file_name=Raihan.txt"
for /f "tokens=1-2" %%a in ('fsutil fsinfo drives ^| findstr /r "^[A-Z]:"' ) do (
if exist "%%a:\%file_name%" (
echo Deleting "%%a:\%file_name%"
del "%%a:\%file_name%"
)
)
#echo off
setlocal enabledelayedexpansion
set "file=example.txt"
for %%i in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist "%%i:\" (
for /f "delims=" %%j in ('dir /s /b "%%i:\%file%"') do (
set "filepath=%%j"
del "!filepath!"
echo !filepath! deleted
)
)
)
echo All instances of %file% have been deleted.
pause
endlocal

.bat file changing first 3 letter of every filename to uppercase - Windows only

From Filename : 1ab12345_def7890.txt
to 1AB12345_def7890.txt. Plese notice 1AB in uppercase.
I tried following but it is renaming whole file name to uppercase including extension.
#echo off
setlocal enableDelayedExpansion
pushd c:\some_dir
for %%f in (*) do (
set "filename=%%~f"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%f" "!filename!" >nul 2>&1
)
endlocal
Can anybody please help me with this?
Thank you !
You simply need to isolate the leading three characters of filename first, perform your replacement, then prepend it back to the string.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
PushD "C:\some_dir" 2> NUL || GoTo :EOF
For %%G In (*) Do (Set "filename=%%~nG"
SetLocal EnableDelayedExpansion
Set "leading=!filename:~,3!"
For %%H In (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
) Do Set "leading=!leading:%%H=%%H!"
Ren "%%G" "!leading!!filename:~3!%%~xG" 1> NUL
EndLocal)
PopD

Batch recursion exceeds stack limits - Error while calling a batch file from another

I've a couple of batch files, 1.bat and 2.bat and i'm calling 1.bat from 2.bat, this is what it has,
1.bat(Unraring some rar files onto a dir)
#echo OFF
setlocal enabledelayedexpansion
#set UNRAR="C:\Program Files\Winrar\unrar.exe"
For /R "E:\Test" %%G IN (*.rar) do (
%UNRAR% x %%G E:\Test
#set PDIR=%%G
#set PDIR1=!PDIR:.rar=!
XCOPY /D /E /S /Y !PDIR1! E:\Test\Files
RMDIR /S /Q !PDIR1!
)
pause
2.bat
call 1.bat
pause
1.bat runs fine on its own but when we try calling it in 2.bat gives the error
****** B A T C H R E C U R S I O N exceeds STACK limits ******
Recursion Count=594, Stack Usage=90 percent
****** B A T C H PROCESSING IS A B O R T E D ******
How do i fix this? any ideas, thanks

recursively rename files AND subfolders in a folder using batch script

I know there are similar posts, but non covers what I need.
I need to rename all the files and sub folders of a given folder and make them uppercase. I found this post which is great but only does the files OR subfolders:
Rename all files in folder to uppercase with batch
I tried doing nested FOR with no joy. According to the manual, /R is suppose to recur through folders, but it is not doing anything. Tried /D /R with no luck either. So I was hoping to use something like below:
#echo off
setlocal enableDelayedExpansion
pushd C:\MyFolder
for /R /D %%f in (*) do (
set "filename=%%~f"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%f" "!filename!" >nul 2>&1
)
endlocal
Any ideas?
Parse a static list dir /B /S * to get all files and subfolders.
Read entire for /? for %%~nxf explanation.
#echo off
setlocal enableDelayedExpansion
pushd C:\MyFolder
for /F "delims=" %%f in ('dir /B /S *') do (
set "filename=%%~nxf"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%~f" "!filename!" >nul 2>&1
)
popd
endlocal
Read ren /? as well:
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.

Command prompt logging with xcopy

I have an xcopy script that I'm running that reads in a .csv file with directories and file names and parses it to copy the files.
Here's the script:
echo F | for /f "delims=, tokens=1,2,3" %i in (D:\foo.csv)
do xcopy /i /d "Z:\%i\%j\%k" "Y:\%i\%j\%k" >> "D:\xcopy\Log.txt"
The output to the command prompt are the commands being executed:
echo F | xcopy /i /d "Z:\hcri001\a1\
ffce5a14-33ca-43cf-b366-af266c450979" "Y:\hcri001\a1\ffce5a14-33ca-43cf-b366
-af266c450979" 1>>"D:\xopy\Log.txt
However, the log just has the output of the commands:
0 File(s) copied
0 File(s) copied
0 File(s) copied
Does Y:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3 specify a file name
or directory name on the target
(F = file, D = directory)? F
Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3
1 File(s) copied
Is there someway to have both the commands being executed, as well as the output?
Also, is there anyway to have some sort of counter that I can put in the beginning of the line?
My Ideal log file would be in this format:
1) C:\Desktop>echo F | xcopy /i /d "Z:\hcri001\a7\
00a62a73-d7a7-4cfb-b55c-457bc67b3647" "Y:\hcri001\a7\00a62a73-d7a7-4cfb-b55c
-457bc67b3647" 1>>"D:\Robocopy\AtlusPatient131172CopyLog.txt
0 File(s) copied
2) C:\Desktop>echo F | xcopy /i /d "Z:\hcri001\d5\
003452354-d7a7-4cfb-452c-457bc67b3647" "Y:\hcri001\d5\003452354-d7a7-4cfb-452c-457bc67b3647" 1>>"D:\Robocopy\AtlusPatient131172CopyLog.txt
0 File(s) copied
3) C:\Desktop>echo F | xcopy /i /d "Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3" "Y:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3" 1>>"D:\Robocopy\AtlusPatient131172CopyLog.txt
Does Y:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3 specify a file name
or directory name on the target
(F = file, D = directory)? F
Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3
1 File(s) copied
and bonus points if the command can be trimmed to just include the file name instead of the whole command i.e. 1) "Z:\hcri001\2d\545392db-50fa-40d9-aaa2-0892ca5057f3"
0 File(s) copied
for /f "delims=, tokens=1,2,3" %i in (D:\foo.csv) do (
echo xcopy /i /d "Z:\%i\%j\%k" "Y:\%i\%j\%k" >> "D:\xcopy\Log.txt"
echo f | xcopy /i /d "Z:\%i\%j\%k" "Y:\%i\%j\%k" >> "D:\xcopy\Log.txt"
)
For the counter you need SETLOCAL ENABLEEXTENSIONS then set counter=1
and inside the loop set /a counter=!counter! + 1

Resources