# Batch help



## abhishek (Jul 23, 2004)

Hi All,
My command line is c:\sed\sed abcd.jcl
I want to written a batch file which runs above command for all .jcl files
in a specific directory.
I want source code for that ,Also number of .jcl files are not fixed


----------



## codejockey (Feb 11, 2002)

Welcome to TSG! 

Assuming that you wish to write a batch file that runs under MS-DOS or similar (e.g., DOS Window), you might have a look at the for command. The general syntax is roughly: for %%variable in (expression) do (command). For example, a batch script with the line: for %%i in (*.JCL) do copy %%i C:\TMP
would copy all files in the current directory that ended in .JCL to the C:\TMP directory.

Hope this helps.


----------



## abhishek (Jul 23, 2004)

codejockey said:


> Welcome to TSG!
> 
> Assuming that you wish to write a batch file that runs under MS-DOS or similar (e.g., DOS Window), you might have a look at the for command. The general syntax is roughly: for %%variable in (expression) do (command). For example, a batch script with the line: for %%i in (*.JCL) do copy %%i C:\TMP
> would copy all files in the current directory that ended in .JCL to the C:\TMP directory.
> ...


But I want to pass that *.jcl file to my utillity sed which takes commandline parameter one *.jcl file.
example:

c:\sed a.jcl
c:\sed b.jcl
c:\sed c.jcl
c:\sed d.jcl
Upto n
For that I want to write a batch script which runs this utillity in batch


----------



## codejockey (Feb 11, 2002)

I think with a little modification, my example will do what you want. Have a look at the portion of the for statement where the copy command is invoked ... 

Hope this helps.


----------



## Shadow2531 (Apr 30, 2001)

@codejockey

Just out of curiousity, I was trying this.

for %%i in (*.lib) do set noext=%i:.lib=.a% && rename %%i %noext%.a && move %noext%.a temp

for each file with extension .lib, change extension to .a and move file to temp folder

According to ever source that I looked at so far, that should work, but it doesn't.

the %i:.lib=.a% part is supposed to replace any occurence of .lib in the variable with .a.

found lots of resources on it by searching google for batch *files; substring*

Got any ideas? This may help the OP also.


----------



## guard (Jun 18, 2004)

Shadow2531 said:


> @codejockey
> 
> Just out of curiousity, I was trying this.
> 
> ...


Try the script below which uses the FREE Advanced NT/2K/XP/K3 Command Library from TheSystemGuard.com. The library a single *.cmd* file which contains only batch scripting commands.

For the benefit of others who may be reading, I added cross-platform compatibility with NT4, 2000, XP and Server 2003.

All commands are color coded to help in understanding.

Windows internal commands are *Dark Red* and are linked to a "Mounted Help" page. This is a merged help page showing the differences in syntax and usage across NT, 2K, XP and K3.

"Mount/\Commands" are *Navy* and are linked to their respective home page. These are optimized code segments stored in environment variables under a "sounds like what it does" name.

*******

@*ECHO* OFF

*SETLOCAL* ENABLEEXTENSIONS

*CALL* ntlib.cmd /Init /Quiet

*CD* /D _{YourSourceDirectory}_

*FOR* *%%i* IN (*.lib) DO (
*%.IfNT%* (
*MOVE* "*%%i*" "c:\temp"

*REN* "c:\temp\*%%~nxi*" "*%%~ni*.a"​) ELSE (
*MOVE* "*%%i*" "c:\temp\*%%~ni*.a"​)​)

*******

Notes:

The Command Library provides over 200 resources to write self-documenting and reliable shell scripts (batch files). Each resource is constructed using only what comes with Windows and everything is contained within a single *.cmd* file. There are no third-party executable files.

The *FOR* loop (*%%i*) cycles through each *.lib* file and moves it to the c:\temp folder. Once there, the file extension is renamed to *.a*. The *%%~ni* expands to filename only while *%%~nxi* is the filename.ext only.

The *.ifNT* Mt/\Cmd is used because the internal *MOVE* command supports simultaneous renaming only in Windows 2000 and above. So NT4 will need to do it in two steps (*MOVE* and *REN*).

Anything that is (or could be) a filename should always be enclosed in "" just in case there are embedded spaces or other characters that are OK in filenames but unreliable in shell scripts.

Changing to your source directory using "*CD* /D" is always a good idea just in case the script is not started from there.

The script assumes that "ntlib.cmd" is your path but if it's not you can always call the library from somewhere else using *drive:\path\ntlib.cmd* or *\\server\share\ntlib.cmd* (ntlib requires only Read access, so this can be a Read-Only network share or even a CD/DVD).

*******

Another similar example is in this post.

*******

To the original poster:

*FOR* *%%i* IN (*.jcl) DO @(c:\sed\sed "*%%i*")​


----------



## abhishek (Jul 23, 2004)

Thank you Codejacky,
It's working properly now.


----------



## Shadow2531 (Apr 30, 2001)

@guard

Thanks for the command lib link and example.

I decided that is was just easier to write a c++ program to change the extension of all files with a certain extension.


----------

