I am looking to create a batch file that when given a pathway, it will count all the folders and sub folders within it. So far I am only able to gather the number of folders within the 1st level of the pathway. I will then pipe it to a text file.
Here's what I have so far:
for /f %%a in ('dir /b /ad %folder%^|find /c /v "" ') do set count=%%a
echo %count% folder(s^)>> !output!
Am I close to getting what I want? What do I need to tweek?
Thanks!
Add /s to include all subfolders:
for /f %%a in ('dir /b /s /ad %folder%^|find /c /v "" ') do set count=%%a
echo %count% folder(s^)>> !output!
A small edit of this answer: Batch file to list files and folders in a simple format
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :listFolder >report.txt
exit /b
:listFolder
setlocal
set "indent=%indent%%tab%"
for /d %%F in (*) do (
echo %indent%.\%%F
pushd "%%F"
call :listFolder
popd
)
exit /b
Here's how I solved the problem.
if exist "File Count" del "File Count"
dir "%~d1%~p1\*.*" /b /s >> "File Count"
find /c "." "File Count"
First we check to see if a file exists, and if so, delete it. Then we get a list of files and dump that to our file. then run FIND on the file. FIND will handily give us a big label, so the name of the file might as well be something attractive and human readable.
I am personally interested in a specific filetype, so I changed . into *.jpg but the principle is the same.
Related
I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.
For example; for each ABC_R10.txt --> there is a ABC).pdf file.
In Windows 10, with a batch file,
I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.
CLS
#ECHO OFF
ECHO FIND BUKUM
findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls
for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%"
if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT
But I want to find file name of exact match but get pdf file with similar name, not txt file.
I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf
Substring of _R occurs each .txt file.
How can I achieve it?
Based solely on your now edited question:
#For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do #Copy /Y "%%A).pdf">Nul
EDIT: there is nothing to say to #Compo's modification,
just when using the ) unquoted inside a code block it has to be escpaped ^)
#ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
I want to make a script which finds as quickly as possible first folder named Target starting from root location D:\ and return its absolute path.
Folder structure of root location (D:\) can be like this:
-DontSearchHereFolder
-Folder1\Subfolder1\SSubfolder1\SSSubfolder1\
-Folder2\Subfolder2\SSubfolder2\TargetFolder
-DontSearchHereFolder2
-Folder3\Subfolder3\
Output of the script should be: D:\Folder2\Subfolder2\SSubfolder2\TargetFolder
For now I tried 2 methods but it's not quick enough:
(1)
set TG=\TargetFolder
set root=D:\
cd %root%
for /f "delims=" %%a in ('dir /b /s /a:d "%root%" ^|findstr /e /i "%TG%"') do set "folderpath=%%~a"
(2)
for /d /r "%root%" %%a in (*) do if /i "%%~nxa"=="%TG%" set "folderpath=%%a"
(1) is quicker than (2)
Question1: Is it possible to specify in command to search only for a maximum of 2 folders "down" from root (e.g. D:\Folder1\Subfolder1) ?
Question2: Is it possible to specify folders that should be automatically skipped (e.g. DontSearchHereFolder1&2)
This batch code is exactly for what you have asked for optimized for speed. It ignores the two specified directories on first level and it searches for the folders maximal two folder levels deep.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Root=D:"
set "TG=TargetFolder"
set "Ignore1=DontSearchHereFolder"
set "Ignore2=DontSearchHereFolder2"
for /D %%A in ("%Root%\*") do (
if "%%~nxA" == "%TG%" set "FolderPath=%%A" & goto Found
if not "%%~nxA" == "%Ignore1%" (
if not "%%~nxA" == "%Ignore2%" (
for /D %%B in ("%%A\*") do (
if "%%~nxB" == "%TG%" set "FolderPath=%%B" & goto Found
for /D %%C in ("%%B\*") do if "%%~nxC" == "%TG%" set "FolderPath=%%C" & goto Found
)
)
)
)
echo Could not find folder: "%TG%"
goto EndSearch
:Found
echo Found folder: "%FolderPath%"
:EndSearch
endlocal
The string comparisons are done case-sensitive for maximum speed.
No recursive subroutine calls are used as usually would be done for such tasks for maximum speed.
The comparisons for the directories to ignore in root folder are coded in batch script directly not using an array or a list of folder names for maximum speed.
Delayed expansion is not used for faster processing the command lines.
But much faster would be coding an executable in C/C++/C# for that task as processing the command lines of the batch file takes most likely the most time on searching for the folder.
Note: Command FOR ignores folders with hidden attribute set.
Well, I use for such tasks shareware tool Total Commander which supports searching only in selected folders for a specific folder not more than X levels deep extremely fast.
This should take into account all the limits indicated in the question, but unless a lot of folders are found inside the indicated exclusions, I don't think this should be faster, just give it a try
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=d:\"
set "target=TargetFolder"
set "maxLevels=2"
set excludeFolders= "DontSearchHereFolder" "DontSearchHereFolder2"
for %%s in ("%source%") do for /f "tokens=*" %%f in ('
robocopy "%%~fs." "%%~fs." /l /nfl /njh /njs /nc /ns /s
/xd %excludeFolders% /lev:%maxLevels%
^| findstr /e /i /l /c:"\\%target%\\"
^| cmd /v /q /c"set /p .= &&(echo(!.!)"
') do echo "%%~f"
I think this is the fastest possible way to do this:
#echo off
setlocal EnableDelayedExpansion
if "%1" neq "" goto %1
set "root=D:\"
set "TG=TargetFolder"
set "exclude=/DontSearchHereFolder1/DontSearchHereFolder2/"
"%~F0" Input | "%~F0" Output > result.txt
set /P "folderpath=" < result.txt
del result.txt
echo First folder: %folderpath%
goto :EOF
:Input
cd "%root%"
for /D %%a in (*) do if "!exclude:/%%a/=!" equ "%exclude%" (
cd "%%a"
dir /B /S /A:D "%TG%" 2>NUL
cd ..
)
exit /B
:Output
set /P "folder="
echo "%folder%"
set "i=0"
for /F "tokens=2" %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO TABLE /NH') do (
set /A i+=1
if !i! equ 2 taskkill /PID %%a /F
)
exit /B
The folders to exclude are given in a slash-separated list; if this list is longer, the process run faster because more folders are skipped. The target folder is search in each one of the non-excluded folders via a dir /B /S /AD "%TG%" command, that is faster than any combination of other commands. The process ends as soon as the first folder name is received in the rigt side of the pipe; the remaining processing at left side of the pipe is cancelled via a taskkill command.
I am new here. I found the way to find only one string from a directory.
findstr /S /M /C:"string" /C:folder *.txt
I can get success for only one string. But my wish is to find a solution, where i will write my wanted multiple strings in a file and will call that file by command and write the directory or folder name, where these information can be found.
i found some information from this forum but i could not succeeded.
#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
this code is also not working for me... after run, i get a blank result.txt file
I hope you already experienced the same problem and can help me to get rid of that problem.
If you have any questions, please let me know. I will be happy to answer.
Thanks in advance.
findstr /r /s /m "string1 string2 string3" *.txt
Space seperates search terms.
I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.
I have a folder with 9500 documents inside(let's call if folder1). I want to make a copy of this folder with the structure it have but i want in the new folder(let's call it folder2) all the documents to be blank. Folder1 contains several type of documents. Untill now i have as code :
#echo off
set "source=C:\Users\DM\Desktop\folder1\*.*"
set "dest=C:\Users\DM\Desktop\folder2"
xcopy "%source%" "%dest%" /t/e
for /f "tokens=2 delims=>" %%a in ('xcopy "%source%" "%dest%" /s/h/e/k/f/c/l') do (
for /f "tokens=*" %%b in ("%%a") do type nul >"%%b"
)
pause
The bat goes ok, but after a while there are 3 different errors on the cmd and it stacks there (my windows are greek so i'm trying to translate the errors). One is that "memory is not enough" , the next one is "The system cannot locate the disk(or drive) path specified" (it's one of the two drive/disk i think.) and the last one is "File name syntax, directory or volume label is incorect". It stops at a specific htm file that has "<>" characters on the title.
Can you please tell me what should i add?
Thanks in advance
I hope the code below will help. But it echo the new line to all files. And I couldn't use source as parameter in !fileRelPath:C:\te mp\=!
#echo off
setlocal enabledelayedexpansion
set source=C:\te mp\
set dest=C:\te mp2\
if not exist "%dest%" mkdir "%dest%"
xcopy "%source%*" "%dest%" /t /e
for /F "tokens=*" %%a in ('dir /B /S /A-D "%source%"') do (
set fileRelPath=%%a
set fileRelPath=!fileRelPath:C:\te mp\=!
echo.>"%dest%!fileRelPath!"
)
As Magoo says, this is strange.
Anyway, while finding where this fails, you can do the same with
robocopy "%source%" "%dest%" * /e /create