Batch CRC-tool

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
darkside999
Junior Member
Junior Member
Posts: 14
Joined: 2012-12-02, 21:58 UTC

Batch CRC-tool

Post by *darkside999 »

Hi. I have a little problem

I want to calculate CRC of different directory. Is there a way to use crc tools in a sequential mode?

Example:


Dir0001
Dir0002
Dir0003
......

I want to have

Dir0001.sfv
Dir0002.sfv
Dir0003.sfv

If I select all the 3 dirs I will have a only "parent dir name".sfv

If I use the CRC on the single dir is perfect but the performance are very low

I would like to use a batch to select the dirs and launch the crc tools in sequential way to obtain unattended process and the full speed

Any help will be appreciated

Thanks
User avatar
ZoSTeR
Power Member
Power Member
Posts: 1015
Joined: 2004-07-29, 11:00 UTC

Post by *ZoSTeR »

You can't do that in TC afaik. Here's a PowerShell script you might find useful.

It will create individual .md5 files for all passed folders. It will only accept folders and it won't overwrite existing .md5 files.

Put it on a button with
Command:

Code: Select all

powershell.exe
Parameter:

Code: Select all

-NoExit -ExecutionPolicy remotesigned -Command "&{&'C:\PathTo\CreateChecksum.ps1' -listFile '%L'}"
There are three optional switches: -sha1, -insidefolder and -donttouch
-sha1 turns on the SHA1 algorithm instead of MD5 (CRC32/SFV is not available in the Get-FileHash function)
-insidefolder puts the checksum files inside the individual folders instead of their parent folder
-donttouch restores the last write time of the folders so it won't mess up the original date sorting (only used in combination with -insidefolder)
Activating them would look like this:
Parameter:

Code: Select all

-NoExit -ExecutionPolicy remotesigned -Command "&{&'C:\PathTo\CreateChecksum.ps1' -sha1 -insidefolder -donttouch -listFile '%L'}"
Save as "CreateChecksum.ps1" and adjust the path of the button parameter:

Code: Select all

# TC Button Parameter:
# powershell.exe
# -NoExit -ExecutionPolicy remotesigned -Command "&{&'C:\PathTo\CreateChecksum.ps1' -sha1 -insidefolder -donttouch -listFile '%L'}"

Param(
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string]$listFile,
    [switch]$insidefolder,
    [switch]$sha1,
    [switch]$donttouch
)

if ($sha1)
{
    $algo = "SHA1"
    $extensionOutputFile = ".sha"
}
else
{
    $algo = "MD5"
    $extensionOutputFile = ".md5"
}

$contentListFile = Get-Content -Path $listFile

Write-Output ("Calculating $algo checksum for " + $contentListFile.Count.ToString() + " folders...")
$timeStart = Get-Date

foreach ($folder in $contentListFile)
{
    $folderItem = Get-Item -LiteralPath $folder
    $nameParentFolder = Split-Path -Path $folderItem -Parent
    $isFolder = Test-Path -LiteralPath $folderItem -PathType Container
   
    if ($isFolder)
    {
        if ($insidefolder)
        {
            $nameOutputFile = $folderItem.FullName + $folderItem.Name + $extensionOutputFile
            $outputFolder = Get-Item -LiteralPath $folderItem.FullName
            $lastWriteTimeOutputFolder = $outputFolder.LastWriteTime
        }
        else
        {
            $nameOutputFile = $nameParentFolder + '\' + $folderItem.Name + $extensionOutputFile
        }

        if (Test-Path -LiteralPath $nameOutputFile)
        {
            Write-Output ("`n" + $nameOutputFile + " already exists. Skipping")
        }
        else
        {
            $pathFolderItem = $folderItem.FullName.ToString().Replace('[','`[').Replace(']','`]')
            $colFiles = Get-ChildItem -Path $pathFolderItem -Recurse -File
            foreach ($file in $colFiles)
            {
                $hashFile = Get-FileHash -LiteralPath $file.FullName -Algorithm $algo
                $relativeName = $file.FullName.Remove(0, $folderItem.FullName.Length)
               
                if ($insidefolder)
                {
                    $finalString = $hashFile.Hash + ' *' + $relativeName
                }
                else
                {
                    $finalString = $hashFile.Hash + ' *' + $folderItem.Name + '\' + $relativeName
                }
               
                Out-File -LiteralPath $nameOutputFile -InputObject $finalString -Append 
            }

            if ($donttouch -and $insidefolder)
            {
                $outputFolder.LastWriteTime = $lastWriteTimeOutputFolder
            }
        }
    }
}

$timeEnd = Get-Date
$timeSpan = New-TimeSpan -Start $timeStart -End $timeEnd
Write-Output ("`nDone. Duration: " + $timeSpan.ToString("hh\:mm\:ss\,fff")) 
If you're running Win7 you need to install PowerShell v4.

