Comparing the number of files in two folders - windows

I want to verify that two folders have the same number of files.
For example if there are 5 files in folder c:\Users\abc\INBOX, I want to verify that there are also 5 files in folder c:\Users\abc\OUTBOX
How can I achieve this?

#echo off
setlocal enableextensions disabledelayedexpansion
call :getNumberOfFilesInFolderList nINBOX "c:\Users\abc\INBOX"
call :getNumberOfFilesInFolderList nFiles "c:\Users\abc\OUTBOX" "c:\Users\abc\OUTBOX\PROC" "c:\Users\abc\OUTBOX\PEND"
if %nINBOX% EQU %nFiles% (
echo SAME number of files
) else (
echo DIFFERENT number of files
)
endlocal
exit /b
:getNumberOfFilesInFolderList variable folder1 [[folder2] ... ]
setlocal enableextensions disabledelayedexpansion
set "variable="
set /a "total=0"
for %%a in (%*) do if not defined variable (set "variable=%%~a" ) else (
for /f %%b in ('dir /a-d "%%~a" 2^>nul ^| findstr /r /c:"^[ ][ ][ ]*[0-9]"') do set /a "total+=%%b"
)
endlocal & set "%~1=%total%" & echo %total%
goto :eof

This should compare two folders.
#echo off
set aa=0&set bb=0
for %%a in ("c:\Users\abc\INBOX\*") do set /a aa+=1
for %%a in ("c:\Users\abc\OUTBOX\*") do set /a bb+=1
if %aa% EQU %bb% (
echo they have the same number of visible files.
) else (
echo the file count is different
)

