Batch File scripting

Hi Experts!

I need to generate a .txt file via batch script that contains a list of file names in a single directory.

the files in the folder are numbered numerically
4400_XXXX.sql
4903_YYYY.sql

They are all '.sql' files

My starting file (presently, but will change in the future) is 4081_world.sql

There are a few constraints that are holding me up:

1) there are more files in the folder than i need (see current file list in code section)
2) my 'starting' file is somewhere in the middle, but i know the exact name (presently 4081_world.sql)
3) my generated file must contain the filenames from the starting file to the end (see result list in code section, ie i need to ignore anything before my starting file)
4) the files i need sometimes contain the word 'world' and sometimes don't, but will never contain 'characters' or 'realmd'
5) the files i need to ignore will always contain the words 'characters' or 'realmd'

below, in the code section, is the current directory listing of all the 'sql' files present in the folder, however more will be added in the future.

I have also included what the 'result' should be with the current listing
BEGIN CURRENT FILE LIST
0000_3.0.9_old.sql
3008_1437_TC1_world_scripts.sql
3018_spell_linked_spell.sql
3030_world_scripts.sql
3039_world_spell_linked_spell.sql
3070_TC1_1463_world_scripts.sql
3070_world_spell_proc_event.sql
3075_TC1_1465_world_scripts.sql
3089_world_trinity_string.sql
3121_world_scripts.sql
3148_mangos_7776_01_world_npc_spellclick_spells.sql
3178_mangos_7777_01_world_spell_proc_event.sql
3204_mangos_7796_01_world_command.sql
3204_mangos_7796_02_world_trinity_string.sql
3210_mangos_7802_01_characters_character_achievement.sql
3210_mangos_7802_02_characters_character_achievement_progress.sql
3233_world_scripts_naxx.sql
3235_characters.sql
3235_world.sql
3257_world_waypoint_data_converter.sql
3258_world_creature_addon_(waypoint).sql
3263_world_scripts_missing_in_full.sql
3314_mangos_7823_01_world_item_template.sql
3320_worldspell_enchant_proc_data.sql
3323_world_spell_proc_event.sql
3331_world_spell.sql
3358_world_spell_bonus_data.sql
3359_world_spell_linked_spell.sql
3363_characters_characters.sql
3373_world_spell.sql
3375_mangos_7839_01_world_trinity_string.sql
3375_mangos_7839_02_world_command.sql
3392_world_spell_proc_event.sql
3393_world_spell_proc_event.sql
3394_world_spell_bonus_data.sql
3409_world_spell_proc_event.sql
3410_world_SD2_scripts.sql
3414_mangos_7850_01_world_command.sql
3416_mangos_7855_01_world_pools.sql
3419_world_SD2_scripts.sql
3421_world_scripts.sql
3422_world_scripts.sql
3423_world_scripts.sql
3424_world_scripts.sql
3427_world_scripts_naxx.sql
3433_world.sql
3451_world_spell_proc_event.sql
3466_world_scripts_wintergrasp.sql
3467_world_spellclick_dk.sql
3498_world_scripts.sql
3500_mangos_7879_01_world_spell_proc_event.sql
3508_mangos_7886_01_world_petcreateinfo_spell.sql
3508_mangos_7887_01_characters_character_pet.sql
3535_TC1_1509_world_scripts.sql
3576_world_spell_script_target.sql
3577_world_spell_target_position.sql
3587_mangos_7893_01_world_command.sql
3592_world_spell_dk.sql
3593_world_spellclick_dk.sql
3596_world_scripts.sql
3601_world.sql
3603_mangos_7896_01_world_creature_template.sql
3609_mangos_7902_01_world_pool_creature.sql
3609_mangos_7902_02_world_pool_gameobject.sql
3612_world_spell_dk.sql
3618_mangos_7903_01_characters_character_pet.sql
3619_mangos_7904_01_world_creature_template.sql
3632_mangos_7908_world_creature_template.sql
3637_world_spell.sql
3643_world_wintergrasp.sql
3648_world_trinity_string_full.sql
3649_world.sql
3689_TC1_1534_world.sql
3691_TC1_919_world.sql
3715_mangos_7932_01_characters_character_pet.sql
3724_world.sql
3729_mangos_7938_01_realmd_account.sql
3736_world_spell_dk.sql
3747_mangos_7945_01_quest_template.sql
3791_characters_ahbot.sql
3801_world_spell.sql
3808_world.sql
3810_world_spell_bonus_data.sql
3813_world_spell_dk.sql
3851_characters_aura.sql
3851_world_spell.sql
3856_world_spell_naxx.sql
3869_world_access_requirement.sql
3870_mangos_7980_01_world_item_required_target.sql
3873_world_spell_dk.sql
3877_world_spell_script_target.sql
3886_world_script_nexus.sql
3899_world_spell_proc_event.sql
3903_world_spell_proc_event.sql
3910_world.sql
3920_characters_309-313_converter.sql
3920_world_309-313_converter.sql
3927_world_spell_proc_event.sql
3943_world_spell_proc_event.sql
3947_world_spell_proc_event.sql
3951_world_spell_proc_event.sql
3952_world_spell_pet_auras.sql
3954_world_spell_bonus_data.sql
3956_world_spell_proc_event.sql
3969_world_spell_proc_event.sql
3991_world_spell_proc_event.sql
3998_sd2.sql
3999_sd2.sql
4002_world_spell_proc_event.sql
4003_world_script_waypoint.sql
4006_sd2.sql
4016_world_spell_dk.sql
4023_world_spell_proc_event.sql
4030_world.sql
4031_world_spell_proc_event.sql
4032_world_spell_proc_event.sql
4043_world_npc_spellclick_spells.sql
4045_world_spell_proc_event.sql
4063_TC1_1569_world_scripts.sql
4066_world_spell_proc_event.sql
4081_world.sql
4105_8030_01_characters_character_spell.sql
4105_8030_02_characters_character_action.sql
4105_8030_03_mangos_npc_trainer.sql
4115_world_sd2.sql
4154_world_script.sql
4159_world.sql
4174_world_script.sql
4176_world.sql
4209_characters_TDB.sql
4209_world_TDB.sql
4211_world.sql
4216_world.sql
4217_world_spell_proc_event.sql
4222_world.sql
4225_world.sql
4233_world_spell_proc_event.sql
4234_world_spell_bonus_data_TDB.sql
4246_world_script.sql
4258_world_TDB.sql
4276_world_TDB.sql
4283_world_TDB.sql
4292_8072_01_characters_characters.sql
4292_8072_02_characters_characters.sql
4307_world_TDB.sql
4308_world_TDB.sql
4320_world_.sql
4321_world.sql
4346_8098_characters.sql
4346_8098_world.sql
4351_8104_01_characters.sql
4352_spell_bonus_data_full.sql
4356_world_spell_proc_event.sql
4360_world_spell_proc_event.sql
4363_world_trinity_string.sql
4367_world_spell_proc_event.sql
4371_world_spell_linked_spell.sql
4377_world_spell_proc_event.sql
4382_8115_world_playercreateinfo_action.sql
4392_world.sql
4393_world_spell_proc_event.sql
4394_world_spell_proc_event.sql
4397_world_playercreateinfo_spell_TDB.sql
4401_world_spell_proc_event.sql
4408_world_spell_proc_event.sql
4409_world_spell_proc_event.sql
4411_world_spell_bonus_data_TDB.sql
4422_world_script.sql
4423_world.sql
4426_world.sql
4428_world_spell_proc_event.sql
4429_world_spell_proc_event.sql
4431_world_trinity_string.sql
4432_world.sql
4445_8158_world_playercreateinfo_action.sql
4451_world_tmp.sql
4464_world_spell_bonus_data_TDB.sql
END CURRENT FILE LIST
 
 
BEGIN 'RESULT' LIST
4081_world.sql
4105_8030_03_mangos_npc_trainer.sql
4115_world_sd2.sql
4154_world_script.sql
4159_world.sql
4174_world_script.sql
4176_world.sql
4209_world_TDB.sql
4211_world.sql
4216_world.sql
4217_world_spell_proc_event.sql
4222_world.sql
4225_world.sql
4233_world_spell_proc_event.sql
4234_world_spell_bonus_data_TDB.sql
4246_world_script.sql
4258_world_TDB.sql
4276_world_TDB.sql
4283_world_TDB.sql
4307_world_TDB.sql
4308_world_TDB.sql
4320_world_.sql
4321_world.sql
4346_8098_world.sql
4352_spell_bonus_data_full.sql
4356_world_spell_proc_event.sql
4360_world_spell_proc_event.sql
4363_world_trinity_string.sql
4367_world_spell_proc_event.sql
4371_world_spell_linked_spell.sql
4377_world_spell_proc_event.sql
4382_8115_world_playercreateinfo_action.sql
4392_world.sql
4393_world_spell_proc_event.sql
4394_world_spell_proc_event.sql
4397_world_playercreateinfo_spell_TDB.sql
4401_world_spell_proc_event.sql
4408_world_spell_proc_event.sql
4409_world_spell_proc_event.sql
4411_world_spell_bonus_data_TDB.sql
4422_world_script.sql
4423_world.sql
4426_world.sql
4428_world_spell_proc_event.sql
4429_world_spell_proc_event.sql
4431_world_trinity_string.sql
4432_world.sql
4445_8158_world_playercreateinfo_action.sql
4451_world_tmp.sql
4464_world_spell_bonus_data_TDB.sql
END 'RESULT' LIST

