I am fairly new in using/making .bat files.
I'm trying to make an incredible simple one that combines .txt files into a .tmp file, and then renames the .tmp file in to a .txt file.
This is the code:
#echo off
title Combine Text Files
for %f in (*.txt) do type %f >> Combined.tmp & echo. >> Combined.tmp
rename Combined.tmp Combined.txt
pause
The problem I come up with is that when I try to run the .bat file, nothing happens. CMD closes itself immediately.
If I try to run the script on the third line on CMD, it works fine and it creates the .tmp file.
Similarly, if I run the .bat file sans the script on the third line, I am able to see the "Press any key to continue.." just fine.
Am I doing wrong with the for loop when in .bat files?
You need to double the % when using for command from batch file because this is how the for parser works:
#echo off
title Combine Text Files
for %%f in (*.txt) do (
type "%%~ff">>"Combined.tmp"
(echo()>>"Combined.tmp"
)
rename "Combined.tmp" "Combined.txt"
pause
The errors in the FOR syntax are one of the very few occasions that will terminate a bat file execuction.
As type command supports wild cards you can just try with:
type "*.txt">"Combined.tmp"
Related
I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"
I'm new in using batch scripts, and moderately experienced with octave. I have a lot of data files I examine with octave functions and I am trying to set up so that I can double click on files with a custom extension to directly open octave functions. Think "when I double click on this text file, it opens in notepad."
To do this I've written a very basic .bat file and I've associated my .data files to open using this .bat file. The .bat file looks like this:
C:\Octave\Octave-4.2.1\octave.vbs --no-gui --persist --eval myOctaveFunction.m
pause
I've tested it with a hard coded filename inside "myOctaveFunction." but instead i'd like to actually pass the data file name to myOctaveFunction when I double click on the data file. How do I do this? And, are batch scripts even the right way to do this?
Thanks for your help.
Try this batch file, which will echo a few specific items:
#echo off
echo My directory %cd%
echo Batchfile Name %0
echo File to run %1
pause
So %1 parameter will provide you with the filename itself.
You can also use it like this.
#echo off
echo My directory %cd%
echo Batchfile Name %~dpnx0
echo file to run %~dpnx1
pause
So in Short, this should work if you are running this the way I am thinking you do.
C:\Octave\Octave-4.2.1\octave.vbs --no-gui --persist --eval myOctaveFunction('%1')
pause
I'm looking to get a batch file to apply to all sub directories.
I have a number of folders. Each folder will contain file pairs
xxx1.mp3 and xxx1.cdg,
xxx2.mp3 and xxx2.cdg,
etc.
I have a 7zip batch file that will look for file pairs and create a single zip file xxx1.zip, xxx2.zip and delete the (now) redundant cdg/mp3 files.
However, this will only work if the bat file is run in each individual folder. What I'm really looking for is a switch to add to the bat file that if I run in the root directory, will run through all sub directories also.
The code I currently run is:
FOR %F IN (*.cdg) DO "C:\Program Files\7-Zip\7Z.exe" a "%~nF.zip" "%~nF.cdg" "%~nF.mp3" -sdel
Any help?
FOR /R %%F IN (*.cdg) DO IF EXIST "%%~dpnF.mp3" pushd "%%~dpF"&ECHO("C:\Program Files\7-Zip\7Z.exe" a "%%~nF.zip" "%%~nF.cdg" "%%~nF.mp3" -sdel&POPD
Note that this is a batch line - to execute from the prompt, reduce each %% in metavariable to % (but it's a whole lot easier to put it into a batch by cut-and-paste;saves all those typos...)
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.
Using for /r will iterate through the filenames matching the supplied mask (it's possible to also add a starting directory - see for /?|more from the prompt.)
Since we know the selected full filename exists, first check that the paired file exists, then switch to the required directory, archive, and switch back.
I have my main batch file with all of the coding in it. I want to keep my coding nicely organised so what I want to do is to make my main batch file read code off multiple .txt files and display the code on its window.
Lets say I do something like this:
if %TEST%==1 for /f "Delims=" %%a in (test.txt) do (set TEXT=%%a)
echo %TEXT%
This echos the whole text that is inside the .txt but how do I make it run the code? Also how do I make my main batch file echo specific lines of the .txt file?
Why dont you save the other scripts a .bat and call them with
start script.bat. I dont think there is a way you could read from a text file and execute the writen text as script.
Or you try setting something up lile `set command = your string here
call %%command%%`
I'm working on a start script, and I have a text file output.txt, containing paths to programs, like:
C:/program1.exe
C:/abab/program2.exe
How do I now run the programs contained in the text file via a batch script?
for /f "usebackq delims=" %A in ("C:\output.txt") Do Start "" "%A"
Start starts programs without waiting for them to exit (so new window) and first set of quotes is the window title. UseBackq is needed to use quotes around output.txt. See For /? and my answer here for how to start programs Trouble with renaming folders and sub folders using Batch. In a batch use %%A and %A when typing interactively (I don't use batch files, I keep stuff in one text file and paste parts into a command prompt window,which is interactive).
If they were to be run sequentially then
Rename the file to batch and run it. Open in notepad and search for / and replace with \ as that is the windows standard (although autocorrect will fix it without telling you but can sometimes cause problems).