How do I make a self locating batch file [duplicate] - batch-file

This question already has answers here:
Get current batchfile directory
(4 answers)
Closed 3 years ago.
So basicly I want to make a batch file that can execute other files, while learing its own location on execution.
It should then use it own path as a reference to the other files.
How should I go about coding that or are there any guides for exactly that?
Thanks in advance!

The batch file name as entered via command line is stored in %0. Using file expander to get other parts. You can put the following code into a batch file and try it out:
REM print batch filename as user entered
echo %0
REM print full file name: d - drive, p - path, f - filename
echo %~dpf0
REM print path only
echo %~dp0
The current directory is stored in %CD%, in case you need it.

Related

START where both path and filename have spaces [duplicate]

This question already has answers here:
How to create batch file in Windows using "start" with a path and command with spaces
(7 answers)
Closed 2 years ago.
START /high /d "./files" "Program Name.exe"
This doesn't work, it works if "Program Name" wasn't in quotation marks, but in this scenario i don't have control over the file name and where the batch file gets executed. How would i solve this using Batch?
Though it's not explicitly stated in the help text from start /?:
START ["title"] [/D path] ...
the first quoted option is treated as the title for the window. In your case, that's the (supposed) executable name.
You can see that if you create an x y.cmd file containing only the command #dir, then run:
start /d "\program files" "\path\to\x y.cmd"
The directory \path\to above is the directory where you created it, needed because you'll actually be in the program files directory when you try to run it.
What you'll see is not a directory listing (because the script isn't running), but a new cmd window titled \path\to\x y.cmd.
If you instead use:
start "xyzzy" /d "\program files" "\path\to\x y.cmd"
you'll find that it does run the script, giving you a directory in a window titled xyzzy.
In other words, if you want to quote your program name, you should provide an actual title so there's no confusion.

How to echo something to a .txt file even if it's executable as a program BATCH [duplicate]

This question already has answers here:
using batch echo with special characters
(8 answers)
Closed 4 years ago.
I have a BATCH script that will create a text file that will later be executed by another program based on user input. I want to be able to echo Label>start to a text file. Unfortunately, CMD reads it as a command (because of the > character) and does not echo properly. If I use quotation marks they echo to the .txt file
"Level>start"
and so it cannot be executed. I really need some help with this.
echo Level^>start
seems to work.

Copy from variable to variable [duplicate]

This question already has answers here:
Assign command output to variable in batch file [duplicate]
(5 answers)
Closed 6 years ago.
For my batch file I need to do a few different things...I have completed steps 1-3
1) Perform a dir search and save the result of that search in a variable
2) Set the destination path as a variable
3) Copy the source file to the destination path
My code so far :
#echo off
cls
cd /d D:\Downloads\Videos
set "flshvid=&dir *flash*.mkv /s /b"
set "flshdir=O:\Aayush\Videos\TV Shows\The Flash\Season 3"
xcopy %flshvid% %flshdir%
Why doesn't this code work?
Any help is appreciated. Thanks in advance!
Environment variables may not start with a numeric because %n where n is 0..9 refers to the serial parameter number provided to the routine.
replace 1 and 2 with variable names that do not start with a numeric

Make a folder with today's date with a batch file [duplicate]

This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed 7 years ago.
I am fairly new with creating batch files.
I have made a batch file with the following content:
"C:\Program Files (x86)\Runtime Software\DriveImage XML\dixml.exe" /bC /c /l /t"B:\DRIVE IMAGES\Windows10_maintenance_backup
This allows me to run DriveImage XML and take a backup simply by running the bat file.
I would like the bat file to automatically make a folder with today's date so that:
B:\DRIVE IMAGES\Windows10_maintenance_backup
Becomes:
B:\DRIVE IMAGES\2016.01.20\Windows10_maintenance_backup
How can I achieve this by editing the .bat file?
I have had a look at foxidrives solution here but I do not know how to implement the solution.
echo md B:\DRIVE IMAGES\%date:~-4%.%date:~7,2%.%date:~4,2%\windows10\etc
See set /? for help on substring extraction.
This assumes date is in following format
Thu 21/01/2016

bat file Creating new Bat file [duplicate]

This question already has an answer here:
Batch file creating another batch file, how to ignore commands when writing lines?
(1 answer)
Closed 7 years ago.
So, on school we need to make this assignment.
So we tried to make a bat file that make's some folders and dox.
but the problem is: we want to let the bat file make another bat file.(with commands in it)
but if we write it like:
echo. #echo off F: tree/f>menu.bat
this creates a .bat file that doesn't work. probebly cause we didnt add lines. so how to do this?
how do we fix this?
looking forward to it.
You can use the >> operator to add lines to existing files. One > by itself will create a new file (or overwrite the contents of an existing file).
echo #echo off >menu.bat
echo F: >>menu.bat
echo tree /f >>menu.bat

Resources