Open in new window

LVL 1
sgaggerjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
this will output all .sql files with names "greater or equal" 4081, not containing "characters" or "realmd".

@echo off
(for %%F in (\path\*.sql) do ^
if "%%~nF" GEQ "4081" echo.%%~nF) ^
|findstr /v "realmd characters"

Open in new window

0
sgaggerjAuthor Commented:
I get
"4081" was unexpected at this time

copied exactly as you have, and also tried

@echo off
(for %%F in (\path\*.sql) do if "%%~nF" GEQ "4081" echo.%%~nF) | findstr /v "realmd characters"
0
sgaggerjAuthor Commented:
Think I got it, removed the () before 'for' and before | findstr

seems to do what i need!


@echo off
for %%F in (*.sql) do if "%%~nF" GEQ "4081" echo %%F | findstr /v "realmd characters"

Open in new window

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

AmazingTechCommented:
Try this.
(for /f "tokens=1,* delims=_" %%F in ('dir /on *.sql') do if %%F GEQ 4081 echo %%F_%%G) | findstr /i /v "realmd characters"

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
IF ... GEQ ...     inside parenthesis seems not to work. Only == is accepted. Seems to be a bug in cmd.exe, I cannot find any reason for that behaviour.

Leaving out the outmost parenthesis will perform worse. FINDSTR is called for each FOR loop step, while with parenthesis all echoes were collected, and findstr applied to the overall result one time only.

