How to write batch file which use the name of one file as part of the name of another file - batch-file

I have some java executable program initialized from cmd. My problem is as following: I would like to read all files from some directory. Next, I would like to run the program as many times as many files I have in my folder. The required inputs are the path to the file with data and the name of the file where the results will be written. Now my question is, how can I write a simple batch file which will do it for me?
For example:
I have a list of files in my folder
file_1.xls
file_2.xls
file_3.xls
I want to run a loop and for each file initialize line specified below:
java -jar -Xmx1000M Program.jar pathToInputFile PathToOutputfile
For example for file file_1.xls I want to write the result to the file with the same name but different extension and at the begining of this file add some constant prefix. In case of file_1.xls the results I would like to write as Output_file_1.txt
for file_2.xls -> Output_file_2.txt
for file_3.xls -> Output_file_3.txt
and so on...
Can anyone help me?

pushd "c:\excel_files"
for %%F in (*.xls) do (
java -jar -Xmx1000M Program.jar "%%~nxF" "Output_%%~nF.txt"
)
Though I'll recommend you to use -classpath and direct call of the entry point class instead of direct call of the .jar .

Related

Huffman compressed files with my own extension

I am working on a project that uses Huffman algorithm to compress files, and I am doing my project using Java, what I want is to create my own file extension say (.huff) for the compressed file, and when I right click a file if it has the (.huff) extension, I want to add a new option which decompresses it, I searched the web but I did not find anything useful.
Any help would be appreciated.
To set the file extension just use one of the String methods like append(".yourExtension") (append it to the filename) and set as filename. Simple as that.
String filename = filename.append(extension);
To decompress the compressed file, I suggest you write a metod to which you provide a path to file as argument, check if the file extension is correct and then in another method you decompress this file.
There is nothing special about a file extension, it's just a part of the file name. To create a .huff file extension, just add .huff to the end of the file name.
To add the windows context menu, that's explained in the question linked in the comments How can I add a context menu to the Windows Explorer for a Java application?
I would recommend creating a batch script that will launch your program taking in the file to decompress as an argument.
Something similar to:
#echo off
java -cp <path-to-jar> <decompression main class> %1
Adding in any other setup or program arguments you need. Then a registry entry might look like.
HKEY_CLASSES_ROOT\.huff\shell\Decompress huffman encoded file\command
"<path to batch file>" "%1"

How can my batch file find its own location and call a matlab script inside that folder?

I have an internal software that generates folders with batch files. The batch file is supposed to run a matlab file in the same folder, but in fact it just runs Matlab and the previous Matlab script (not the one in its folder).
I need a command in my batch file to recognize its own location(folder) and run the matlab file from the same folder.
Thank you in advance
use the %0 parameter. This on is an implicit parameter that you do not pass to the scrip
try this and see if it helps you get going:
#echo %~dp0
the ~dp sequence strips the name and extension from the full path to the script.
note that this works only from within a script, not from the command prompt
References: for-command

Select multiple files with the same extension in .bat file

i found a script that converts a certain subtitle form , but to make it work i need to rename the subtitle file to "test.srt", then after coonverting , i rename it back to its original name , and its really annoying to keep repeating the process and to name each subtitle file on its own , i want the script to convert all the files in that folder that ends up with ".srt", not only to "test.srt" file
the bat file is like this :
"perl ps3_friendly_arabic_subs_converter.pl test.srt"
it uses the perl script to convert the file named "test.srt" , i tried to make it convert all the files that ends up with ".srt" , and i edited it this way
"perl ps3_friendly_arabic_subs_converter.pl *.srt"
and yeah it didn't work , so please anyone knows how ? thanks in adnvance
for %a in (*.srt) do perl ps3_friendly_arabic_subs_converter.pl "%a"
For each .srt file (in current folder), execute the indicated script passing the file as argument.
To execute it from a batch file, it is necessary to escape the percent signs, replacing each % with %%

Executing .bat file in FOR loop

Edit: Brief Summary
I have number files in a directory, called crash0, crash1 etc. I want to run a .bat file for each of this with a command line like:
abc.bat crash0 > crash0.txt
How can I make another .bat file that loops over all the crashXX files calls abc.bat once for each one of them?
Original Question
Please find my situation below..
I have some files (number may vary each time) in a folder with its name starting with crash. That is crash0, crash1..etc. I want to provide these files as an input to a .bat file (let it be abc.bat) and then navigate the out put a corresponding text file. The command looks like abc.bat crash0 > crash0.txt. I have to do this to all the crash files in the folder. This abc.bat files actually converts the non-readable files to a readable format. So at the end I should have txt files like crash0.txt, crash1.txt.. etc for the corresponding crash files which i provided as the input. Can any one help with a .bat script to run this in cmd?? am new to .bat scripting.. thx in advance
for %%i in (crash*) do #call abc.bat %%i > %%i.txt

How do I make batch files to handle certian commands

I need help making a batch file in ms dos to do certain commands like:
Request from you to first press any key
List the contents of the C:\WINDOWS directory
Create a subdirectory on one of your local drives. Use your initials to name the subdirectory.
Copy all the text files from C:\WINDOWS directory into the new subdirectory.
Print one of the text files copied to your new subdirectory.
for 5. I think I need to make a call to a command to print
You need to create a .bat file that has the following text:
a)Request from you to first press any key
pause
b) List the contents of the C:\WINDOWS directory
dir c:\windows
c) Create a subdirectory on one of your local drives. Use your initials to name the subdirectory.
md c:\ro
d) Copy all the text files from C:\WINDOWS directory into the new subdirectory.
copy c:\windows\*.txt c:\ro
Regarding printing files I'm not sure, if I remember correctly the simplest way is something like:
type file.txt > LPT1
To create a .bat file, you should type in:
copy con filename.bat
... print the relevant lines above
When finished press Ctrl-Z and Enter.
So, all in all, you should type in:
copy con filename.bat
pause
dir c:\windows
md c:\ro
copy c:\windows\*.txt c:\ro
type file.txt > LPT1
And then Ctrl-Z and Enter.
Update: If you don't want the commands to be printed to the user, you can add another line before pause that will contain #echo off.
Check out Rob van der Woude's Scripting Pages, specifically the Batch Files section. He has an excellent reference with lots of tips and tricks that will help with most of these tasks.
Command line help is your friend here for most of this. You will need to have input parameters specified for a number of the things you;re asking for, though.
As a quick search, try this article.

Resources