Help with Batch Files? - file

What are batch files useful for? They just seem to be used to make viruses and other things...but it seems like shell scripting to me.
Whats the uses for batch files?

From Batch file article at Wikipedia:
Batch files are useful for running a
sequence of executables automatically
and are often used by system
administrators to automate tedious
processes. Unix-like operating
systems (such as Linux) have a similar
type of file called a shell script.
A simple example:
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d *svn') do ( rd /s /q "%%i")
If you save the above line in a file called ClearSVNFolders.bat and after that execute a double click you'll delete every folder named svn that resides inside a root path...
You automated the whole process. You could easily spend hours doing the above task if you had a deep root directory, that is, one containing thousands of folders. :)

Batch files are the Windows equivalent of a Unix shell script. So you can use them to automate things.

You could use them for shell scripting. :-P
Of course, they kind of suck at that, compared to bash (or perl/python/tcl). But if you're on Windows, it's a one-horse race unless you want to install cygwin or msys and battle with Unix/Windows incompatibilities.

Batch Files are extremely useful. They are super easy to learn as well. you can make them do things on startup like say that a program wants to open itself and wont close even from taskman.exe you can force it to shutoff without warning.
or you could make games and ineractive things like i like to do.
i have a Messenger that i made with fully customizable colors and accounts with account management and servers.
But you probably dont trust me enough for you to download it.
But yea they are pretty useful.

Batch file is "a computer file containing a list of instructions to be carried out in turn."
We have been studying since childhood that computer is a dummy machine and this is a method of instructing a dummy machine.
For example :-
If you want to instruct the system to create a folder with random name then type ,
#echo off
md %random%
Creating Batch files enables you to execute several line of CMD commands in a single file.
For example :-
#echo off
md %random%
tasklist
Pause

The entire purpose of a Batch script is to execute several DOS commands in sequence:
echo Hello!
set var=7
echo I just made var=%var%!
pause
It was invented in MS-DOS for user simplicity to execute things they did all the time, the most notable thing being "AUTOEXEC.BAT" which started once the command interpreter started, people would add things like:
echo Welcome to my computer!
or
cd C:\Games\
To make it quicker to access their games or whatever they needed.

Related

Batch command double extension fix