*Edit:
Added "-insidefolder", "-sha1" and ExecutionPolicy
*Edit2:
Added "-donttouch"
*Edit3:
Bugfix
*Edit4:
Handle special characters
Last edited by ZoSTeR on 2015-03-09, 11:19 UTC, edited 18 times in total.
User avatar
hlloyge
Member
Member
Posts: 131
Joined: 2006-11-02, 23:14 UTC

Post by *hlloyge »

Just for info, if it's not working with an error 'cannot be loaded because running scripts is disabled on this system' start powershell as administrator and run this command 'Set-ExecutionPolicy RemoteSigned'.
And to ZoSTeR, if you can tell me what to modify inside the script to save md5 files INSIDE the folder it has scanned, this could be the most useful script ever, at least for me... so it looks like this:

Folder_1
--Folder_1.md5
Folder_2
--Folder_2.md5

Thank you.
User avatar
ZoSTeR
Power Member
Power Member
Posts: 1015
Joined: 2004-07-29, 11:00 UTC

Post by *ZoSTeR »

@hlloyge:

No problem. See the updated code. I also added the ExecutionPolicy.
darkside999
Junior Member
Junior Member
Posts: 14
Joined: 2012-12-02, 21:58 UTC

Post by *darkside999 »

THANKS !!!

After the fixes it's simply perfect.

Thank you
User avatar
hlloyge
Member
Member
Posts: 131
Joined: 2006-11-02, 23:14 UTC

Post by *hlloyge »

It doesn't work for me now... when I start it on two selected folders, I get this:

Code: Select all

Calculating SHA1 checksum for 2 folders...
Split-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Program Files\totalcmd\CreateChecksum.ps1:33 char:42
+     $nameParentFolder = Split-Path -Path $folderItem -Parent
+                                          ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Split-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCo
   mmand

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Program Files\totalcmd\CreateChecksum.ps1:34 char:33
+     $isFolder = Test-Path -Path $folderItem -PathType Container
+                                 ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCom
   mand

Split-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Program Files\totalcmd\CreateChecksum.ps1:33 char:42
+     $nameParentFolder = Split-Path -Path $folderItem -Parent
+                                          ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Split-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCo
   mmand

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Program Files\totalcmd\CreateChecksum.ps1:34 char:33
+     $isFolder = Test-Path -Path $folderItem -PathType Container
+                                 ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCom
   mand


Done. Duration: 00:00:00,134
This is my version:

Code: Select all

PS C:\Windows\System32\WindowsPowerShell> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34014
BuildVersion                   6.3.9600.17090
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2
I am runing Windows 8.1 64 bit, and I don't know what is wrong :) and the catch is, on other set of folders works like a charm. Both are with ASCII characters only, but one are created locally, and others are copied from linux samba share.
I can access them normally.
User avatar
ZoSTeR
Power Member
Power Member
Posts: 1015
Joined: 2004-07-29, 11:00 UTC

Post by *ZoSTeR »

Hmm strange. You could try the following:
Replace line 32

Code: Select all

$folderItem = Get-Item -Path $folder"
with

Code: Select all

Write-Output $folder
$folderItem = Get-Item -LiteralPath $folder
This will write the folder strings to the console and -LiteralPath ignores any wildcard characters that might get in the way.
User avatar
hlloyge
Member
Member
Posts: 131
Joined: 2006-11-02, 23:14 UTC

Post by *hlloyge »

Well, nothing happens. Just this:

Code: Select all

Calculating MD5 checksum for 21 folders...
(lists 21 folders in 1 second)
Done. Duration: 00:00:00,046
At least no red letters.
Changed sha1 to md5, nothing.
Is it codepage problem? I've copy-pasted both lines in place of line 32, so I have now 1 line more.
User avatar
ZoSTeR
Power Member
Power Member
Posts: 1015
Joined: 2004-07-29, 11:00 UTC

Post by *ZoSTeR »

I've updated the code to handle problematic characters.
Tested with local drives and UNC paths:

Code: Select all

Folder with ' two ' single quotes\
Folder with [ open and ] closing bracket\
Folder with [ open bracket\
Folder with ] closing bracket\
Folder with `Backtick\
Folder with ' single quote\
Folder with $dollar and #hash\
Folder with .dot\
@hlloyge:
If it still doesn't work please check if the folders/files have the "hidden" or "system" attribute. Also compare the file permission between the working and failing folders.
User avatar
Ovg
Power Member
Power Member
Posts: 756
Joined: 2014-01-06, 16:26 UTC

Post by *Ovg »

2ZoSTeR

It is working for me, even with Cyrillic file/folder names :-)

Many thanks!
It's impossible to lead us astray for we don't care even to choose the way.
#259941, TC 11.01 x64, Windows 7 SP1 x64
User avatar
hlloyge
Member
Member
Posts: 131
Joined: 2006-11-02, 23:14 UTC

Post by *hlloyge »

Now it works like a charm! On local folders, on SMB share on Ubuntu OS.
Thank you very much!
ccuappz
Junior Member
Junior Member
Posts: 5
Joined: 2009-12-19, 09:52 UTC

Post by *ccuappz »

I sometimes search for *.sfv or *.md5 then feed them to list box and run verify checksums.
HTH
Post Reply