473,797 Members | 3,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CMD shell programming Looping with FOR

140 New Member
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 7079
sitko
140 New Member
<please delete me....thanks>
Oct 15 '07 #2
sitko
140 New Member
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
3539
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 an application, now this was going fine up until this morning, where I had to send the parameters to the actual app. I have this: Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
9
6966
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 his users run PC Anywhere from the shell, and minimize it, it minimizes to the system tray. With no task bar, there is no system tray, so there is no way to restore the PC Anywhere window. The shell starts PC Anywhere using ShellExecute. I...
10
2306
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
3135
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. I have no idea, how to accomplish this. For what I can imagine:
2
3107
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 system() call but could make it work, must be doing something wrong. Appreciate any help in this regard. Thanks, Juggler
5
2268
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 thing from Unix I miss the most - the command line. I've install a Unix like programming environment, call MSys, which let's me do command line programming the way I want to, with the bash shell, and this is running smoothly (vim editor, bash,...
2
4175
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!!! I'm currently writting a program in which there is a file/folder list which mimics part of Explorer's list. I've got it pretty much cornered, but I still have a couple of problems :
5
4613
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 gjuro
3
1943
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 file, I have to restart the IDLE shell for the changes to take effect. It's pretty annoying. Assuming IDLE is already open, here are the steps that I typically take:
21
3040
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 take it as -winpath "c:\temp\" and csh will take it literally (with the double-slashes). Is there a way for me to know what shell is currently running my
0
9537
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10246
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.