Hi everybody i would like to ask how i can remove a mistakenly added second file extension using a batch script.
E.g. "test.aac.m4a" -> "test.m4a"
So the last extension is the right one which i want to have.
But this is ONLY the case for
.aac.m4a
-> .m4a
and
.m4a.aac
-> .aac
I know some batch scripting but
ren *.aac.m4a *.m4a
Won't work :(
Another thing worth mentioning would be that these double extensions come from my music software MusicBee.
I use mp4box on m4a files to extract the raw aac stream from the m4a container so i can edit it in other software.
Currently the syntax is:
mp4box.exe -raw 1 "<URL>" -out "<URL>".aac
The "<URL>" is the variable MusicBee will replace with the file url.
But this will add the .aac extension after the .m4a and i have no idea how to replace it instead. (and again when i repack the files ".aac -> .aac.m4a")
As far as i know MusicBee just replaces the variables and launches the batch code when activated so i think other batch code will work too.
Is it possible to prevent this double extension from even developing?
As always ANY help is apreciated!!
Thanks, Daniel
In preventing the double extension from developing I'm guessing you're not appending the file extension accordingly in the right way, I am however not familiar at all with Music Bee.
As for creating the right batch files that do what you want, I've used Advanced File Renamer in the past for all sorts of renaming patterns such as your case. It's freeware too! The program has a fairly advanced feature that allows users to write custom scripts in JavaScript user guide here. And can even generate batch scripts that do special renaming (as you've noted in the comments) for your specific use case.
For the other less advanced users the program has a GUI that makes it easy to do batch renames.
Best of all, if you're like me that avoids third party software installs just to do one thing as much as possible, the program has a portable mode that won't hook itself into your system, which is always good.
Read the manuals and guides there for more information. My answer might sound a little too much like advertising for it, but that's only because it's helped me so much in renaming my music a long time ago.
Here's a screenshot by the OP, RapidFireArts that shows how OP used the software to remove the second file extension.
It ought to be possible, but I have no idea how to work with your software to prevent the double extensions from occurring in the first place. But it is fairly easy to strip off the unwanted middle "extension".
If you know for a fact that none of your .m4a or .aac files are supposed to have multiple dots, then you could simply do the following:
ren *.m4a ???????????????????????????????????????????.m4a
ren *.aac ???????????????????????????????????????????.aac
Just make sure you have enough ? wildcards to match the longest name in your folder. See How does the Windows RENAME command interpret wildcards? for an explanation of why this solution works.
But sometimes file names legitimately have additional dots prior to the actual extension. If this is your case, then the following batch script will remove only the unwanted .m4a and .aac middle "extensions"
#echo off
for /f "eol=: delims=" %%A in ('dir /b /a-d *.m4a.aac *.aac.m4a') do (
for %%B in ("%%A") do ren "%%A" "%%~nB%%~xA"
)
Another option is to use my JREN.BAT regular expression file renaming utility. JREN.BAT is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. Ideally, the script should be placed within a folder that is included within your PATH. I like to use c:\utils for all of my non-standard utilities.
Once you have JREN.BAT, then all you would need would be
call jren "\.(m4a|aac)(?=\.(m4a|aac)$)" ""
Provided you understand regular expressions, and you take the time to read the built-in JREN help, then there are many wondrous things you can do with the utility. The help is accessed by issuing jren /? from the command line. You might want to use jren /?|more if you have not configured your console window to have a large buffer that enables scrolling to see prior output.
I use File Renamer Basic.
http://download.cnet.com/File-Renamer-Basic/3000-2248_4-10306538.html
It's Free

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.

Implement a .vbs in a batch without it being stored locally

I would make this short & sweet, but I'd like to explain what/why I need help with. I have been working on a batch file that collects various information files from a system that are helpful in debugging situations. As a kernel debugger, having to ask for crash dumps, sysinfo logs, all the nine yards, over and over again, is very tiresome. With this said, I am working on a pre-existing batch file that collects such files.
I have updated it as requested by the original creator in various ways, however, their one request is one I am not sure how to go about. On Windows 8/8.1, you have the option of logging into a Microsoft account. If you use this method of logging into Windows 8, upon the completion of this batch, it will display your email address in the logs. Given this is a popular collection application among the BSOD community, the creator has received various 'complaints', all of which valid, that this is a security issue. With that said, a .vbs script has been created that essentially removes the line in the log that denotes 'Registered Owner'.
The creator wants to implement this .vbs script into the batch file in such a way that the script is created FROM the batch in %temp%, run successfully as needed, and then afterwards deleted. I do not know how to implement it this way, which is my problem. I of course successfully made a simple example \Data folder within the batch directory in which I stored the script, and then called it from the batch. However, as far as creating the script from within the batch itself within %temp% goes without having it existing locally beforehand, I have no idea.
I would very much appreciate some guidance/what to read to figure this out.
Regards,
Patrick
Easier is to use batch to do it.
findstr /i /v /c:"Registered Owner" c:\windows\minidump\somelogfile.txt >c:\windows\somefilewithoutregisteredowner.txt
For help
findstr/?
To extract a vbs from bat
FOR /F "usebackq skip=6 delims=" %%i IN (%0) DO #echo %%i >>"%temp%\tmp010.vbs"
cscript "%temp%\tmp010.vbs"
del "%temp%\tmp010.vbs"
pause
goto :eof
Set ie = CreateObject("InternetExplorer.Application")
ie.AddressBar = 0
ie.Visible = 1
ie.ToolBar = 0
ie.StatusBar = 0
ie.Left = 400
ie.Top = 100
ie.Width = 800
ie.Height = 900
ie.Navigate2 "http://answers.microsoft.com/en-us/windows/forum/windows_vista?tab=question&status=all"
Remember help is available on every command.

I am trying to start an office program on a flash drive with a batch file

I am putting Kingsoft office on my flash drive, and I want to use a batch file to start the applications because the paths are not easily accessible, I cannon create a .lnk file because the path varies by computer because it may be plugged into a different port. Here is my batch file code, could somebody give some suggestions on how to make this work. Thanks in advance...
set "path=%~dp0"
start %path%office6\wpp.exe
The second line is the problem, the program won't start the program. Thanks!
cd /d "%~dp0"
start "" /b wpp.exe
I think some of the directory names in %path% contain spaces and since %path% is not enclosed within ""( double quotes), the script is unable to find the exe .
You may also want to include a log file so that it becomes easier to debug in case of any errors.
Try this:
set baseFolder=%~dp0
start "%baseFolder%office6\wpp.exe" > "%baseFolder%batchRunLog.log"

OCR batch processing tiff to text

I have a problem where i need to batch convert 50,000 tiff's into 50,000 txt files respectively. I am aware of abbyy finereader and some other pieces of software that may be able to do this, but a free solution would be best. I have been researching tesseract as well. Is anyone aware of any script or program that uses tesseract to do this automatically with a good quality output??
Thanks in advance
For a free solution with Tesseract, here's a straightforward command line batch file. Change the variable contents and/or create folders as necessary:
:Start
#Echo off
Set _SourcePath=C:\tifs\*.tif
Set _OutputPath=C:\txts\
Set _Tesseract="C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
In my opinion, I think Tesseract is going to give you the best results, whether you're looking at free solutions or not.
If you figure out how to convert one file, and then you post back the command you use, it will be easy to hack a batch script together to process multiple files.
Take a look at VietOCR, a Java/.NET frontend for Tesseract; its function seems to suit your need.

Resources