How to use a .txt file as an command - file

I am developing on a lot of batch programs that need to have a option, to do the work easy to the user. So he can type in the host, domain, IP address name in a text file, instead of editing 10 different batch files and 1/20 lines in one batch file.
I have an idea that might work if it gets a little push:
Command.txt
ping http:www.stackoverflow.com
ping1.bat
#echo off
Cd ..
Echo testing connection.
Type command.txt {please describe the solution here}

for /f "delims=" %%a in (file.txt) do "%%~a"

It sounds like you just want a configuration file that you can reference from your batch files. I do this by setting environment variables that are defined in an .ini file.
Config.ini
host=myhostname
domain=mydomain
ip=10.10.10.1
Then in your batch files, use the command:
for /f "delims== tokens=1,*" %%A in (config.ini) do #set %%A=%%B
From that point forward, you will have 3 variables that you can use: %host%, %domain%, and %ip%.

Related

Trouble converting multiple PDF files to JPEG files using Ghostscript from a batch file

I have several (thousand) multipage PDF files that I am trying to convert to JPEG files (one file for each page of each PDF) with Ghostscript. I have the following batch file, but I can't get it to work properly.
for %%f in (*.pdf) do (
gswin64.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg %%f
)
The loop seems to work okay in the sense that it is opening the files. It is going wrong at the "p%03d.jpg" -- I'm guessing that the loop sees the "%0" and for some reason inserts the name of the batch file. I get the following error:
**** Could not open the file pC:\Users\bmjones\Downloads\convert.bat3d.jpg
("convert.bat" is the name of the batch file)
I'm guessing that even if I got this to work, I would have another problem. When I manually run:
gswin64.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg [file name].pdf
It works fine. I get a set of files names 001.jpg, 002.jpg, etc. But I would like them to be numbered differently. So, if my first two files have 3 and 2 pages respectively, I would like to have out something like this:
f1_pg1.jpg, f1_pg2.jpg, f1_pg3.jpg, f2_pg1.jpg, f2_pg2.jpg
Instead of using a counter for unique filenames, you could set the name of the output files based on the names of the PDFs.
#echo off
setlocal
for %%I in (*.pdf) do (
gswin64 -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile="%%~nI_p%%02d.jpg" "%%~I"
)
%%~nI gets the base name without extension of each PDF. The "%%~I" just makes sure filenames containing spaces don't break things. Enter help for in a console window and see the last two pages for more information about this syntax.
Alright, with a little help from #rojo, I think I've got this (I'm certainly open to better solutions)
set /a c=0
setlocal ENABLEDELAYEDEXPANSION
for %%f in (*.pdf) do (
set /a c=c+1
set FNAME=f_!c!_p%%02d.jpg
gswin64.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -r144 -sOutputFile=!FNAME! %%f
)

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

Reading input from a file doesn't work for filenames containing spaces

I have a case where I have to move over 15,000 files. There are many duplicates, so I parsed them by MD5 and now I have a list of file names in an input file (files.txt).
I want to read from it, then copy the listed files to a new directory.
I pulled an old batch that someone had written as a two-part simple script and modified it.
It works for files without spaces. How can I get this to cover all file names?
Also, can't I put all of this into one file?
Cstart.bat:
for /f %%x in (files.txt) do call copyfiles.bat
copyfiles.bat:
set filename=%1
copy "C:\temp\%filename%" "C:\temp\pruned files\"
Your current code doesn't even pass the filename to copyfiles.bat, so it's not working with or without spaces. (If you need to confirm that, add echo %1 %filename & pause before the copy line in copyfiles.bat and run cstart.bat.)
With that being said, you can do it all in one file easily:
for /f %%x "tokens=1 delims=*" in (files.txt) do copy "C:\Temp\%%x" "C:\Temp\pruned files\%%x"
To make sure it works, just replace the copy in the line above with echo and run it from a command prompt.
I tested this with a text file named test.txt that contained the following:
One.txt
Two.txt
Three.txt
And Four.txt
with a batch file named testcopy.bat containing this:
#echo off
for /f "tokens=1 delims=*" %%x in (test.txt) do echo "C:\Temp\%%x" "C:\Temp\test this\%%x"
The above test showed this output:
e:\TempFiles>testcopy
"C:\Temp\One.txt" "C:\Temp\test this\One.txt"
"C:\Temp\Two.txt" "C:\Temp\test this\Two.txt"
"C:\Temp\Three.txt" "C:\Temp\test this\Three.txt"
"C:\Temp\And Four.txt" "C:\Temp\test this\And Four.txt"
for /f "usebackqdelims=" %%x in ("my file list.txt") do copy "C:\temp\%%~x" "C:\temp\pruned files"

Running an .exe file through a batch file and passing parameters

