Solved

Batch File scripting

Posted on 2009-07-15
19
323 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:sgaggerj
  • 9
  • 5
  • 5
19 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 24859336
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24859654
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24859788
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24859893
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 24860067
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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24860350
That's really weird. My dir command needs a /s and would need the same removal of the outer brackets.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24860427
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24860590
@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
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24860677
hmm.. Looks like the findstr doesn't work at all.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:sgaggerj
ID: 24860844
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 24861371
@AT
You used || instead of |, (conditional OR instead of piping).
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 250 total points
ID: 24867026
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 24867275
Yes, that should perform better and work always.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24868802
Yep - that seems to work well. i'm adapting it to my needs right now.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24869629
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24869862
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
 
LVL 1

Author Comment

by:sgaggerj
ID: 24869973
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
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 24870081
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
 
LVL 1

Author Closing Comment

by:sgaggerj
ID: 31603721
Thanks for the great answers!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now