我对创建批处理文件(以及一般而言的编程)非常陌生,并且想寻求有关此任务的帮助。我需要创建一个批处理文件,可以将其放入网络文件夹中并运行,以便计算所遇到的每个PDF,TIF和TIFF文件名的所有字符。我想让它搜索运行它的目录以及其中的任何子文件夹,然后在末尾返回总数。
到目前为止,它已将每个文件都计数在内,但我想对其进行调整以从字符数中排除特殊字符(主要是下划线)。我也没有能够成功获得它,以便最后给我一个总数。在在线研究教程的过程中,我已经成功地完成了一些更改,但是不幸的是,现在我已经花光了时间。任何帮助或指导将不胜感激。
@ECHO OFF
SETLOCAL
PUSHD %~dp0
FOR /R %%G in (*.PDF *.TIF *.TIFF) DO (
SET FileName=%%~nG
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /l %%L IN (2048,-1,0) DO IF NOT DEFINED CH (
SET CH=!FileName:~%%L,1!
IF DEFINED CH SET /a CH=1+%%L & ECHO !FileName!: !CH! Characters
)
ENDLOCAL
)
POPD
ECHO:
ECHO Total Number of Characters: %Total%
REM Not yet sure where to put Set /a Total=%Total%+%CH%
ECHO:
PAUSE
GOTO :EOF
编辑:对于可能偶然发现此线程的其他任何人。我无法弄清楚如何使用Batch完全实现此目的,但是我可以在PowerShell中做到这一点。这是我想出的PS脚本。
# This script looks through all folders and subfolders that are sitting next to or below the powershell file. It gives the total file and character count of all PDFs, TIFFs, and TIFs that it finds.
# Clear all default PowerShell messages and remove from view.
Clear-Host
# Make the view more orderly.
Write-Output "" "---Processing Files---" ""
# Look through the current directory and all sub-folders for the names of .pdfs, .tifs, and .tiffs and show them as a list.
Get-ChildItem -Recurse -Name -Include *.pdf,*.tif,*.tiff
# Get the total number of files.
$LineCount = (Get-ChildItem -Recurse -Name -Include *.pdf,*.tif,*.tiff | Measure-Object -Line).Lines
# Set ChracterCount to the complete list of files and select ONLY the name portion so that no folder names are shown.
$CharacterCount = (Get-ChildItem -Recurse -Include *.pdf,*.tif,*.tiff | Select -ExpandProperty Name)
# Replace all of the extensions and underscores in the filenames with nothing so that we only have the characters that were manually typed by someone.
$CharacterCount = $CharacterCount -Replace ".pdf","" -Replace ".tiff","" -Replace ".tif","" -Replace "_",""
# Gives us the final character count as a single number.
$CharacterCount = ($CharacterCount | Measure-Object -Character).Characters
# Show the viewer the final counts.
Write-Output "" "---All Files Processed---" "" "" "Total File Count: $LineCount" "" "" "Total Character Count: $CharacterCount" "" ""
# Gives the final closing message to the viewer so that they can exit with any key. Does not show message when script is run from ISE.
if ($Host.Name -eq "ConsoleHost")
{
Write-Host "Press Any Key to Exit."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}