Set batch title to file name - windows

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.

Related

Get the filename of a running batch file

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

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"

Using a shortcut or batch file to move file to a relative location

I'm trying to create a shortcutor batch file to sit in the windows'send to' menu which sends the file to a location relative to the current file path of the file.
So for example I'm looking to create an archive shortcut to stick in the ocntext menu that would move files as follows
C:\cheese\stilton.txt > C:\cheese\archive\stilton.txt
or
C:\biscuits\hobnobs.txt > C:\biscuits\archive\hobnobs.txt
But I don't understand how to capture the current file path and pass it to the batch\shortcut.
Can anyone help?
Funnily enough, windows relative path does not differentiate between file & directory, when using ...
Simply, move "%1" "%1"\..\archive also works. ;-) Though not a clean way. :-)
Also make sure that you do mkdir "%1"\..\archive before move.
Also, instead of send-to, you may think of adding it to registry; specific to a file type.
For text files, .reg file will look as below:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\txtfile\shell\movefile]
#="Move to archive"
[HKEY_CLASSES_ROOT\txtfile\shell\movefile\command]
#="cmd /c mkdir \"%1\"\\..\\archive & move \"%1\" \"%1\"\\..\\archive "
In a batch file you can get the path of the first parameter with %~dp1, or just the path with %~p1. This means that you would want to pass the full filename with path to the batch file.
To use this in a batch file, for example to move a file, you could do the following:
#echo off
setlocal enabledelayedexpansion
set fn=%~1
move "!!fn!!" "%~p1archive"
The %~1 removes quotes from the filename, and I just do that out of habit.
The %~p1 expands to the path of the file, with a slash appended.
Note that I haven't included here checking that the directory exists, which should be simple enough... add this line before the move to create it:
md "%~p1archive"

Running a batch file in a given directory using VBA

When I go to my batch file's location and open it, the batch file works. My batch file is simply:
cd .\data
dir/b/o:n > names.txt
As you can see, I'm in my current directory and moving down to the sub directory "data" and coping all the names and creating a file called names.txt.
When I say
shell "location of file"
it opens the batch file, but the directory that is defaulted to is C:\my documents, so my commands won't work because it cannot find the sub directory. I want this to be a dynamic batch file, and therefore i need to write something in VBA that will open the batch file under its current directory or something to this effect.
How do I do this?
The following should give you the effect you seek.
My test code is:
Option Explicit
Sub TryShell()
Dim PathCrnt As String
PathCrnt = ActiveWorkbook.Path
Call Shell(PathCrnt & "\TryShell.bat " & PathCrnt)
End Sub
My test batch file is named TryShell.bat and contains:
cd %1
dir *.* >TryShell.txt
I have placed my batch file in the same folder as the workbook containing my macro.
The statement PathCrnt = ActiveWorkbook.Path sets PathCrnt to the name of the directory containing the active workbook. You can set PathCrnt to whatever directory you require.
When I call Shell, I have added PathCrnt as a parameter.
In my batch file, I set the current directory to %1 which is the first parameter.
The dir command works as I wish because the current directory is my directory and not the system default directory.
Hope this is clear.
C:\My Documents is probably the directory where your speadsheet is located. If you add
ChDir "C:\TheFolderWhereYourBatchIs"
before launching your Shell command and that should work...
Alternatively, you could change your batch file to use an absolute directory instead of a relative one.

How can a text file be edited from a Windows batch file?

I am trying to make a .bat file to edit the redirect in index.asp to be the filename of the file selected via the context menu option .bat file.
I know how to replace the file:
#echo OFF
del/q D:\e-lib\index.asp
copy %1 D:\e-lib\index.asp
but not how to edit the contents so that the redirect within index.asp points to the filename of the file I right-click, i.e the value %1.
I'm not sure about editing a file via DOS BATCH file, but you could easily pump in the contents of the file using the print command (e.g. "something" > file.asp). You could also build a template that reads in a text file via ASP, that has the contents of the data you need to include in your dynamic ASP file.

Resources