Solved Help with a bat file

zelarra821

Registered User.
Local time
Today, 08:15
Joined
Jan 14, 2019
Messages
840
Hi guys.

I'm having a problem with a bat file that I can't seem to solve, and it's driving me crazy.

I want to count the files in a folder and its subfolders, but without including a file type or the name of a specific file. This is where the problem lies: no matter how many filters I use, it doesn't give me the result I want.

The bat file code is as follows:

Code:
@echo off

setlocal enabledelayedexpansion

set "FOLDER="%D:\Bandeja de entrada\000 M¢vil""

set "IGNORE_EXT=.sh"

echo.
dir !FOLDER!

for /f %%n in ('dir /b /a-d ^| findstr /v /i "!IGNORE_EXT!" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files.
    @pause
        exit /b
    ) else (
        echo.
        echo Files found.
    @pause
        exit /b
    )
)

endlocal

1742112027267.png


1742112052489.png


I don't want it to count files with a .sh extension initially, but I'd like to know if I want to include a specific file name along with a file type in the future, how would I do it?

Thank you very much.
 
can you use vbscript (.vbs extension)
Code:
Option Explicit

Dim objFSO, objFolder, folderPath, excludeExtensions, totalFiles
Dim exclude, ext, excludeExt

' Specify the folder path
folderPath = "D:\Bandeja de entrada\000 M¢vil\"

' Specify extensions to exclude (in lowercase, without dots)
excludeExtensions = Array("sh")

' Initialize file count
totalFiles = 0

' Create FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
on error resume next
' Check if the folder exists
If objFSO.FolderExists(folderPath) Then
    Set objFolder = objFSO.GetFolder(folderPath)
    CountFiles objFolder
    WScript.Echo "Total files (excluding specified extensions): " & totalFiles
Else
    WScript.Echo "Folder does not exist."
End If

' Recursive function to count files
Sub CountFiles(folder)
    Dim file, subFolder, ext, exclude
    For Each file In folder.Files
        ext = LCase(objFSO.GetExtensionName(file.Name))
        exclude = False
        
        ' Check if the extension is in the exclude list
        For Each excludeExt In excludeExtensions
            If ext = excludeExt Then
                exclude = True
                Exit For
            End If
        Next
        
        ' Increment count if not excluded
        If Not exclude Then
            totalFiles = totalFiles + 1
        End If
    Next
    
    ' Recurse through subfolders
    For Each subFolder In folder.SubFolders
        CountFiles subFolder
    Next
End Sub
 
Awesome.

So, I need set this: if there's more than one file, vbs script should move all files from every single folder. This is bat script I want to rewrite to vbs script:

Code:
@echo off

echo.

SET choice=

SET /p choice=¨Quieres mover los archivos? [S/N]:

IF NOT '%choice%'=='' SET choice=%choice:~0,1%

IF '%choice%'=='S' GOTO Sí
IF '%choice%'=='s' GOTO Sí

IF '%choice%'=='N' GOTO No
IF '%choice%'=='n' GOTO No
IF '%choice%'=='' GOTO No

ECHO "%choice%" introducido incorrecto. Prueba de nuevo.

ECHO.

GOTO start

:Sí

move /y "D:\Bandeja de entrada\000 M¢vil\Audios\*.*" "D:\Bandeja de entrada\Audios"

move /y "D:\Bandeja de entrada\000 M¢vil\Documentos\*.*" "D:\Bandeja de entrada\Documentos"

move /y "D:\Bandeja de entrada\000 M¢vil\Im genes\*.*" "D:\Bandeja de entrada\Im genes"

move /y "D:\Bandeja de entrada\000 M¢vil\V¡deos\*.*" "D:\Bandeja de entrada\V¡deos"

move /y "D:\Bandeja de entrada\000 M¢vil\ZZZ Comprobar\*.*" "D:\Bandeja de entrada\ZZZ Comprobar"

:No

EXIT

ECHO Se han movido los archivos correctamente.

@pause

endlocal

I don't know how to set a msgbox to get that from a vbs file.
 
not tested.
Code:
Option Explicit

Dim objFSO, objFolder, folderPath, excludeExtensions, totalFiles
Dim exclude, ext, excludeExt, sourceFolder, targetFolder, file, fold, i

' Specify the folder path
folderPath = "D:\Bandeja de entrada\000 M¢vil\"

sourceFolder = Array("D:\Bandeja de entrada\000 M¢vil\Audios", "D:\Bandeja de entrada\000 M¢vil\Documentos", "D:\Bandeja de entrada\000 M¢vil\Im genes", "D:\Bandeja de entrada\000 M¢vil\V¡deos", "D:\Bandeja de entrada\000 M¢vil\ZZZ Comprobar")

targetFolder = Array("D:\Bandeja de entrada\Audios", "D:\Bandeja de entrada\Documentos", "D:\Bandeja de entrada\Im genes", "D:\Bandeja de entrada\V¡deos", "D:\Bandeja de entrada\ZZZ Comprobar")

' Specify extensions to exclude (in lowercase, without dots)
excludeExtensions = Array("sh")

' Initialize file count
totalFiles = 0

' Create FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
on error resume next
' Check if the folder exists
If objFSO.FolderExists(folderPath) Then
    Set objFolder = objFSO.GetFolder(folderPath)
    CountFiles objFolder
    WScript.Echo "Total files (excluding specified extensions): " & totalFiles
    If totalfiles <> 0 then
        If Msgbox("Do you want to Move these files?", vbQuestion+vbYesNo)=vbYes Then
            For i = 0 To Ubound(sourceFolder)
                Set objFolder = objFSO.GetFolder(sourceFolder)
                For each file In objFolder.Files
                    file.Move targetFolder(i) & "\" & fso.GetFileName(file.Path)
                Next
            Next
            Wscript.Echo "Files moved to their respective archive folder."
         End If
    End If
Else
    WScript.Echo "Folder does not exist."
End If

' Recursive function to count files
Sub CountFiles(folder)
    Dim file, subFolder, ext, exclude
    For Each file In folder.Files
        ext = LCase(objFSO.GetExtensionName(file.Name))
        exclude = False
        
        ' Check if the extension is in the exclude list
        For Each excludeExt In excludeExtensions
            If ext = excludeExt Then
                exclude = True
                Exit For
            End If
        Next
        
        ' Increment count if not excluded
        If Not exclude Then
            totalFiles = totalFiles + 1
        End If
    Next
    
    ' Recurse through subfolders
    For Each subFolder In folder.SubFolders
        CountFiles subFolder
    Next
End Sub
 
I noticed that

For i = 0 To Ubound(sourceFolder)
Set objFolder = objFSO.GetFolder(sourceFolder)

sourceFolder doesn't get folders path
 
Ok, that's fixed.

Next:

For each file In objFolder.Files
file.Move targetFolder(i) & "\" & fso.GetFileName(file.Path)

If I try to see every single file by "WScript fso.GetFileName(file.Path)", I get nothing.
 
maybe the "current path" has no file on it.
and you need to Echo it (wscript.echo)
 
Fixed.

Here's the error: fso.GetFileName(file.Path) --> objFSO.GetFileName(file.Path)

Thanks a lot.
 

Users who are viewing this thread

Back
Top Bottom