Here is a simple batch file solution for this task:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("C:\Folders\* CHK#*") do call :ProcessFile "%%I"
endlocal
goto :EOF
:ProcessFile
set "FileName=%~nx1"
set "CheckSum=%FileName:* CHK#= CHK#%"
call set "FolderName=%%FileName:%CheckSum%=%%"
md "%~dp1%FolderName%" 2>nul
move /Y %1 "%~dp1%FolderName%"
goto :EOF
The string after CHK#
must not have an equal sign or one or more percent signs.
The subroutine ProcessFile
assigns with a string substitution the string part beginning from first occurrence of CHK#
to end of file name to environment variable CheckSum
.
One more string substitution is used to remove the checksum string from file name to get the folder name. This command line is double parsed by Windows command processor because of command call
to replace on first parsing %CheckSum%
by current value of environment variable CheckSum
and replace both %%
by just a single %
. On second parsing the remaining set "FolderName=%FileName: CHK#123=%"
is processed resulting in assigning to environment variable FolderName
the string FIRSTNAME LASTNAME
for the first file name example. See also How does the Windows Command Interpreter (CMD.EXE) parse scripts?
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.