How do I get a Batch file to store data? I've tried using Text files and Dat files but no luck. Could anyone help me out?
I tried this for storing a name:
echo %name% >name.txt
but I'm finding it hard making it extract the name data and printing it in a batch file.
Another way is to save the data like this :
#echo off
set "name=Jaden"
>name.txt echo name=%name%
And to get the value again. Just evaluate the line(s) in name.txt
like this :
#echo off
for /f "delims=" %%a in (name.txt) do set %%a
echo name --^> %name%
First set the value and then if you echo to a file redirect, it will send the content value to a file.
set name=Jaden
echo %name% > name.txt
then it will work.
Just use set /p name=<name.txt
Related
I am new to Win10 batch files and I am running into a problem. I want to parse a directory for files that meet a certain criteria and then return a substring of the file.
Example:
If the file name is 210526_FPRS.PAS.SKBTXNS.TXT -> I want to echo 210526_FPRS.PAS.SKBTXNS
If the file name is 210526_FPRS.PAS.XXXXXXX.TXT-> I want to take no action
Here is what I wrote:
#ECHO OFF
ECHO "Looping through files..."
CD \blah
FOR %%G in ("*SKBTXNS*") DO ECHO %%G:~1,22%
The output I am getting is: 210526_FPRS.PAS.SKBTXNS.txt:~1,22
Thanks in advance!
#ECHO OFF
CLS
ECHO Looping through files...
CD \blah
FOR %%G IN (\*SKB\*) DO (call :uFile: %%G)
GOTO :eof
:uFile
SET _filename=%1
SET _result=%_filename:~0,22%
ECHO %_result%
:eof
The issue that I found in my original code was that I was trying to update my variables inside the FOR loop. What I needed to do was set them outside the loop via a subroutine. From there I was able to adjust the substring value as needed for the situation.
Thanks to all for your help!
I am trying to get a batch script to read the text from a document called "Response.txt". I can't seem to get it to read the text inside the file. The file says Yes. I want it to create a new text file to say week A if "Reponse.txt" = Yes.
#echo off
IF Response.txt = Yes
goto next
:next
ECHO >Week.txt
ECHO Week >Week A.txt
#echo off
for /f %%a in (Response.txt) do if /i "%%a"=="Yes" ECHO Week >"Week A.txt"
Will create "Week A.txt" if any line in response.txt has a first word "Yes" in any case.
Beyond that, you'll have to explain further what you want to do.
2 Questions:
1) Changing scripts based on input
Basically, say I have a file, like search.html that changes based on what you type in.
Aside from doing
set/p string=What would you like to search for?
echo ^<!DOCTYPE html^> >>file.html
echo ^<html^> >>file.html
echo ^<title^>^</title> >>file.html
echo ^<script language="JavaScript""^> >>file.html
echo string = '%site%'
...
Is there another way to do this?
2) Getting things returned from a file?
I have no example for this. I was simply wondering if you could start a file, use wait, and once it has closed get what was in it?
try this:
#echo OFF &setlocal
(
echo ^<!DOCTYPE html^>
echo ^<html^>
echo ^<title^>^</title^>
echo ^<script language="JavaScript""^>
echo string = '%site%'
)>file.html
2) Getting things returned from a file
#echo OFF &setlocal
FOR /f "delims=" %%a IN (file.html) DO (
ECHO(%%a
)
There are many ways to replace text in a file with something else. You can get input and then replace a MARKER (text like that) with the input text. VBS, Powershell, SED, AWK, and batch can do it.
Your second question is a bit short on detail - but FINDSTR etc can read lines from a file.
I have a txt file naemd as (settings.txt )which has content
SET BACKUP_DRIVE=E:\
SET BACKUP_DIRECTORY=BACKUP\
SET HOURLY_DIRECTORY=HOURLY\
SET INPUT_DIRECTORY=D:\MySQL\Data\CDR\.
I have a bat file where I want to use these variables for prepare backup path
SET BACKUP_PATH=%BACKUP_DRIVE%%BACKUP_DIRECTORY%%HOURLY_DIRECTORY%%CURRENT_HOUR%\
But I am not getting prepared path.
I have tried
type settings.txt in the bat file
It is printing content of setting file but not implementing it..showing echo is off..
if I do echo on then some prob also.
Please tell me how to use these variables
Thanks
Try this:
#echo off&setlocal
:: set CURRENT_HOUR for testing
set "CURRENT_HOUR=03"
for /f "delims=" %%i in (settings.txt) do %%i
SET "BACKUP_PATH=%BACKUP_DRIVE%%BACKUP_DIRECTORY%%HOURLY_DIRECTORY%%CURRENT_HOUR%\"
echo %BACKUP_PATH%
Output is:
E:\BACKUP\HOURLY\03\
I am trying to find and replace values of a string within a batch file but having issues. I have the user run the batch file and it asks the user 1)what drive the file is on 2)what is the name of folder in the TEST parent folder 3)what is the name of the new server. I want the batch file to look within a file called importer.config and replace a value called server_name with whatever the input from the user is. Here is what I have:
#echo off
SET drive=
SET /P drive=Please enter the drive:
SET folder=
SET /P folder=Enter name of folder desired:
SET server=
SET /P server=Enter name of new server:
#echo off > newfile.txt
setLocal EnableDelayedExpansion
if exist newfile.txt del newfile.txt
for /f "tokens=* delims= " %%a in (%drive%\test\%folder%\importer.config) do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config
pause
Every time I run this, the cmd prompt shows:
The system cannot find the file specified c:\test\users_input_they_entered\importer.config. The issue is that file is there so trying to understand what I am missing and why it cant find the file that does exist.
It then also states "Could not find c:\windows\system32\importer.config" which not sure why that happens as well
I have searched on stackoverflow, but cannot figure this out with any assistance.
You're pushing your luck using the for loop for that.
A tool like sed would work well.
If you look at this post they have a vbscript implementation that you could use
Is there any sed like utility for cmd.exe
set input_file=importer.config
set output_file=temp.config
set new_server_name=server1984
cscript /Nologo sed.vbs s/server_name/%new_server_name%/ < %input_file% > %output_file%