0
AmazingTechCommented:
That's really weird. My dir command needs a /s and would need the same removal of the outer brackets.
0
AmazingTechCommented:
Use a double pipe and it will work.
(for /f "tokens=1,* delims=_" %%F in ('dir /b /on *.sql') do if %%F GEQ 4081 echo %%F_%%G) || findstr /i /v "realmd characters"

Open in new window

0
sgaggerjAuthor Commented:
@AmazingTech

I just tried that one, and while it does run much faster, it doesn't filter out the files with 'characters' in the name.
0
AmazingTechCommented:
hmm.. Looks like the findstr doesn't work at all.
0
sgaggerjAuthor Commented:
it does this way, but is relatively "slow", though it is fast enough for my needs as there are only ~60 files or so and rarely exceeds 100.


for %%F in (*.sql) do if "%%~nF" GEQ "4081" echo %%F | findstr /v "realmd characters"

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
@AT
You used || instead of |, (conditional OR instead of piping).
0
AmazingTechCommented:
Hmm... Yes I was wrong I was just so happy it didn't say 4081 was unexpected. But I figured out another way to get the same effect. Maybe even faster.

I'm delimiting with _ so we just get the number in %%F. This is to avoid something like 756_ being greater than 4081_ I'm also not doing a string comparison so 756 will be less than 4081.
@echo off
for /f "tokens=1,* delims=_" %%F in ('dir /b /on *.sql ^| findstr /i /v "realmd characters"') do if %%F GEQ 4081 echo %%F_%%G

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, that should perform better and work always.
0
sgaggerjAuthor Commented:
Yep - that seems to work well. i'm adapting it to my needs right now.
0
sgaggerjAuthor Commented:
ok, one slight problem

