0

Okay I am trying to check the variable that contains the string pg but it says yes for each file name. Eventually based on each file that my have g, pg, or neither I will do further things which I might have to create other questions for or maybe one cool btach scripter here can teach me if i get stuck:

@ECHO OFF
setlocal enabledelayedexpansion

set message=Hello World 
echo %message%

echo -------------------
for /f "tokens=1-2 delims=_" %%i in ('dir /B /A-D "*.pdf"') do (
    set var=%%i
    echo !var!
    set var2=pg

    If NOT "%var%"=="%var:%var2%=%" (
            echo Yes
    ) else (
            echo No
    )
)


PAUSE

Here is my output:

Hello World
-------------------
00000
Yes
00267
Yes
20859
Yes
G0454
Yes
P01339
Yes
PG0435
Yes
PG2522
Yes
testItAll
  • 49
  • 8
  • 2
    If you are looking for files with `PG` in the filename why wouldn't you just use that as part of your file mask? `dir /b /a-d *PG*.pdf`. Regardless of that you have a variable expansion problem. Not sure why you chose to use delayed expansion with the `echo` command but none of your other commands. – Squashman Sep 24 '21 at 18:03
  • @Squashman honestly i have no idea what i am doing lol just started to do a batch script to move renames files and move them into other folders, end goal. But for now i am taking baby steps so I am checking if the name starts with PG because those pdf files and others will need to be renamed and put into certain folders. – testItAll Sep 24 '21 at 18:12
  • 3
    To check if a variable contains a string use `if "!var:string=!" neq "!var!" (echo Yes) else (echo No)`. However, to _select files that starts with a given string_ use the appropriate wild-card, as Squashman suggested: `PG*.pdf` – Aacini Sep 24 '21 at 18:14
  • @Aacini thank you, your code/syntax worked for me now i can do various else if situations because the pdf files have various letters that require more actions based on the letters therefore why i grab all pdfs because they are already in one folder, hundreds of them – testItAll Sep 24 '21 at 18:24
  • Be aware that you may use complicated wild-cards. For example: `PG???12??.pdf` etc... – Aacini Sep 24 '21 at 18:27
  • @Aacini now that i further progressed I am stuck. Since my var value is the first string cut of by an underscore i need my the full file name so that i can rename them to the substring i created. How can i go about pulling the full file name within this loop? – testItAll Sep 24 '21 at 18:53
  • just to show you an alternative: `echo %%i|findstr /i "pg" && echo yes||echo no`. No delayed expansion needed. – Stephan Sep 24 '21 at 20:33

0 Answers0