batch rename files with different file extensions to 1 extension - batch-file

I'm recently new to batch scripting. I need to rename a few thousand files, each one having a different file extension to a single common extension. The files come off a machine like so:
1.2.840.113619.2.131.3171610912.1353091118.893703
which windows tells me is a type "893703" file because of the fullstop(.) position.
So I need to turn this example:
1.2.840.113619.2.131.3171610912.1353091118.893703
1.2.840.113619.2.131.3171610912.1353091118.907596
1.2.840.113619.2.131.3171610912.1353091118.920723
1.2.840.113619.2.131.3171610912.1353091118.932988
1.2.840.113619.2.131.3171610912.1353091118.945443
into this
1.2.840.113619.2.131.3171610912.1353091118.893703.IMA
1.2.840.113619.2.131.3171610912.1353091118.907596.IMA
1.2.840.113619.2.131.3171610912.1353091118.920723.IMA
1.2.840.113619.2.131.3171610912.1353091118.932988.IMA
1.2.840.113619.2.131.3171610912.1353091118.945443.IMA
I can edit the text string before the extension, and change a series of the same extension, but I'm not sure how to deal with files of different extensions.
I'm using Windows 7.

Run the following on the command line:
ren *.* *.*.IMA

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"

Bulk extension changer

I would like to make .bat file which will be changing all files on all discs with extension .xxx to extension .yyy. It's just an example because I will want to add more file extensions like: change all .aaa, .bbb, .ccc, etc. files to extension .ggg.
You can download this script from Microsoft's Script Center. It can change file extensions in bulk:
This batch script can be used to change file extensions in bulk. File extensions are renamed for all matching files in the current folder and in all subdirectories.It supports the following.1. Change extension for all the files in the current folder and all sub folders.
You can also use cmd or file renaming utilities.
You can do it using rename command, the general form is:
RENAME [drive:][path]filename1 filename2
Or
REN [drive:][path]filename1 filename2
For example;
REN *.xxx *.yyy
And also there are some file renaming utilities that you can use for the same:
Bulk Rename Utility,
Bulk Extension Changer
For more detail please refer to this link.

Using ghostscript in a Windows .bat file to convert multiple pdf files to png

I have many many pdf files in a directory that I need to convert from pdf to png. Currently, I am using the ImageMagick command:
magick mogrify -format png *.pdf
Because, there are so many files, I would like to use ghostscript directly because there are several sources that suggest that I could achieve a 75% reduction in processing time by doing this.
However, I am having trouble finding a clean dos command example to accomplish the same thing as the ImageMagick command above. I believe I need to execute the gswin64c.exe module but I am unsure how to do this to accomplish what I need to get done. Can someone provide me with a clean example of the ghostscript that accomplishes what I'm doing in ImageMagick?
After much digging, what I discovered was that ghostscript does not really have a wildcard that would allow reference to all files of a certain pattern (like ImageMagick does). To convert all files in a directory that are pdf's to png's, a dos script like the following could be used:
for %%x in (*) do gswin64c.exe -sDEVICE=png16m -dBATCH -dNOPAUSE -dQUIET -
SOutputFile="%%~nx.png" %%~nx.pdf
This can also be run from the command line by simply using single percentage signs (%) instead of the double percentage signs in the script above.
The terms are as follows:
gswin64c.exe: This is the dos command version of GhostScript. It should be used as opposed to gswin64.exe which will open a GhostScript window.
-sDEVICE=png16m This indicates the form of the output file. Is this case png.
-dBATCH -dNOPAUSE. These are GhostScript options and when employed will allow for continuous operation of the script (without them, the program will pause after each file converted).
-dQUIET - This suppresses notifications that display on stdout after each processed file.
SOutputFile="%%~nx.png" %%~nx.pdf This indicates the pattern for the input files and the output files. x is the loop variable. The % sign is used as a wild card. ~nx is a Dos convention which truncates the extension of an echoed file name.

Simple way to delete one line from file using command prompt

