Batch to check filename prefix to process

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Batch to check filename prefix to process

Post by MigrationUser »

02 Oct 2007 08:42
roxy2k

Hi,

need help for NT batch coding of following:

Code: Select all

    check filename prefix
    if filename prefix = "D" then
       echo "File prefix D"
    else
       echo "File prefix not D"
    endif
Thanks for your help in advance.

----------------------------

#2 02 Oct 2007 09:52
bluesxman


I've probably over complicated this a bit, but I did that to enable easier customisation on your part.

Code: Select all

@echo off

REM required to allow use of ! for delayed variable expansion
setlocal enabledelayedexpansion

set "_prefix=D" & REM prefix to match
set "_offset=0" & REM where the substring should start (0=first character)
set "_#chars=1" & REM number of characters to match (needs be a count of the chars in "_prefix")

REM process all passed files in sequence
for %%a in (%*) do (
    REM store just the name and extension of the file path as "_temp"
    set "_temp=%%~nxa"

    REM check the defined substring of "_temp" to see if it matches "_prefix"
    if /i "!_temp:~%_offset%,%_#chars%!" EQU "%_prefix%" (set "_verdict=") ELSE (set "_verdict=not ")

    echo:"%%~a" - prefix is !_verdict!%_prefix%
)

endlocal

pause
Simply drop the file(s) onto the script (or a short-cut thereto); alternatively you could issue them as parameters when running from a CMD window.

cmd | *sh | ruby | chef

----------------------------

#3 02 Oct 2007 10:35
roxy2k
bluesxman wrote:

I've probably over complicated this a bit, but I did that to enable easier customisation on your part.

@echo off

REM required to allow use of ! for delayed variable expansion
setlocal enabledelayedexpansion

set "_prefix=D" & REM prefix to match
set "_offset=0" & REM where the substring should start (0=first character)
set "_#chars=1" & REM number of characters to match (needs be a count of the chars in "_prefix")

REM process all passed files in sequence
for %%a in (%*) do (
REM store just the name and extension of the file path as "_temp"
set "_temp=%%~nxa"

REM check the defined substring of "_temp" to see if it matches "_prefix"
if /i "!_temp:~%_offset%,%_#chars%!" EQU "%_prefix%" (set "_verdict=") ELSE (set "_verdict=not ")

echo:"%%~a" - prefix is !_verdict!%_prefix%
)

endlocal

pause

Simply drop the file(s) onto the script (or a short-cut thereto); alternatively you could issue them as parameters when running from a CMD window.
Thanks you for your help, let me test it and let you know.
Post Reply