I have an .exe file that takes two parameters when i run it from the command line, as such:
test_app.exe -vid.avi -data.txt
How would i be able to START the .exe file through a batch script and pass it those parameters?
If i have multiple .avi and .txt files that i need to pass to the .exe file through START, how would i be able to have a variable that goes through all of those files two at a time? (pairing every .avi with it's correspondant .txt).
Let's assume that every pair of .avi and .txt share the same name but obviously have different extensions.
I need to write something like this:
#ECHO OFF
START test_app.exe -vid.avi -data.txt
pause
But the parameters should be variables that increment every time a pair of parameters are proccessed through the .exe so it would loop on all of the files in the CWD.
Trying to do this but seems like START does not work that way?
#echo off
for %%a in (*.avi) do (
START Tester.exe -%%a -%%~na.txt
)
pause
try this, it works with AVI as the main extension, you may change this:
#echo off &setlocal enabledelayedexpansion
for %%i in (*.avi) do (
set "line="
for %%j in ("%%~ni.*") do set line=!line! -"%%~j"
start "" test_app.exe !line!
)
Try this with your avi files. It will just echo the bunch of commands and you can see what it does. The - signs seem a bit odd but I included them with the names.
#echo off
for %%a in (*.avi) do (
echo exe.file "-%%a" "-%%~na.txt"
)
pause

Batch File To Read And Modify Text File

Okay, so basically, I have a whole list of links in a plain text Notepad file, each link on a seperate line. All I am wanting to do is to add a bit of text before each link, specifically: 127.0.0.1 and a couple of spaces.
So this...
somelink.com
becomes this...
127.0.0.1 somelink.com
By now you've probably already guessed that I am trying to edit the contents of a text file to make it useable as a HOSTS file in Windows.
So I am wanting some batch file code, executable in a .bat file, which basically reads a Notepad text file, and then add "127.0.0.1 " at the beginning of each line with text on it. I am guessing this is probably a very simple piece of code for someone with some knowledge of MS DOS and batch file code, but that most certainly isn't me, and the only batch files I have ever written have been with help like now.
Thanks for any and all help in advance with this, it really is much appreciated.
read HELP FOR and then try this in a command prompt
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1 %a >>output.txt
here is some explanation and some considerations to extend it with a little more complete functionality and put it in a BAT file
FOR is the command to iterate over the lines of your input text file. Read microsoft documentation at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
input.txt is the text file that contains your list of domain names, it must reside in the current directory
output.txt will be the result file that will contain the list of domain names prefixed with 127.0.0.1, it will be created in the current directory
If you want to create a BAT file, you need to move the FOR command and edit it a little bit, changing the %a loop variable names to be %%a.
You can then place the BAT file either in the current directory, where your input resided and where the output will be created.
Alternativelly, you may place your BAT file, elsewhere. In that case, you need to invoke it with its full path.
Or you may even place it in a special directory (I have my own C:\Program Files\CMD) and add it in the PATH system variable. See here www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true how you can change your PATH for your current session. And here ss64.com/nt/path.html you may find some explanation on how to make the PATH change permanent.
Also, you might be tempted to add some flexibility to your BAT file, instead of having constant input.txt and output.txt filename, replace them with %1 and %2 which represent the first and second command line parameters of your BAT file.
the user might then want to use files which contain blanks in their filenames. They might specify them surrounding the names with quotes ". In that case, you need to add some incantation usebackq in the FOR command for it to not break havoc when the user uses quotes.
Finally, you will need to decide what to do in case the output text file already exists, you migh want to consider prevent overwriting.
So putting all this pieces together, here is a short BAT file to get you started...
#echo off
if .%2==. goto help
if not exist %1 goto helpno1
if exist %2 goto helpalready2
FOR /F "usebackq delims=" %%a in (%1) do #echo 127.0.0.1 %%a >>%2
goto :eof
:help
echo you need to specify input and output text files
goto :eof
:helpno1
echo %1 not found
goto :eof
:helpalready2
echo %2 already exist
goto :eof
welcome to BAT programming and enjoy!
here we go!
(
Set /p line1=
Set /p line2=
Set /p line3=
Set /p line4=
)<Filename.txt
echo 127.0.0.1 %line1%>Filename.txt
echo 127.0.0.1 %line2%>>Filename.txt
echo 127.0.0.1 %line3%>>Filename.txt
echo 127.0.0.1 %line4%>>Filename.txt
This will Read the first four lines of the text file, and then put in your stuff and each line back into the line it came from.
Have Fun!
In addition to PA.'s answer, if you require a specific number of spaces, you can throw them into a variable and add it to the command as well.
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1%spaces%%a>>output.txt
So the output will be
127.0.0.1 somelink.com
Batch-File flavor:
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %%a in (input.txt) do #echo 127.0.0.1%spaces%%%a>>output.txt

Resources