batch script to skip file is file name contains a specific prefix

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nets
Posts: 1
Joined: 04 Feb 2020 02:51

batch script to skip file is file name contains a specific prefix

#1 Post by nets » 04 Feb 2020 03:01

hi,

I have below script that add a prefix to all files available in a folder.
how to include another if statement that if a file name already contains A23_ as prefix in the folder it should skip it?? pls help. thxs


@echo off

set "dir=C:\TEST"
set "pfx=A23_"

setlocal enabledelayedexpansion
for /r "%dir%" %%A in (*.txt) do (
set "txt=%%~nA"
if not "!txt:~0,13!"=="%pfx%" ren "%%A" "%pfx%%%~nxA"
)

pause

Squashman
Expert
Posts: 4198
Joined: 23 Dec 2011 13:59

Re: batch script to skip file is file name contains a specific prefix

#2 Post by Squashman » 05 Feb 2020 10:06

Your code is comparing 13 characters of the file name to 4 characters of your prefix string. That will never be true.

Post Reply