i am using this code to generate three lists
list of 'world' updates
list of 'characters' updates
list of 'realmd' updates

for world i am doing:

call :genlist 4081 world_updates.txt realmd characters

:genlist
if exist %2 del %2
for /f "tokens=1,* delims=_" %%F in ('dir /b /on "%TC2_PATH%\sql\updates\*.sql" ^| findstr /i /v "%3 %4"') do if %%F GEQ %1 echo %%F_%%G >> .\%2
goto :eof

and it works perfectly, as these updates NEVER contain "realmd" or "characters", and *almost* always contain "world".

if i run
call :genlist 4081 characters_updates.txt realmd world
and
call :genlist 4081 realmd_updates.txt world characters

they both work, however they will also pick up the files that *don't* contain "world".

ie 4352_spell_bonus_data_full.sql

that means that all three update files contain the same file, which will cause problems.

is there a simple way to modify this :genlist so that it works in all three methods?


0
sgaggerjAuthor Commented:
ok, got it

This seems to work fine

:genlist
if exist %2 del %2
set w=world
set r=realmd
set c=characters
if %3==world for /f "tokens=1,* delims=_" %%F in ('dir /b /on "%TC2_PATH%\sql\updates\*.sql" ^| findstr /i /v "%r% %c%"') do if %%F GEQ %1 echo

%%F_%%G >> .\%2
if %3==characters for /f "tokens=1,* delims=_" %%F in ('dir /b /on "%TC2_PATH%\sql\updates\*.sql" ^| findstr /i /m "%c%"') do if %%F GEQ %1

echo %%F_%%G >> .\%2
if %3==realmd for /f "tokens=1,* delims=_" %%F in ('dir /b /on "%TC2_PATH%\sql\updates\*.sql" ^| findstr /i /m "%r%"') do if %%F GEQ %1 echo

%%F_%%G >> .\%2

goto :eof
0
sgaggerjAuthor Commented:
played a little more and got a condensed version

:genlist
if exist %2 del %2
if %3==world set vars=/v "realmd characters"
if %3==characters set vars=/m "characters"
if %3==realmd set vars=/m "realmd"

for /f "tokens=1,* delims=_" %%F in ('dir /b /on "%TC2_PATH%\sql\updates\*.sql" ^| findstr /i %vars%') do if %%F GEQ %1 echo %%F_%%G >> .\%2

goto :eof
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You don't need the /m switch for findstr.
The script is now  reasonable. The high-tech variant would be to use a syntax like

call :genlist 4081 world_updates.txt +world -realmd -characters

And, if you use your code, you could compress a bit more:

call :genlist 4081 world
call :genlist 4081 realmd
call :genlist 4081 characters
exit /b

:genlist
set file=%2_updates.txt
if exist %file% del %file%
if %2==world set vars=/v "realmd characters"
if %2==characters set vars=/m "characters"
if %2==realmd set vars=/m "realmd"

for /f "tokens=1,* delims=_" %%F in ('dir /b /on "%TC2_PATH%\sql\updates\*.sql" ^| findstr /i %vars%') do if %%F GEQ %1 echo %%F_%%G >> .\%file%
exit /b
0
sgaggerjAuthor Commented:
Thanks for the great answers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.