How to write to different lines of a text file with batch - batch-file

I wrote the following code:
#echo off
title Kiel Configurations
rem Credits Will Go Here
rem Website Here
color 08
echo **********************************************************************
echo **************************Kiel Configuration**************************
echo **********************************************************************
echo ======================================================================
echo Use yes/no to configure the following options
pause
cls
echo Would you like it to run in fake mode? (recommended for slow computers or for the paranoid)
set /p FakeModeVar=(yes/no):
cls
echo Would you like to close skype?
set /p SkypeVar=(yes/no):
cls
echo Woud you like to close spotify?
set /p SpotifyVar=(yes/no):
cls
echo Would you like to close steam?
set /p SteamVar=(yes/no)
cls
echo Would you like to close chrome? (reccommended)
set /p ChromeVar=(yes/no):
cls
echo Which chrome profile would you like chrome to open as? (Ask me if unsure)
set /p ChromeProfile=(1,2,3...):
cls
echo Now configurating you settings...
pause >nul
if %FakeModeVar%==yes goto Locate1
if %FakeModeVar%==no goto Locate2
:Locate1
echo yes> ConfigVarini.txt
goto Locate3
:Locate2
echo no> ConfigVarini.txt
goto Locate3
:Locate3
if %SkypeVar%==yes goto Locate4
if %SkypeVar%==no goto Locate5
:Locate4
echo yes>> ConfigVarini.txt
goto Locate6
:Locate5
echo no>> ConfigVarini.txt
goto Locate6
:Locate6
if %SpotifyVar%==yes goto Locate7
if %SpotifyVar%==no goto Locate8
:Locate7
echo yes>>> ConfigVarini.txt
goto Locate9
:Locate 8
echo no>>> ConfigVarini.txt
goto locate9
:Locate9
if %SteamVar%==yes goto Locate10
if %Steamvar%==no goto Locate11
:Locate10
echo yes>>>> ConfigVarini.txt
goto Locate12
:Locate11
echo no>>>> ConfigVarini.txt
goto Locate12
:Locate12
if %ChromeVar%==yes goto Locate13
if %ChromeVar%==no goto Locate14
:Locate13
echo yes>> ConfigVarini.txt
goto Locate15
:Locate14
echo no>>>>> ConfigVarini.txt
goto Locate15
:Locate15
if %ChromeProfile%==1 goto Locate16
if %ChromeProfile%==2 goto Locate17
if %ChromeProfile%==3 goto Locate18
:Locate16
echo 1>>>>> ConfigVarini.txt
goto End
:Locate17
echo 2>>>>>> ConfigVarini.txt
goto End
:Locate18
echo 3>>>>>> ConfigVarini.txt
goto End
:End
cls
echo Files Configured!
When i run it i want it to take the user through a configuration process then save their preferences to a text file called ConfigVarini.txt
Then i will have another program that reads the settings on the text file. However the file on says
yes yes
and it saves no other settings.

You can append to a batch file using ">>"
#echo off
echo Hi >> test.txt
echo Hello >> test.txt
echo. >>test.txt
echo 3 >> test.txt
Results in A file called test.txt containing
Hi
Hello
3
Im not sure what excatly you want besides this.
Also its good practice to leave a space before a inputting to a file since numbers from one to nine will cause problems.

Imho best practice: initially, empty your output file using
type nul> ConfigVarini.txt
and all further writing by either
(ECHO any text)>> ConfigVarini.txt
or
>> ConfigVarini.txt (ECHO any text)
Note:
no space preceding > nor >> redirection operator nor ) closing parenthesis. Otherwise, that space appears as a trailing one in output file (could be at least weird or even harmful);
() parentheses: important if output text ends with a single digit 0, 1, ..., 9 .
Explication (examples given with 1 but valid for any single decimal digit):
echo 1>> ConfigVarini.txt appends nothing (or an empty line or ECHO is on/off text) to output file as the CLI or batch parser treats a single decimal digit as the numeric file handle to be redirected (see redirection link above);
echo 1 >> ConfigVarini.txt appends 1 with trailing space;
>> ConfigVarini.txt ECHO 1 appends 1 but we can't be sure whether there is an unwanted (forgotten) trailing space;
>> ConfigVarini.txt (ECHO 1) appends 1 without trailing space;
(echo 1)>> ConfigVarini.txt appends 1 without trailing space;
More explanation and discussion here

