Trying to get this to work.
Getting this line from dir: 3 File(s) 7,332,731,128 bytes and trying to just get the number of files to use for addition.
#Echo ON
dir %1 /a:h | find "File(s)" > hidden.txt
dir %1 | find "File(s)" > reg.txt
set /p hidden=<hidden.txt
echo %hidden%
IF /I "%hidden%"=="File Not Found" (
Set hidden = 0
) Else (
Set hidden=%hidden~-29%)
echo %hidden%
Set /p reg=<reg.txt
IF /I "%hidden%"=="File Not Found" (
set reg = 0
) ELSE (
Set reg=%reg~-29%)
set /a total = %reg% + %hidden%
Echo The total number of files in the %1 directory is: %total%. This includes hidden files.
Echo.
Echo The total number of non-hidden files in the %1 directory is: %reg%.
Echo.
Echo The total number of hidden files in the %1 directory is: %hidden%
you don't need temp files which only will slow down your script:
if you want to count the hidden files try this
#echo off
set counter=0
for /f %%# in ('dir /a:h-d "%~1"') do (
set /a counter=counter+1
)
echo hidden files=%counter%
to count all files
#echo off
set counter=0
for /f %%# in ('dir /a:-d "%~1"') do (
set /a counter=counter+1
)
echo all files=%counter%
This is just another For loop option:
#For /F %%A In ('Dir/AH-D-L "%~1" 2^>Nul') Do #If Not "%%A"=="0" Set "fileCount=%%A"
#Echo %fileCount%
#Pause
Remove -L, if you wish not to exclude junction points from your file count.
I've got the same result with just two FOR loops. Using sentences between single quotes inside the parentheses we can execute commands and store results.
I've left the same Result Text just to let you compare and see if this is what you're looking for:
CODE:
#echo off
setlocal
for /f %%A in ('dir "%~1" /a-h-d /b ^| find /V /C ""') do set "visCount=%%A"
for /f %%A in ('dir "%~1" /ah-d /b ^| find /V /C ""') do set "hidCount=%%A"
set /a totalCount = visCount + hidCount
echo.
echo The total number of files in the %1 directory is: %totalCount%. This includes hidden files.
echo.
echo The total number of non-hidden files in the %1 directory is: %visCount%.
echo.
echo The total number of hidden files in the %1 directory is: %hidCount%
With /B parameter of DIR command I only get one line per file and no more text, and with /A I can select Hidden or not files and no directories.
Then SET /A command let us use arithmetics operations.
Related
I am trying to compare the number of files on a local folder to that of the source folder which is in a server.
I have the following lines in a batch file below but i always get the same value for both no matter what.
#for /f %%a in ('2^>nul dir "%local_folder%" /a/b/-o/-p/s^|find /v /c ""') do set n=%%a
echo Total files in Local Folder: %n%
pause
#for /f %%a in ('2^>nul dir "%source_folder%" /a/b/-o/-p/s^|find /v /c ""') do set m=%%a
echo Total files in Source Folder: %m%
pause
if %n%==%m% (copy update_folder local_folder.\ && goto end) else (goto copy_all_files)
pause
I am getting the same value for when I try to count the files in the separate locations. I only want to count the visible files with extension *.wav on each folder.
UPDATE: RESOLUTION I found is below,
:file_count_chck
set folder_cf=".\folder-A\*.wav"
set folder_lib="\\folder-B\*.wav"
dir %folder_cf% > A.txt
dir %folder_lib% > B.txt
set count_cf=0
for /f %%x in (A.txt) do set /a count_cf+=1
set count_lib=0
for /f %%y in (B.txt) do set /a count_lib+=1
set data_lines=5
set /A count_cf = %count_cf%-%data_lines%
set /A count_lib = %count_lib%-%data_lines%
echo.
echo Checking Sound folder...
echo Sound files on CF card = %count_cf%
echo Sound files on library = %count_lib%
del "A.txt" "B.txt"
set /a difference_count=%count_lib%-%count_cf%
if %difference_count% EQU 0 (echo Ok && goto wav_update) else (echo Detected missing files and correcting && goto copy_all_wav)
Hope someone finds this useful.
I have many folders in a directory which I need to rename with a fixed base name and a progressive number starting from 1 to infinite.
Path of folders have space and base folder is D:\Programmi Installati.
Example of folders to rename:
log_1
log_2
log_04-01-2019 15-15-11,51
log_01-01-2019 8-22-14,19
log_27-12-2018 14-23-18,28
log_aaaa
log_bbbb
log_5
log_6
log_02-01-2019 6-21-17,34
log_03-01-2019 21-18-16,22
Example of wanted folder names:
log_1
log_2
log_3
log_4
log_5
log_6
log_7
log_8
log_9
log_10
log_11
log_12
The numbers of folder to rename can be large, but the structure is the same.
I tryed more batch file but all fail when there are some folders with the wanted name (example log_5 or log_1)
The order is not important it is important that all the folders starting with "log" are renamed with an incrental number.
Code already tryed without success
:: 1 code
#echo off
setlocal enabledelayedexpansion
set counter=
for /d %%a in ("D:\Programmi Installati\log_*") do (
set /a counter+=1
ren "%%~fa" "log_!counter!"
)
pause
:: 2 code
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Counter=1"
for /F "delims=" %%I in ('dir "D:\Programmi Installati\log*" /AD /B /ON 2^>nul') do ren "D:\Programmi Installati\%%I" "log_!Counter!" & set /A Counter+=1
endlocal
pause
:: 3 code
#ECHO OFF
#setlocal enabledelayedexpansion
Rem | Folder Path & CD To Location
Set "Folder=D:\Programmi Installati\"
CD %Folder%
Rem | Get Raw File Name
Set "Number=1"
for /F "tokens=*" %%A in ('dir "log*" /S /b /AD') do (
Rem | Rename Folder || Raw Name - %%~n1
rename "%%~nA" "log_!Number!"
Rem | Add One To Number
set /a "number+=1"
)
Goto :EOF
PAUSE
The codes over works only if there is no desired directory name in the directory otherwise do not rename folders.
This batch works differently, it
skips folders with the proper naming scheme (so they keep the number)
increments a counter and fills in possible gaps
:: Q:\Test\2019\01\11\SO_54149437.cmd
#Echo off
Pushd "D:\Programmi Installati\" || (Echo couldn't change dir&pause&goto :eof)
set Cnt=0
for /f "delims=" %%A in (
'dir /B /AD log_* ^| findstr /iV "^log_[0-9][0-9]*$" '
) Do Call :RenInc "%%A"
PopD
Goto :Eof
:RenInc
Set /A Cnt+=1
if Exist "log_%Cnt%" goto :RenInc
Ren "%~1" "log_%Cnt%"
The resulting names (there are only eleven, not twelve)
log_1
log_10
log_11
log_2
log_3
log_4
log_5
log_6
log_7
log_8
log_9
Is it possible to do this?
In a folder i have files with same initial names
Example:
Main folder
-Quest2323231.txt
Quest2343434.txt
Quest2343435.txt
Fund103.txt
Fund102.txt
I have a config file (abc.config) in which i have name of file on which i need to count and move them . If the count is more than 2 then i need to move them.
In this case for e g I need to find files which have name as 'Quest'
Appreciate you help on this.
#echo off
setlocal enableextensions
set "number="
for /f "tokens=1" %%a in (
'dir /a-d /-c "c:\mainfolder\quest*" 2^>nul^|findstr /b /c:" "'
) do if not defined number set "number=%%a"
if not defined number set "number=0"
echo %number%
Untested: This expects text in the first line of abc.config which is the file information such as Quest in the example and if there are more than 2 matching files in the source folder then it will echo a move command to move them to the target folder.
Change *%file%* to %file%* in two places if you want to match only the start of the filename.
Remove the echo to actually perform the move commands.
#echo off
set "source=c:\mainfolder"
set "target=d:\target\folder"
set /p "file=" < "abc.config"
for /f %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" 2^>nul^|find /c /v "" ') do set "number=%%a"
if %number% GTR 2 for /f "delims=" %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" ') do (
echo move "%source%\%%a" "%target%"
)
pause
I have recently started to learn how to make batch files. I have a folder that contains bunch of internet related log files. When I run the .cmd file (located in the same folder) I want it to be able to find out how many log files are in the folder and make a numbered menu from it. So lets say there are twenty files in the folder, then the user must be able to select from 1 to 21. 21 will close the batch file. Here is what I have done so far:
#echo off
setlocal enableextensions enabledelayedexpansion
set RawData1=TempData%random%.tmp
set FileCtr=0
:MAIN
dir *.log /b | findstr /i /n ".log" > %RawData1%
for /f "tokens=1 delims=:" %%a in (%RawData1%) do set FileCtr=%%a
set /a ExitCode=%FileCtr% + 1
set UserChoice=%ExitCode%
echo.
echo +++++++++++++++++++++++++++
echo Weblog File Viewer
echo +++++++++++++++++++++++++++
for /f "tokens=1-2 delims=:." %%a in (%RawData1%) do echo %%a. %%b
echo %Exitcode%. To Quit.
set /p UserChoice= Choose item number from menu (%UserChoice%):
echo\
echo user entered: %UserChoice%
pause
:THEEND
del /q %RawData1%
So what this batch file can do for now is that it figures out the number of log files and makes a numbered menu from it. Of course it won't show the filetype which is how I wanted it. So "Kelley-Blue-Book.log" for example is shown only as "Kelley-Blue-Book". However, if the user selects say number 4 from the list the program will terminate because I couldn't figure out how to make it actually open the desired log file using notepad.
This should do what you want:
#echo Off
setlocal EnableDelayedExpansion
set "Count=0"
pushd "%~dp0"
echo.
echo +++++++++++++++++++++++++++
echo Weblog File Viewer
echo +++++++++++++++++++++++++++
for %%A in (*.log) do (
set /a "Count+=1"
set "Menu[!Count!]=%%~fA"
set "Number= !Count!"
echo !Number:~-3!. %%~nA
)
set /a "Count+=1"
set "Number= %Count%"
echo %Number:~-3%. To Quit.
:Prompt
set "UserChoice="
set /p "UserChoice= Choose item number from menu (%Count%):"
if not defined UserChoice goto Prompt
set "UserChoice=%UserChoice:"=%"
if "%UserChoice%"=="%Count%" goto Done
for /f "tokens=1,* delims==" %%A in ('set Menu') do (
if /i "Menu[%UserChoice%]"=="%%~A" (
notepad "%%~fB"
set "UserChoice="
)
)
if defined UserChoice echo Invalid Choice.
goto Prompt
:Done
popd
endlocal
exit /b 0
Let me know if you want any explanations.
#echo off
setlocal enableextensions
set RawData1=TempData%random%.tmp
rem Get numbered list of files
dir /b "*.log" | findstr /i /n ".log" > %RawData1%
rem We could use 0 as exitCode,
rem but to keep original behaviour
rem lets count the number of files
for /F "tokens=*" %%f in ('type %RawData1% ^| find /c /v "" ') do set /A ExitCode=%%f + 1
if %ExitCode%==0 (
echo No log files
goto endProcess
)
rem show menu
for /f "tokens=1-2 delims=:." %%a in (%RawData1%) do echo %%a. %%b
echo %Exitcode%. To Quit.
set UserChoice=%ExitCode%
set /p UserChoice= Choose item number from menu (%UserChoice%):
if "%UserChoice%"=="" goto :EOF
if "%UserChoice%"=="%ExitCode%" goto endProcess
rem Search indicated file in list
set SelectedFile=
for /f "tokens=2 delims=:" %%f in ('findstr /B "%UserChoice%:" %RawData1%') do set SelectedFile=%%f
if "%SelectedFile%"=="" (
echo Incorrect selection
goto endProcess
)
if not exist %SelectedFile% (
echo File deleted
goto endProcess
)
notepad %SelectedFile%
:endProcess
del /q %RawData1%
I using a Batch script into a .bat file to get the last folder from an absolute path and then compare with a string. For example I have: C:\Scripts\ (from where I start the run.bat) and sub-folders C:\Scripts\a\, C:\Scripts\b\results, C:\Scripts\c\results. I want to search for folders that have their names = "results" and when I found it to do some stuff (for example to increment a counter).
set /A Counter=0
for /d /r %%F in (*.*) do (
set path=%FF
rem if the last folder from the path is = "results" then do some stuff
if path.contains("results") set /A Counter+=1
)
echo %Counter%
#ECHO OFF
SETLOCAL
set Counter=0
for /f %%F in ('dir /s/b/ad') do (
FOR /f %%p IN ("%%F") DO (
IF /i "%%~nxp"=="results" set /A Counter+=1
)
)
ECHO %counter%
On the other hand,
DIR /s/ad/b |FINDSTR /i /e "\results"|FIND /c /v ""
will display the same count.
BUT Either of these methods WILL count ANY appearance of a "results" directory - whether it's at the lowest level or not, so it WILL be counted if ...\results has subdirectories.