Get the filename of a running batch file - windows

I am trying to make a batch file that will write the name we names it.
For example if i names the file "test1" and start it it will change the title only to "test1" If i change the name to "bat file" it will display "bat file" in the title.
Is it possible to make a file that will write to a .txt file with the same name as the bat file? I am trying to make a batch file that will log sometings and write it too ".txt" where is the name of the bat file without extension

In batch files %0 stores a reference to the current batch file. The exact format of the value depends on how the file is invoked, but there are a serie of modifiers that can be used to retrieve the needed information (You can see the documentation or execute for /?, as the help for this command also includes the full list of modifiers)
#echo off
title %~n0
echo I am %~n0
> "%~dpn0.txt" echo This goes to the file
Where
%~n0 = name of the current batch file (without extension)
%~dpn0 = drive, path and file name (without extension) of the current batch file

echo %~f0 for writing your batch file name. Use ">" to write anything to a file. For eg use echo "write this" > file.txt

Related

Batch file variable in an environment variable

I have a program named go.exe, and I pass it a file name example so that it can make a log file named example_golog.txt.
The problem here is that I want to only call the executable without explicitly giving a filename or making a user have to give a file name.
My solution to this was to name a system variable called GO whose value is go.exe %~n0. What's wrong with this is instead of the %~n0 part getting the file name of the batch file that called it, my go.exe file just makes a text file called %~n0_golog.txt.
Is there any way around this so that %~n0 will do it's magic in the batch files that call it in go.exe %~n0?
EDIT:
My system variable:
Name: GO
Value: go %~n0
My test.bat file:
#echo on
%GO%
pause
When I run test.bat, the %~n0 does not expand out.
So instead of test_golog.txt being made, %~n0_golog.txt is made
In your batch file, you can call it to dereference the variable.
echo %go% will report go %~n0.
call echo %go% will report go test (from a batch file named test).
You can use whatever other sets you need from there.

Set batch title to file name

Is there a way for the title of a batch file to be the same as the file name of the batch file? I am constantly renaming files, so having the title of a batch file be the same as the file name would prevent me from constantly changing the title name.
I think you can do this by writing a line of code like this.
title %~n0
The %0 is the file path for the batch file and the ~n removes the path and keeps the name.
Hope it works.

Batch file that creates text file, called from another program

I am trying to run a batch file from a different program.
When I run the batch file manually (double clicking the file) it behaves as expected and creates a text file as a result. But when I call the batch file from a program (Winlog SCADA) the text file is not created.
Does anyone know why this happens?
The batch file contains this line of code:
systeminfo |findstr /C:"Time Zone" >UTCTime.txt
and the code that is calling the batch is (Winlog SCADA):
ShellExec(scriptPath,"run",projectPath+"/Settings/",8,".exe","");
The program probably writes to a different folder - try specifying the path to the folder where you want the file.
systeminfo |findstr /C:"Time Zone" >"c:\folder\UTCTime.txt"

help with windows batch scripting basics - execution and calling a separate executable within the script

Newbie to windows scripting. I need help running the .bat file on the command line so I can test it.
I used Text Document as my editor to create the file (opens up also as Notepad).
I performed file "save as" (ALL FILES). If I open up cmd, I can see the file has a .txt extension (myfile.bat.txt). So if I just type in cmd myfile.bat.txt the editor opens. I am not sure how to execute this correctly.
As for the logic in my batch script, I am basically logging into a remote directory (already created the net mount) and now I want to:
run an executeable file
rename some files.
With some research, I written this so far. I have saved it as a .bat file
# echo off
echo This is a batch file to run an executable and rename some files
pause
--run executable file here, just don't know how to do it
x:
cd x:
rename fileA fileB
Any help, good tips/practice would be great. Thanks.
Type in this command in cmd window:
rename myfile.bat.txt myfile.bat
Now you can run the script by simply invoking:
myfile.bat
or
myfile
(provided there's no myfile.exe or myfile.com in the same directory).
If you need to edit the script further, you can either right click it in Explorer and choose Edit or call the editor from the command window:
notepad myfile.bat
To call a program from the script, simply add its name, if it's in the current directory:
someprogram.exe
or the name with the path, if it's somewhere else:
directory\program.exe
or
d:\directory\program.exe
If the name or the path contain spaces, be sure to enclose the entire name & path string in double quotes:
"d:\directory\program name.exe"
you can just type the full name of the program
eg
"c:\program dir\program.exe"
or you can add the program directory to your path environment variable
set PATH=%PATH%;"c:\program dir"
and just type the program name
program
you can also edit your PATH variable in windows http://support.microsoft.com/kb/310519
NOTE: When you save the file in notepad, you want to save it as filename.BAT and select All Files from the second dropdown. If you don't it still gets saved as a .TXT.
A couple of command to consider:
CSCRIPT cscript /? in CMD
START http://ss64.com/nt/start.html
If you're doing say a VBSCRIPT use CSCRIPT to start it. If you're trying to execute another BATCH script or an EXE, use START

Create file command in batch files (*.bat)

I want to create a file named "new text document.txt" in the folder %tv% using a batch file ( *.bat). This is my batch file:
set tv=D:\prog\arpack96\ARPACK\SRC
cd "%tv%"
#CON >> "new text document.txt"
set tv=
Although I can really create the file in %tv%, but when I run the above batch file, I will get an error message saying
' ' is not recognized as an internal
or external command, operable program
or batch file.
Is there anyway to get rid of this error message? Or I am creating the file using the wrong command?
In order to get a truly empty file without having to provide user interaction, you can use the set /p command with a little trickery:
set tv=c:\documents and settings\administrator
cd "%tv%"
<nul >"new text document.txt" (set /p tv=)
The set /p asks the user for input into the tv variable after outputting the prompt after the = character.
Since the prompt is empty, no prompt is shown. Since you're reading from nul, it doesn't wait for user interaction. And you can store the empty string straight into tv thus removing the need to unset it.
Actually, after some more thought, there's an easier way. I've used that set /p trick in the past since it's the only way I know of echoing text without a newline being added (great for progress bars in the console). But if all you want is an empty file, you can probably get away with:
copy /y nul "new text document.txt"
The copy simply copies the contents of the nul device (effectively an empty file) to your new file name. The /y is to ensure it's overwritten without bothering the user.
type nul > file.txt
file.txt should be created empty
or paxdiablo's answer
Create an empty file from a *.bat file - this worked for me.
echo off > test.txt
con is the windows specialized file name and it should not be used.
copy con >> filename.txt
shall ask you to enter the text and you can save the text by typing Ctrl Z.

Resources