473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CMD shell programming Looping with FOR

140 100+
Hi,

I'm attempting to add a FOR Loop to a pre-existing cmd batch that I have, and am very confused by the stuff I've found on the web by way of documentation regarding using for loops.

Here is my old code:
Expand|Select|Wrap|Line Numbers
  1. @echo on
  2. echo Now processing: %1
  3. @echo off
  4.  
  5. if !%1==! goto ERROR
  6.  
  7. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/     1 /p" > %2____1_nodin
  8. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 10001 /p" > %210001_nodin
  9. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 20001 /p" > %220001_nodin
  10. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 30001 /p" > %230001_nodin 
  11. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 40001 /p" > %240001_nodin
  12. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 50001 /p" > %250001_nodin
  13. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 60001 /p" > %260001_nodin
  14. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 70001 /p" > %270001_nodin
  15. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 80001 /p" > %280001_nodin
  16.  
  17. %1\util\sed -n -e "/time/p" %2nodout | %1\util\sed -n -e "$!N; /^\(.*\)\n\1$/!P; D" > %2time_nodin
  18.  
  19. REM %1 = Current Working Directory
  20. REM %2 = ".\shake_01\"
  21. REM Format would be do_a_shake2.cmd  . .\shake_01\
  22.  
  23. goto EXIT0
  24. :ERROR
  25. @echo on
  26. echo Usage:   do_a_node .\shake_01\
  27. echo Creates: X+1 bottle datafiles for each shake and a time datafile
  28. echo          
  29. :EXIT0
  30.  
Here is the FOR Loop I intend to add:
Expand|Select|Wrap|Line Numbers
  1.  
  2. FOR /L %%G IN (1,1,%3) DO %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ 10001 /p" > %210001_nodin
  3.  
I've taken one of the calls to the sed and inserted it where the example had "[Commands]", is this the correct way to format it? If there is a new argument "%3 (which is the number of times I want to do this loop, instead of the standard 8 times in the old code (above), would it work to substitute that argument, in the sed statement in the two places where there are mention to the # (at the beginning of the stuff in the strings #0001, where it currently is set to 1,2,3...7,8 (I bolded the numbers in the above codes, which would be substituted with the count variable).

Or would it be better to save the sed call into a new cmd file and call that from the FOR loop passing in the appropriate # for the call? (FYI, I have the windows sed utility, if your wondering how I'm calling sed from a windows cmd file...)

Examples and explanations would be much appreciated.

Thanks,
Sitko.
Oct 11 '07 #1
2 7061
sitko
140 100+
<please delete me....thanks>
Oct 15 '07 #2
sitko
140 100+
Just to tell you how I figured this out. I created 3 scripts which were:

Main Code.
Expand|Select|Wrap|Line Numbers
  1. ' VBScript source code
  2. Dim WshShell
  3. Dim CurWrkDir
  4. Dim fso, f, f1, s, sf
  5. Dim MyPos
  6. Dim FolderName
  7. Dim CmdName
  8. Dim oExec
  9. Dim vArg, aArgs(), iCount
  10. Dim NumOfBottles
  11.  
  12. If WScript.Arguments.Count > 1 then
  13.     WScript.Echo "Too many arguments"
  14.     WScript.Quit
  15. End If
  16. If WScript.Arguments.Count = 0 Then
  17.     NumOfBottles = 6
  18. Else
  19.     ReDim aArgs(wscript.Arguments.Count -1)
  20.     For iCount = 0 to WScript.Arguments.Count -1
  21.         aArgs(iCount) = WScript.Arguments(iCount)
  22.         NumOfBottles = aArgs(iCount)
  23.     Next
  24. End If
  25.  
  26. Set WshShell = WScript.CreateObject("WScript.Shell")
  27. CurWrkDir = WshShell.CurrentDirectory
  28.  
  29. Set fso = CreateObject("Scripting.FileSystemObject")
  30. Set f = fso.GetFolder(CurWrkDir)
  31. Set sf = f.SubFolders
  32. For Each f1 in sf
  33.     FolderName = f1.name
  34.     MyPos = InStr(1, FolderName, "shake", 1)
  35.     if MyPos <> 0 then
  36.         CmdName = "do_a_Shake.cmd " & CurWrkDir & " " & f1.path & "\ "
  37.                                    & NumOfBottles
  38.         Set oExec = WshShell.Exec(CmdName)
  39.         Do While oExec.Status = 0
  40.             WScript.Sleep 100
  41.         Loop        
  42.     end if
  43. Next
  44.  
Do_A_Shake.cmd, has basically just this FOR loop along with a couple other unrelevant codes...The FOR statement, I ended up using was:
Expand|Select|Wrap|Line Numbers
  1. FOR /L %%G IN (1,1,%3) DO DoSed %1 %2 %%G
  2.  
and as I couldn't figure out haw to call the command with the FOR statement, I moved it to a new cmd file and called (DoSed.cmd) from the previous code.
Expand|Select|Wrap|Line Numbers
  1. @echo off
  2. %1\util\sed "/z-rot acc/,/n o d a l/d;/Center/d;/Bottle/d;/n o d a l/d" %2nodout | %1\util\sed -n "/ %30001 /p" > %2%30001_nodin
  3.  
Any other improvements you can offer, would be appreciate, or maybe an explaination of what the vArg variable in the first code snippet is for (I got the argument code off the web, but don't see a call to it anywhere...
In the end, it works...so I guess thats the most important thing...

Thanks,
Sitko.
Oct 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ken Miller | last post by:
Hi all. I'm fairly new to programming, and have started using visual basic.net, just found this place, so hope someone can help as I'm a little stuck :) I'm trying to write a small front end to...
9
by: none | last post by:
Hello all, I wrote a shell program a few years ago in VB6 that needs to be modified. The problem I have is this: The SysAdmin uses this shell in place of Explorer, so there is no taskbar. When...
10
by: Daven Nair | last post by:
Hi, I would like to know if Python supports codes similar to shell scripts: count=`ps -ef|grep "pattern"|wc -l` for count in `echo $count` do done fi
2
by: Francesco | last post by:
Hello Pythonnian's (sorry for crossposting) I have written on the base of Miro Rajic a (still) small filemanager in wxPython. Now I want to add (for Windows XP) a explorer shell context menu....
2
by: Juggler | last post by:
Hi, I have a shell script which takes three arguments, how can I call this script from within a C++ program. I am new to C++ programming and not sure how to accomplish this. I tried using...
5
by: Javaman59 | last post by:
Hi all, I'm a Unix guy from way back, who's enjoying learning C# and Windows programming. Overall, it's not such a difficult transition (as long as one keeps an open mind :) ), but there is one...
2
by: Sin | last post by:
Hello everyone, I'm totally stumped at how little info I can find in MSDN or on the web concerning this. It's almost as if only microsoft personel hold the key to these secrets or something!!! ...
5
by: gjuro kladaric | last post by:
it was not possible to make a shell extension from within VB2003, I believe has anything changed since then, can I (easily) write a VB code that would function as a shell extension thank you ...
3
by: Mark | last post by:
Hello, What I need to know is if there is a better method to run/edit modules on my pc. I'm currently running the IDLE shell under Python 2.5, on Windows XP. Every time I edit my .txt or .py...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.