bat file Creating new Bat file [duplicate] - batch-file

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

Related

Batch deleting exact file extensions [duplicate]

This question already has answers here:
Delete file with specific extension in batch file
(3 answers)
Closed 2 years ago.
I have .blk and .blkx files in the same folders, of wich im trying to delete only the .blk file. Ive attempted examples found on this post as in del "\folder\*.blk" /s. I managed to get the chosen files deleted however the .blkx files were deleted too (other files werent so i assume its an issue with both the file having .blk in their extension).
How can i select only the .blk file?
Edit: I dont know why this post was marked a duplicate but the above pinned post does not resolve the problem (wich i already talked about not working)
You must have SFNs (short filenames) enabled on that drive, and the .blkx extension is mapped to .blk in the short 8.3 filename. Because of this, and because wildcards match both long and short names, *.blk returns both .blk and blkx files. To distinguish between them you'll need to run a for loop, check the actual extension, and delete each .blk individually.
for %%b in (*.blk) do #(if /I "%%~xb" == ".blk" echo del "%%~b")
Remove echo from the above to actually delete the .blk files.
To recurse into subdirectories (like del /s) use for /r instead of for.
The command is written as to be used in a batch file (per the question tag). To run it at a command prompt, instead, replace the double percents %%b with single ones %b.

How to paste in the command line (not using shortcuts/mouse)? [duplicate]

This question already has answers here:
How can you get the clipboard contents with a Windows command?
(14 answers)
Access clipboard in Windows batch file
(11 answers)
Closed 2 years ago.
I am working on a batch script that runs .exe files in a given folder. It should do something like this:
SET /P _inputname= Please enter app folder path:
cd %_inputname%
dir /s /b *.exe | clip
"paste-from-clipboard"
However, for my last line, I haven't found a way to paste from clipboard without using a mouse/keyboard shortcut. I would greatly appreciate it if anyone has a solution.
To copy & paste within windows command line you can use:
https://github.com/kpym/windows-paste
It's the successor of the once famous paste.exe.
See also How can you get the clipboard contents with a Windows command?

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

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.

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.

Appending environmental variables from one batch file to another

This is a line of code from a batch file I'm working on.
echo %systemroot% >> untitled.bat
It's job is to append the text %systemroot% to another batch file called untitled.bat.
When I open the untitled.bat file with notepad to view the code it reads C:\Windows.
This makes sense because that is my %systemroot% but my problem is that I want the code to be written to the untitled.bat file as %systemroot% not C:\Windows.
Does anyone know how (if at all) this is possible?
This line of code works for me:
echo %%systemroot%% >> untitled.bat

Resources