I am writing a simple batch script, and I need to delete one line from the file that gets downloaded by the script. What is the easiest way to do this using ONLY the command prompt? I have come across several various suggestions, but nothing to very simply delete one string that is constant across all files that are downloaded with the script.
This is a very platform dependant question. If you are using a *nix environment (Linux / Mac environment using bash / shell), you can accomplish this with sed
sed '/${regular_expression_that_matches_line_to_be_removed}/d' yourFile.txt > newFile.txt
This will generate a new file called newFile.txt that will contain the output. You can also do this in place (it modifies the file it's using as input), but I recommend against that because if you mess up your regex, you've lost your input.
If you're using a Windows environment (which I assume you are due to your batch-file tag), try looking at this Delete certain lines in a txt file via a batch file

Deciphering "Make EXE from BAT" Script written by Jason Faulker

I came across a way to convert my .bat with dependencies on tool to an .exe file. However when I try using the script and run the .exe created, I always getting an error. Seems I modified the script incorrectly.
Anyone can help, please?
Here's the code with my modifications:
#ECHO OFF
ECHO Make EXE From BAT
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.
REM Usage:
MakeExeFromBat BatFileToConvert -bat MyProgram.bat
REM
REM Required Parameters:
BatFileToConvert -save MyProgram
REM Source batch file to use to produce the output Exe file.
REM
REM Optional Parameters:
IncludeFile -include Tool.exe
REM Additional files to include in the Exe file.
REM You can include external tools used by the batch file so they are available on the executing machine.
SETLOCAL
REM Configuration (no quotes needed):
SET PathTo7Zip=C:\Desktop\
REM ---- Do not modify anything below this line ----
SET OutputFile="%~n1.exe"
SET SourceFiles="%TEMP%\MakeEXE_files.txt"
SET Config="%TEMP%\MakeEXE_config.txt"
SET Source7ZFile="%Temp%\MakeEXE.7z"
REM Remove existing files
IF EXIST %OutputFile% DEL %OutputFile%
REM Build source archive
ECHO "%~dpnx1" > %SourceFiles%
:AddInclude
IF {%2}=={} GOTO EndInclude
ECHO "%~dpnx2" >> %SourceFiles%
SHIFT /2
GOTO AddInclude
:EndInclude
"%PathTo7Zip%\7za.exe" a %Source7ZFile% #%SourceFiles%
REM Build config file
ECHO ;!#Install#!UTF-8! > %Config%
ECHO RunProgram="%~nx1" >> %Config%
ECHO ;!#InstallEnd#! >> %Config%
REM Build EXE
COPY /B "%PathTo7Zip%\7zsd.sfx" + %Config% + %Source7ZFile% %OutputFile%
REM Clean up
IF EXIST %SourceFiles% DEL %SourceFiles%
IF EXIST %Config% DEL %Config%
IF EXIST %Source7ZFile% DEL %Source7ZFile%
ENDLOCAL
This doesn't really convert a bat file to an exe. It just creates a selfextracting archive (exe) which contains the bat file. On execution it extracts the file to a temporary folder and runs it from there. You can even extract the bat from the exe just by using 7zip/rar/winzip or any other archiver.
If you want to convert a bat to an exe for real you should use one of the tools from the web (like this one: http://www.f2ko.de/index.php?lang=en) or concider using a simple script language like AutoIt.
If you pick the second, you can simply execute your bat code with Run("put your bat code in here") and you can compile your script to a "real" exe file.
For an alternative approach, you can basically do the same thing as described in the accepted answer (making a 7z-SFX) with WinRAR. That way, you can also do it with a GUI, and I will try to add some more useful information.
Actually, you can also use the latter approach to generate portable applications and it also works with "converting" every runnable (or openable) file into an .exe.
If you need that "portability hack", you should unpack your .exe or .msi installer with Universal Extractor. Details can be found in this Article, Step 1 to 4. Newer Versions of 7zip or WinRAR also come with comparable features.
Now you add all needed files to the archive. In the easiest case, this is just your .bat script or whatever file you want to "convert" into an .exe applivation. (Step 5 here)
Steps 6 and 7 are just some Settings for the SFX-Archive, 8 is the interesting one, as you select what you actually want to run there. Input the name of your (.bat-)file.
Step 9 lets you select where to unpack to - you do this setting manually and programmatically in the MakeExeFromBat.bat-script.
After this process you created a Portable App in SFX archiever form, enjoy
The word "converting" was put into quotation marks, because running that .exe actually works like this:
The contents of the (SFX-)EXE file are extracted from the "archive part" to a directory as the specified temp directory.
( The config file generated by the script is read. )
The file, that was previously contained in the EXE file and then extracted, is now executed in a new window.
a) This file could besides a .bat be anything - as e.g. an image, a MP3 or a video
b) or also a Python Script (of course your OS needs to know how to deal with that file.
Once finished, the temp files are removed.
You can also derive some limitations from that. If you have a .bat that needs the content of the working directory, you will have a problem. (Say, a batch that renames all files in the current dir from 1 to n.) In some cases that can be dealt with by adding all needed files to the archive too. On Windows Vista and all newer OSes, you might encounter a message box after the script is run. After selecting ‘This program installed correctly’, the message box will not be displayed in the future for this file. Because the EXE file launches in a new window, the typical way of logging output (using the > char) will not work as expected. In order to log the output, you would need to handle this natively in your source script.
All references were already linked, but once again: Big credit goes to Jason Faulkner for providing the Article and 7zip-Approach, binbert for the WinRAR-SFX Solution (which is as hinted much more versatile -> portability) and some credit to creative8 for finding the two and the article comparing them.
Actually, I was develping another solution using AutoHotkey. In my case, I just want to be able to add my .bat to the windows start menu - but the options are not limited to that.
The script itself is just a oneliner and .AHK is easily converted to .exe (I used v1.1.33.09):
run % SubStr(A_ScriptName, 1, -4) ;// run also has the option to run your file minimized or hidden, see the source 2 below
Source 2
What it does is taking its own name, removing the .ahk or .exe respectively (the last 4 characters, hence -4) and running excactly that. Usage could not be easier: you have a runme.bat, so you rename the program I provide to runme.bat.exe. Say you want the .exe to open an image.png - guess what, rename it to image.png.exe. You get the gist - that's it. It dynamically checks its name to find what to run. In my opinion, this is not much less mighty than "unpacking the .bat and then run it", but (again imho) it is much more elegant.
Use it as you wish, I should probably start a public github page or so.

Resources