One single > will overwrite whatever might be in ConfigVarini.txt and add only ONE line. Two >> will add another line on a new line and leave the previously written content intact. With the code below each time Locate1 or Locate2 is started old settings will be overwritten and new ones applied. (I didn't properly check all the code, only changed your flawed >> usage. Someone check and edit if needed)
#echo off
title Kiel Configurations
rem Credits Will Go Here
rem Website Here
color 08
echo **********************************************************************
echo **************************Kiel Configuration**************************
echo **********************************************************************
echo ======================================================================
echo Use yes/no to configure the following options
pause
cls
echo Would you like it to run in fake mode? (recommended for slow computers or for the paranoid)
set /p FakeModeVar=(yes/no):
cls
echo Would you like to close skype?
set /p SkypeVar=(yes/no):
cls
echo Woud you like to close spotify?
set /p SpotifyVar=(yes/no):
cls
echo Would you like to close steam?
set /p SteamVar=(yes/no)
cls
echo Would you like to close chrome? (reccommended)
set /p ChromeVar=(yes/no):
cls
echo Which chrome profile would you like chrome to open as? (Ask me if unsure)
set /p ChromeProfile=(1,2,3...):
cls
echo Now configurating you settings...
pause >nul
if %FakeModeVar%==yes goto Locate1
if %FakeModeVar%==no goto Locate2
:Locate1
echo yes > ConfigVarini.txt
goto Locate3
:Locate2
echo no > ConfigVarini.txt
goto Locate3
:Locate3
if %SkypeVar%==yes goto Locate4
if %SkypeVar%==no goto Locate5
:Locate4
echo yes >> ConfigVarini.txt
goto Locate6
:Locate5
echo no >> ConfigVarini.txt
goto Locate6
:Locate6
if %SpotifyVar%==yes goto Locate7
if %SpotifyVar%==no goto Locate8
:Locate7
echo yes >> ConfigVarini.txt
goto Locate9
:Locate 8
echo no >> ConfigVarini.txt
goto locate9
:Locate9
if %SteamVar%==yes goto Locate10
if %Steamvar%==no goto Locate11
:Locate10
echo yes >> ConfigVarini.txt
goto Locate12
:Locate11
echo no >> ConfigVarini.txt
goto Locate12
:Locate12
if %ChromeVar%==yes goto Locate13
if %ChromeVar%==no goto Locate14
:Locate13
echo yes >> ConfigVarini.txt
goto Locate15
:Locate14
echo no >> ConfigVarini.txt
goto Locate15
:Locate15
if %ChromeProfile%==1 goto Locate16
if %ChromeProfile%==2 goto Locate17
if %ChromeProfile%==3 goto Locate18
:Locate16
echo 1 >> ConfigVarini.txt
goto End
:Locate17
echo 2 >> ConfigVarini.txt
goto End
:Locate18
echo 3 >> ConfigVarini.txt
goto End
:End
cls
echo Files Configured!

Related

Hybrid Batch-VBS TTS script not working

I tried to make a hybrid Batch-VBS script out of a VBS script that i already made. It would give a inputBox, and use the results to sapi.spvoice.Speak it. I tried to make it into a batch script (below), but it doesn't work, and tts.vbs ends up containing only
sapi.Speak message.
Batch Script:
#echo off
:start
cls
echo Batch Text-To-Speech
echo By SudDaBuilder
:: echo Fixed by %YourNameHere% ::
set /p msg=What do you want your PC to say?
set /p vce=Choose a Voice (0 - Male, 1 - Female)
pause
cls
echo Dim message, sapi, voice > tts.vbs
echo message=%msg% > tts.vbs
echo voice=%vce% > tts.vbs
echo Set sapi=CreateObject("sapi.spvoice") > tts.vbs
echo with sapi > tts.vbs
echo Set .voice = .getvoices.item(voice) > tts.vbs
echo .Volume = 100 > tts.vbs
echo end with > tts.vbs
echo sapi.Speak message > tts.vbs
cscript tts.vbs
cls
pause
:again
cls
set /p retry=Again? (y/n)
if %retry% == y goto start
goto end
:end
echo See you soon!
ping localhost > nul
You can embed the code directly into the batch script without using a temp file. This will increase the speed of the script as there will be no redundant IO operations:
<!-- : BATCH
#echo off
:start
cls
echo Batch Text-To-Speech
echo By SudDaBuilder
:: echo Fixed by %YourNameHere% ::
set /p msg=What do you want your PC to say?
set /p vce=Choose a Voice (0 - Male, 1 - Female)
pause
cscript //nologo "%~f0?.wsf" %msg% %vce%
:again
cls
set /p retry=Again? (y/n)
if %retry% == y goto start
goto end
:end
echo See you soon!
ping localhost > nul
exit /b %errorlevel%
BATCH : --->
<job><script language="VBScript">
Dim message, voice
message=WScript.Arguments.Item(0)
voice=WScript.Arguments.Item(1)
'WScript.Echo(voice & "--" & message)
set sapi = CreateObject("SAPI.SpVoice")
with sapi
Set .voice = .getvoices.item(voice)
'.Volume = 100
end with
sapi.Speak( message)
</script></job>
You even can use the sp voice objects in a one line:
#echo off
set /p "to_say=enter a text :"
mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('%to_say%')))"
Simply enclose your echos in a (code block) that is redirected to a file.
The code is much easier to read then.
To not end the code block prematurely,
the closing parentheses inside have to be escaped with a caret ^)
( echo Dim message, sapi, voice
echo message=%msg%
echo voice=%vce%
echo Set sapi=CreateObject("sapi.spvoice"^)
echo with sapi
echo Set .voice = .getvoices.item(voice^)
echo .Volume = 100
echo end with
echo sapi.Speak message
) > tts.vbs
A real hybrid consists IMO of only one file.
This is also possible, but requires handling of parameters via cmd line arguments.
The `>` needs to become `>>` from the twelfth line onwards.
Why?
The > character overwrites the contents of the file and adds the specifie content, so you only end up getting the last line. Whereas, the >> character(s) adds on the specified line to the end of the file's contents.
You also need to enclose the msg and vce variables in quotations.
Fixed script:
#echo off
:start
cls
echo Batch Text-To-Speech
echo By SudDaBuilder
:: echo Fixed by SO Suda ::
set /p msg=What do you want your PC to say?
set /p vce=Choose a Voice (0 - Male, 1 - Female)
pause
cls
echo Dim message, sapi, voice > tts.vbs
:: THIS IS THE TWELFTH LINE ::
echo message="%msg%" >> tts.vbs
echo voice="%vce%" >> tts.vbs
echo Set sapi=CreateObject("sapi.spvoice") >> tts.vbs
echo with sapi >> tts.vbs
echo Set .voice = .getvoices.item(voice) >> tts.vbs
echo .Volume = 100 >> tts.vbs
echo end with >> tts.vbs
echo sapi.Speak message >> tts.vbs
cscript //NoLogo tts.vbs
cls
pause
:again
cls
:: ADDED A DELETE FOR THE tts.vbs FILE::
del tts.vbs
set /p retry=Again? (y/n)
if %retry% == y goto start
goto end
:end
echo See you soon!
ping localhost >> nul