TRy something like this :
#echo off
set $Folder1="c:\Users\abc\INBOX"
set $folder2="c:\Users\abc\OUTBOX"
set $count=1
setlocal EnableDelayedExpansion
for %%x in (%$Folder1% %$Folder2%) do (
for /f "tokens=1 delims= " %%a in ('dir %%x ^| find /i "File(s)"') do (
set $Total!$Count!=%%a)
set /a $Count+=1)
If %$Total1% Equ %$Total2% (echo Same number of files) else (echo Different number of files)
If your system is not in english you have to change the "File" according with your system language (ie: "Fichier(s)' in French)
EDIT :
To compare more Directory with the FIRST ONE :
#echo off
set $Folder1="c:\Users\abc\INBOX"
set $folder2="c:\Users\abc\OUTBOX"
set $Folder3=c:\Users\abc\OUTBOX\PROC
set $Folder4=c:\Users\abc\OUTBOX\PEND
set $Count=0
setlocal EnableDelayedExpansion
for %%x in (%$Folder1% %$Folder2% %$Folder3% %$Folder4%) do (
for /f "tokens=1 delims= " %%a in ('dir %%x /a-d ^| find /i "File(s)"') do (
call:test %%x %%a
if !$count! Equ 0 set $Ref=%%a
set $Count=1))
exit/b
:test
if !$count! Equ 1 (
If "%$Ref%" Equ "%2" (echo %$Folder1% SAME %1) else (echo %$Folder1% DIFFERENT %1))

Related

Batch file : Encrypted opt out repeats it self for every file

Hi and thanks for answer, so my batch file that is intended to encrypt certain files with certain extensions. So there is my code:
#echo off
setlocal EnableDelayedExpansion
set "Alphabet=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set "lowCase=abcdefghijklmnopqrstuvwxyz"
:a offset "input string" outVar=
setlocal DisableDelayedExpansion
set "inString=%~2"
set maxLen=80
set offset=11
set fname=%random%.%random%.%random%.%random%
setlocal EnableDelayedExpansion
(
for /f "tokens=*" %%X in ('dir /b /s *.encodeme') do (
FOR /f "delims=" %%a IN ('findstr /v /L /i /b /c:"INDEX?" "%%X"') DO (
SET "line=%%a"
call :A %offset% "!line!" a=
echo !a!
type %fname% > %%X
del /Q %fname%
)
)
)>"%fname%"
:A offset "input string" outVar=
setlocal DisableDelayedExpansion
set "inString=%~2"
setlocal EnableDelayedExpansion
for /L %%i in (0,1,61) do (
set /A "i=(%%i + %1) %% 62"
set c["!Alphabet:~%%i,1!"]=!i!
)
set "outVar="
for /L %%i in (0,1,%maxLen%) do (
set "char=!inString:~%%i,1!"
if defined char (
for /F "delims=" %%c in ("!char!") do (
if defined c["%%c"] (
set j=!c["%%c"]!
if "!lowCase:%%c=%%c!" neq "%lowCase%" set /A "j-=26"
for /F %%j in ("!j!") do set "outVar=!outVar!!Alphabet:~%%j,1!"
) else (
set "outVar=!outVar!!char!"
)
)
)
)
(
endlocal
for /F "delims=" %%a in ("%outVar:!=^!%") do endlocal & set "%3=%%a"
)
I have problem with the script that it will repeat its previous encrypted results.
For example, first encrypted file content is ok : Er6HGFJFrq6FJpFr6
Second example file content is repeated from previous on :
Er6HGFJFrq6FJpFr6
C:\Users\HP\Desktop\16383.29528.16703.12516
n6GJp6GJp7GF6
I want it to be just :
n6GJp6GJp7GF6
Sorry for my English, if you could help me, thank you!

Moving specific number of files into newly created numbered folders

I currently have around 550 files in a folder with the same format (.csv) and same headers (all started with the letters "YL").
I wonder if there is a way to splits these files (50 files at a time) (order doesn't matter) into numbered folders? (ex. 1, 2, 3, 4, 5) And also create a subsequent folder for the leftover files?
I have found this scripts and tried to modify it for 50 files, but it looks like it only created a the first folder (subdir1)
#echo off
set /a counter=1
set /a filesperfolder=50
cd dir\dir_main
:loopstart
set dirname=subdir%counter%
md %dirname%
echo %dirname%
dir /b | findstr /v /i "subdir*"> %temp%\temp.txt && for /l %%l in (1,1,%filesperfolder%) do #for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%\temp.txt ^| findstr /r "^%%l:"') do #move %%b %dirname%\%%b >nul
set /a counter=%counter%+1
for /f "tokens=*" %%a in ('type %temp%\temp.txt ^| find /c /v ""') do set _filesmoved=%%a
del %temp%\temp.txt
IF %_filesmoved% LSS 50 goto done
goto loopstart
:done
cls
echo All files were moved!!
pause
exit
I disliked the script you found as it was hard to read and used a temp file to keep track of the list of files. (Also, it evidently doesn't work, so there's that.)
#echo off
SET /a cnt=50
SET /a fnum=0
FOR /F "delims=" %%f IN ('dir /b /a-d *.csv') DO (
CALL :moveFile "%%f"
)
GOTO :end
:moveFile
IF "%cnt%" equ "50" CALL :makeDir
move "%~1" "%fnum%\%~1"
SET /a cnt+=1
GOTO :EOF
:makeDir
SET /a fnum+=1
mkdir %fnum%
SET /a cnt=0
GOTO :EOF
:end
Here is another way to do it. We test if there are still files in the directory, if there is, create a new directory and copy 50 files.
#echo off & setlocal enabledelayedexpansion
set fold_cnt=1
:test
set file_cnt=50
dir /a-d YL*.csv | findstr /IRC:"File(s)"
if %errorlevel% equ 0 (
mkdir !fold_cnt!
) else (
goto :eof
)
for %%i in (YL*.csv) do (
if not !file_cnt! equ 0 (
set /a file_cnt-=1
move /Y "%%i" "!fold_cnt!\%%i"
)
)
set /a fold_cnt+=1
goto test

File count and size of each folder

I have the following batch script which gives the size of each folder in a directory. I need help in tweaking this or creating a new script so it gives the file count of each folders as well:
#echo off
setlocal disabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
for /d %%a in ("%folder%\*") do (
set "size=0"
for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:" "') do if "%%~c"=="" set "size=%%~b"
setlocal enabledelayedexpansion
echo(%%~nxa # !size!
endlocal
)
endlocal
All the information is already in the output of the inner dir command, you only need to change what to retrieve
#echo off
setlocal disabledelayedexpansion
for %%a in ("%~f1.") do set "folder=%%~fa"
for /d %%f in ("%folder%\*") do (
set /a "size=0", "files=0", "directories=0"
for /f "tokens=1,3,5" %%a in ('
dir /-c /a /w /s "%%~ff\*" 2^>nul ^| findstr /b /c:" "
') do if "%%~c"=="" (
set "files=%%~a"
set "size=%%~b"
) else set /a "directories=%%~a/3"
setlocal enabledelayedexpansion
echo(%%~nxf # !size! bytes : !files! files : !directories! directories
endlocal
)
Add this cmd at the end and it will count files in a given dir
setlocal enableextensions
set count=0
for %%x in ("%folder%\*") do set /a count+=1
echo %count%
endlocal
This is inefficient tho, as you are traversing the whole directory twice.
Ideally, you need to combine the two FOR loops into one, just scan the dir once and add the size to the size counter and do count+=1 for each.
Try something like *added spacing to show what i added
set "folder=%~1"
if not defined folder set "folder=%cd%"
set count=0
for /d %%a in ("%folder%\*") do (
set "size=0"
for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:" "') do if "%%~c"=="" set "size=%%~b"
set /a count+=1
setlocal enabledelayedexpansion
echo(%%~nxa # !size!
endlocal
)
echo %count%
endlocal

count length of filenames in batch

my problem is I want to count the length of multiple filenames and save this numbers into a file.
My approach is this:
#echo off
for %%i in (*.txt) do (
set Datei=%%~ni
call :strLen Datei strlen
:strLen
setlocal enabledelayedexpansion
:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
echo.%strlen%>> tmp
)
The problem here is it only works for the first filename and after that it is stuck and does not go on to the next filename.
Just for another alternative, findstr can output the offset of each matching line. Using it over the dir output and substracting the offset of the previous line from the current one (and the CRLF at the end of the line), we get the length of the previous line / file name
#echo off
setlocal enableextensions disabledelayedexpansion
set "lastOffset=0"
for /f "tokens=1,* delims=:" %%a in ('(dir /b *.txt ^& echo(^) ^| findstr /o "^"') do (
if %%a gtr 0 (
set /a "size=%%a - lastOffset - 2"
setlocal enabledelayedexpansion
echo(!fileName! !size!
endlocal
)
set "lastOffset=%%a"
set "fileName=%%b"
)
Just for another alternative of a one-line command:
for %# in (*.txt) do #for /F "delims=:" %G in ('(echo "%~f#"^&echo(^)^|findstr /O "^"') do #if %~G NEQ 0 ( <^NUL set /p "dummy=%~f#|%~z#|"&set /a %~G-5&echo()
or the same in a bit more readable form:
for %# in (*.txt) ^
do #for /F "delims=:" %G in ('(echo "%~f#"^&echo(^)^|findstr /O "^"') ^
do #if %~G NEQ 0 ( <^NUL set /p "dummy=%~f#|%~z#|"&set /a %~G-5&echo()
Unfortunately, unlike the cmd shell, set command does not display its result in a batch script. Therefore, we need to set a string length to an environment variable and then echo its value with delayed expansion enabled:
#ECHO OFF >NUL
SETLOCAL EnableExtensions EnableDelayedExpansion
rem file lengths:
rem for %%A in (*.txt) do echo Name:%%A, Length: %%~zA
rem full path lengths:
for %%# in (*.txt) do (
for /F "delims=:" %%G in ('(echo "%%~f#"^&echo(^)^|findstr /O "^"') do (
if %%~G NEQ 0 (
set /a "length=%%~G-5"
rem echo(%%~f#^|%%~z#^|!length!
echo(%%~f#^|!length!
)
)
)
A slightly modified how to do count length of file name.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(
for /f "tokens=*" %%A in ('dir *.txt /B /S /A:-D ^| findstr /IV "_output.txt"') do (
set "name=%%~nA"
#echo "!name!">"%TMP%\_Temp.txt"
for %%I in ("%TMP%\_Temp.txt") do (
set /a "length=%%~zI"
set /a length-=4
#echo !length!:'!name!'
)
)
)> _output.txt 2>&1
del "%TMP%\_Temp.txt"
MORE /C /P _output.txt
ENDLOCAL
EXIT /B 0
well the solution was coming from you i just moved the parts out of the loop. the code is this:
#echo off
for %%i in (*.txt) do (
set Datei=%%~ni
call :strLen Datei strlen
)
:strLen
setlocal enabledelayedexpansion
:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1
goto :strLen_Loop
(endlocal & set %2=%len%)
echo.%strlen%>> tmp

cmd and exclamation marks

the following code fails for folder names containing exclamation marks. I think I need DelayedExpansion enabled to handle the nested for loops. Any ideas to get this work? Thanks!
#echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"
set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0
for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
Set /a Folders+=1
for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
Set NumberOfFiles=%%i
)
for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
set NumberOfFolders=%%i
)
IF "!NumberOfFiles!" neq "0" IF "!NumberOfFolders!" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
IF "!NumberOfFiles!" neq "0" IF "!NumberOfFolders!" equ "0" set /a FoldersWithOnlyFiles+=1
IF "!NumberOfFiles!" equ "0" IF "!NumberOfFolders!" neq "0" set /a FoldersWithOnlySubFolders+=1
IF "!NumberOfFiles!" equ "0" IF "!NumberOfFolders!" equ "0" (
set /a FoldersEmpty+=1
echo %%a is empty.
)
)
echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
endlocal
I see 3 simple solutions:
1) The slowest method is to use CALL so that you do not need delayed expansion within your loop.
#echo off & setlocal disableDelayedExpansion
set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"
set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0
for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
Set /a Folders+=1
for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
Set NumberOfFiles=%%i
)
for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
set NumberOfFolders=%%i
)
call :incrementCounts
)
echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
echo See dircount.log for list of empty folders.
endlocal
exit /b
:incrementCounts
IF "%NumberOfFiles%" neq "0" IF "%NumberOfFolders%" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
IF "%NumberOfFiles%" neq "0" IF "%NumberOfFolders%" equ "0" set /a FoldersWithOnlyFiles+=1
IF "%NumberOfFiles%" equ "0" IF "%NumberOfFolders%" neq "0" set /a FoldersWithOnlySubFolders+=1
IF "%NumberOfFiles%" equ "0" IF "%NumberOfFolders%" equ "0" (
set /a FoldersEmpty+=1
echo %%a is empty.
)
exit /b
2) A faster method is to temporarily enable delayed expansion only long enough to transfer the values to FOR variables.
#echo off & setlocal disableDelayedExpansion
set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"
set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0
for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
Set /a Folders+=1
for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
Set NumberOfFiles=%%i
)
for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
set NumberOfFolders=%%i
)
setlocal enableDelayedExpansion
for /f "tokens=1,2" %%N in ("!NumberOfFiles! !NumberOfFolders!") do (
endlocal
IF "%%N" neq "0" IF "%%M" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
IF "%%N" neq "0" IF "%%M" equ "0" set /a FoldersWithOnlyFiles+=1
IF "%%N" equ "0" IF "%%M" neq "0" set /a FoldersWithOnlySubFolders+=1
IF "%%N" equ "0" IF "%%M" equ "0" (
set /a FoldersEmpty+=1
echo %%a is empty.
)
)
)
echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
echo See dircount.log for list of empty folders.
endlocal
3) But the way I would handle it is to eliminate the 2 variables that are only used within the loop. They aren't used outside the loop, and they are already derived from FOR variables. Since everything is now a FOR variable, you no longer need delayed expansion.
#echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "dest=%~dpn1"
IF "%1" equ "" Set "dest=%cd%"
set /a Folders=0
set /a FoldersWithSubFoldersAndFiles=0
set /a FoldersWithOnlyFiles=0
set /a FoldersWithOnlySubFolders=0
set /a FoldersEmpty=0
for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
Set /a Folders+=1
for /f "usebackq tokens=*" %%N in (`DIR "%%a" /A-D /B 2^>NUL^| FIND /C /V ""`) do (
for /f "usebackq tokens=*" %%M in (`DIR "%%a" /AD /B 2^>NUL^| FIND /C /V ""`) do (
IF "%%N" neq "0" IF "%%M" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
IF "%%N" neq "0" IF "%%M" equ "0" set /a FoldersWithOnlyFiles+=1
IF "%%N" equ "0" IF "%%M" neq "0" set /a FoldersWithOnlySubFolders+=1
IF "%%N" equ "0" IF "%%M" equ "0" (
set /a FoldersEmpty+=1
echo %%a is empty.
)
)
)
)
echo Found %Folders% folders below "%dest%".
echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
echo %FoldersWithOnlyFiles% folders containing files only.
echo %FoldersWithOnlySubFolders% folders containing subfolders only.
echo %FoldersEmpty% folders are empty.
echo See dircount.log for list of empty folders.
endlocal

Resources