How do I open a text file to edit DIRECTLY in batch?

Batch language.
So, I made a small text editor called microPad. It can create a file, but I don't know how to have it open a file and write to it. Any ideas?
(I want to add another marker block called 'oeditor' at the end, after 'neditor'
#echo off
color c
title microPad v1.02
:options
echo ----------------------------------
echo microPad v1.02
echo ----------------------------------
echo.
echo 1 - Create a new document.
echo 2 - Preferences.
set /p opt=Select an option number.
if %opt%==1 goto neditor
if %opt%==2 goto prefs
if NOT %opt%==2 goto notopt
:notopt
cls
echo That isn't an option.
timeout -t 1 >nul
cls
goto options
:prefs
cls
echo 1 - Change the color.
echo 2 - Change the window size.
echo 3 - Return to options screen.
set /p prefopt=^>^>
if %prefopt%==1 goto colorpref
if %prefopt%==2 goto sizepref
if %prefopt%==3 goto options
if NOT %prefopt%==3 goto notopt
:colorpref
cls
set /p colors=^>^>
color %colors%
goto prefs
:sizepref
cls
echo The size should be between 50 and 1000.
set /p size=^>^>
mode %size%
:titling
echo ----------------------------------
echo microPad v1.02
echo ----------------------------------
echo.
echo.
echo Input the filename and remember to use the file extension.
set /p fname=">> "
timeout -t 1 >nul
title %fname% - microPad v1.02
cls
echo ----------------------------------
echo microPad v1.02
echo ----------------------------------
echo.
:neditor
set /p content=">> "
echo %content%>>%fname%
goto neditor

Issue with Batch file input for "War Games" game

If you are familiar with the 1983 movie "War Games" you should remember the part where the computer ask him "Shall We Play a Game". I have been trying to recreate that and this is what I have so far. The issue is after I enter the password the Command Prompt window closes out. Can someone help me find my mistake?
#echo off
title Shall We Play a Game?
color 0b
set /a tries=3
set password=Joshua
:top
echo %tries% Tries Remaining
set /p pass=Password:
if %pass%==%password% (
goto correct
)
set /a tries=%tries -1
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo CONNECTION TERMINATED
pause
exit
:correct
goto greeting
:greeting
echo Shall we play a game?
echo y/n
set input=
if %input%=y goto y
if %input%=n goto n
:y
echo How about
echo Chess
echo Tic-Tac-Toe
echo Snake
echo Global Thermonuclear War
if %opt%==Chess goto Chess
if %opt%==Tic-Tac-Toe goto TicTacToe
if %opt%==Snake goto Snake
if %opt%==Global Thermonuclear War goto Global Thermonuclear War
:n
echo Thats too bad! Maybe we should play some other day!
pause
exit
:chess
:tictactoe
echo Are you sure?
echo y/n
set response=
if %response%==y goto tictactoe1
if %response%==n goto tictactoe2
:tictactoe1
echo Go Back?
echo y/n
set feedback=
if %feedback%==y goto greeting
if %feedback%==n goto tictactoe2
:tictactoe2
echo testing
goto tictactoe2
This should be what you're trying to do:
#echo off
title Shall We Play a Game?
color 0b
set /a "tries=3"
set "password=Joshua"
:top
echo %tries% Tries Remaining
set /p "pass=Password: "
if "%pass%"=="%password%" (
goto correct
)
set /a "tries=%tries% -1"
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo CONNECTION TERMINATED
pause
exit
:correct
goto greeting
:greeting
echo Shall we play a game?
echo y/n
set /p "input="
if "%input%"=="y" goto y
if "%input%"=="n" goto n
:y
echo How about
echo Chess
echo Tic-Tac-Toe
echo Snake
echo Global Thermonuclear War
set /P "opt="
if "%opt%"=="Chess" goto Chess
if "%opt%"=="Tic-Tac-Toe" goto TicTacToe
if "%opt%"=="Snake" goto Snake
if "%opt%"=="Global Thermonuclear War" goto GlobalThermonuclearWar
:n
echo Thats too bad! Maybe we should play some other day!
pause
exit
:chess
:tictactoe
echo Are you sure?
echo y/n
set /p response=
if %response%==y goto tictactoe1
if %response%==n goto tictactoe2
:tictactoe1
echo Go Back?
echo y/n
set /p feedback=
if %feedback%==y goto greeting
if %feedback%==n goto tictactoe2
:tictactoe2
echo testing
goto tictactoe2
You forgot /P in a set a couple of times, this is used to get user input. In your set /a tries=%tries -1 you also forgot to put a second % around tries, this should be set /a tries=%tries% -1. Furthermore, you should put "" double quotes around your variables if you're comparing them, this prevents the script from braking if a variable doesn't exist or is empty. You also shouldn't have spaces in your labels, and you should put quotes around your set, like this: set "variable=value", this prevents trailing spaces from getting in your variables

Batch opening wrong file type

So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona

"access denied" error message for a text file i just made?

About 3 days ago I asked a question, which can be found here:
how to replace a string on the second line in a text file using a batch file?
I'm converting the letters in a text file into their respective numbers. I'm getting error messages such as "Access denied" and "Cannot locate this file" -- but the same batch file that's giving me all these errors is also the one that made these text files to begin with! So it should be in the same directory as the batch file itself (unless specified otherwise), right? I even went to that folder and checked, and they're there.
I did add a small script to hide the files after they were created so that it wouldn't look so cluttered up. I did this by using
attrib +h C:\script\%name%.txt
Would hiding a file with this command make it invisible to batch programs that are searching for it/call upon it?
Here's a link to the file, "stringparsing.bat": http://uploading.com/files/a1m1d2f4/stringparsing.bat/
If you could assist me in getting this program to carry out its task without any errors it would be greatly appreciated!
Here's the "stringparsing.bat" file in full:
#echo off
setlocal enabledelayedexpansion
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo Setting Variables...
echo Loading Language Database...
:: ###################################################################################
:: CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo ===================================
echo #################################
echo ####### Main Menu: #######
echo #################################
echo ===================================
echo.
echo.
echo 1.) Create New Language File...
echo.
echo 2.) Load Existing Lanuage File...
echo.
echo 3.) Settings...
echo ---------------------------------------------------------
SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS
GOTO MAIN
:CREATE
cls
title Step 1
echo.
echo.
echo.
echo =================================================================================
echo.
set /p name= please type a name for your new language file:
echo.
echo =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo ==============================================================
echo ##############################################################
echo #============================================================#
echo # #
echo # - After you hit enter you will be redirected #
echo # to a Live Typer. so anything you type into #
echo # it will be sent to %name%.txt. #
echo # #
echo # #
echo # - Next, select load language File For Encoding! #
echo # #
echo #============================================================#
echo ##############################################################
echo ==============================================================
set /p line1= :
echo %line1% >> %name%.txt 2> nul
echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================
goto LOAD
:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23
:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23
:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo Encoding Your Language File... Please Wait...
echo.
echo.
echo.
for /f "delims=" %%i in (!name!.txt) do (
echo translating "%%i"
set var=%%i
set var=!var:a=1 !
set var=!var:b=2 !
set var=!var:c=3 !
set var=!var:d=4 !
set var=!var:e=5 !
set var=!var:f=6 !
set var=!var:g=7 !
set var=!var:h=8 !
set var=!var:i=9 !
set var=!var:j=10 !
set var=!var:k=11 !
set var=!var:l=12 !
set var=!var:m=13 !
set var=!var:n=14 !
set var=!var:o=15 !
set var=!var:p=16 !
set var=!var:q=17 !
set var=!var:r=18 !
set var=!var:s=19 !
set var=!var:t=20 !
set var=!var:u=21 !
set var=!var:v=22 !
set var=!var:w=23 !
set var=!var:x=24 !
set var=!var:y=25 !
set var=!var:z=26 !
echo !var!
)
echo !var! > !name!.txt
pause >nul
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo Terminating service stream...
echo.
echo.
echo.
echo.
echo Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul
::(%xx%) -1 I/O Stream= "SHELL.dll"
:: IF EXIST [&1[Parser_2009]] exit
Exit
:: #####################################################################################
You've got a few problems. First, the access denied problem is from you redirecting to a hidden file.
echo %name% > Language_File.txt
attrib +h Language_File.txt
Note that the first time you run the script, it will work because Language_File.txt won't exist and therefore won't be hidden. The second time you run it, you'll get access denied. I don't know why Windows doesn't let you do that. You can solve this problem in a couple ways.
1. Save your file to the user's temp directory. With this approach your directory won't get cluttered.
echo %name% > %TMP%\Language_File.txt
2. Save your file to a subdirectory that you own so that it doesn't clutter the script's directory.
if not exist workspace mkdir workspace
echo %name% > workspace\Language_File.txt
3. Unhide the file before you use it. Since the file may not exist the first time you run the script, perhaps you should only attrib -h if it exists.
if exist Language_File.txt attrib -h Language_File.txt
echo %name% > %TMP%\Language_File.txt
attrib +h Language_File.txt
4. Don't use Language_File.txt at all! I don't see why you need it. Just use variables to hold the name of the language file. In fact, you already have the name in %name%, right?
Second, you should check the value of your variables to see what they really hold. When you load the contents of Language_File.txt into your variable, it's loading all the contents. That includes the hidden newline characters \r\n, although the script seems to bring them into the variable as spaces. See:
c:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>echo -%name%-
-langfile -
If you echo %name% surrounded by hyphens, you can see that there are 2 spaces after it from (presumably) the newline characters. To solve this problem, you can use set to trim the trailing characters.
C:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>set name=%name:~0,-2%
C:\batch\t>echo -%name%-
-langfile-
In the second example, `%name% doesn't have the hidden characters.
Finally, you only need to use ! to access variables that you set inside the for loop. So all references to !name! should be %name% instead. That's probably your "cannot find file" error.
heres the "stringparsing.bat" file:
#echo off
setlocal enabledelayedexpansion
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo Setting Variables...
echo Loading Language Database...
:: ###################################################################################
:: CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo ===================================
echo #################################
echo ####### Main Menu: #######
echo #################################
echo ===================================
echo.
echo.
echo 1.) Create New Language File...
echo.
echo 2.) Load Existing Lanuage File...
echo.
echo 3.) Settings...
echo ---------------------------------------------------------
SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS
GOTO MAIN
:CREATE
cls
title Step 1
echo.
echo.
echo.
echo =================================================================================
echo.
set /p name= please type a name for your new language file:
echo.
echo =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo ==============================================================
echo ##############################################################
echo #============================================================#
echo # #
echo # - After you hit enter you will be redirected #
echo # to a Live Typer. so anything you type into #
echo # it will be sent to %name%.txt. #
echo # #
echo # #
echo # - Next, select load language File For Encoding! #
echo # #
echo #============================================================#
echo ##############################################################
echo ==============================================================
set /p line1= :
echo %line1% >> %name%.txt 2> nul
echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================
goto LOAD
:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23
:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23
:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo Encoding Your Language File... Please Wait...
echo.
echo.
echo.
for /f "delims=" %%i in (!name!.txt) do (
echo translating "%%i"
set var=%%i
set var=!var:a=1 !
set var=!var:b=2 !
set var=!var:c=3 !
set var=!var:d=4 !
set var=!var:e=5 !
set var=!var:f=6 !
set var=!var:g=7 !
set var=!var:h=8 !
set var=!var:i=9 !
set var=!var:j=10 !
set var=!var:k=11 !
set var=!var:l=12 !
set var=!var:m=13 !
set var=!var:n=14 !
set var=!var:o=15 !
set var=!var:p=16 !
set var=!var:q=17 !
set var=!var:r=18 !
set var=!var:s=19 !
set var=!var:t=20 !
set var=!var:u=21 !
set var=!var:v=22 !
set var=!var:w=23 !
set var=!var:x=24 !
set var=!var:y=25 !
set var=!var:z=26 !
echo !var!
)
echo !var! > !name!.txt
pause >nul
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo Terminating service stream...
echo.
echo.
echo.
echo.
echo Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul
::(%xx%) -1 I/O Stream= "SHELL.dll"
:: IF EXIST [&1[Parser_2009]] exit
Exit
:: #####################################################################################
I Finally learned how to format the code snippet.
(heres a link to another copy of it if you need it.)
how to replace a string on the second line in a text file using a batch file?

Resources