Windows - .Bat Editing Text File is missing pieces of Edit? - windows

I have a .bat file that is intended to a create a text file with parameters that could be used to execute a JS file, here me out ok.
The first .Bat file you would see is a SETUP.bat, thats what creates the 2nd .bat file for startup. But when I create the 2nd .bat file (.txt at this point in time) it misses some of my edit and I dont know why?
SETUP.bat
#echo off
break>"C:\Users\%username%\Desktop\test\START.txt"
echo set /p content=<"C:\Users\%username%\Desktop\test\lettercount.txt" >> C:\Users\%username%\Desktop\test\START.txt
echo call node "C:\Users\%username%\Desktop\test\index.js" content >> C:\Users\%username%\Desktop\test\START.txt
When its created it loses <"C:\Users\%username%\Desktop\test\lettercount.txt" and i dont know why?
I tried to do this but it didnt help either
SETUP.bat
#echo off
break>"C:\Users\%username%\Desktop\test\START.txt"
echo cd C:\Users\%username%\Desktop\test
echo set /p content=<lettercount.txt >> C:\Users\%username%\Desktop\test\START.txt
echo call node "C:\Users\%username%\Desktop\test\index.js" content >> C:\Users\%username%\Desktop\test\START.txt
and it still comes out as set /p content= instead of the thing it should be
any help?

According to my comment, and simplified.
#Echo off
(Echo Set /P "content="^<"%UserProfile%\Desktop\test\lettercount.txt"
Echo Call node "%UserProfile%\Desktop\test\index.js" content
)>"%UserProfile%\Desktop\test\START.txt"

Related

how to set a text file to be altered by a batch script and output to a .txt and erase the contents of the original text file?

ok so i have a text file i am trying to work on. the batch reverses text.
the batch works fine in cmd>batch.bat i am happy [enter]
yppah ma i
however i have been trying to do this. cmd>batch.bat input.txt [enter]
output.txt
so basically if input.txt says abcdef - output.txt will say fedcba and input.txt will be blank.
i have tried many snippets of code from similar questions on here and none have done well; input.txt is remained unchanged and output.txt remains empty.
when i try to run this to set the file to be run against the batch file,
C:....>batch.bat set /p Build=input.txt
i just get something like C:....>echo txt.tupni=dliuB p/ tes
txt.tupni=dliuB p/ tes
i have tried code from so many different questions on here and they all come out backwards rather than the file contents. i must be making a noob mistake. maybe there is a way i can edit the batch by adding a line in it... something like:
set file=input.txt
...
outfile=output.txt
i think that would simplify things but everytime i try to add set file and outfile to the batch file the whole batch stops working..
heres the batch file. it works it just doesnt like to work anyway other than C:...>batch.bat "sometext"
...
"txetemos"
i know me asking this question will annoy some of the more serious coders here but im doing my best to learn, and i feel like a simple alteration to this code would make it perfect... so heres the batch.
setlocal
if [%1] neq [] goto start
echo Reverse Given Text
echo Usage: %0 text
goto :end
:start
set _len=0
set _str=%*
:: Get the length of the sentence
set _subs=%_str%
:loop
if not defined _subs goto :result
::remove the first char
set _subs=%_subs:~1%
set /a _len+=1
goto loop
:result
set /a _len-=1
for /l %%g in (0,1,%_len%) do (
call :build %%g
)
echo %s%
goto :end
:build
:: get the next character
call set _digit=%%_str:~%1,1%%%
set s=%_digit%%s%
:end
endlocal
i have set sever different methods of implementing a set file= and an >outfile and have had no luck so i tried to add code on the cmd line and i just get
echo "edoc sdrawkcab"
"backwards code"
EDIT:cut out useless code and made the following edit;
EDIT!:
i have found that running batch.bat "text">>log.txt [enter]
makes,,, "txet" being appended into a log.txt... this solves the output to a text file problem..
now i just need to figure out how to get input.txt's text to be ran against batch.bat which will reverse its text using the working batch.bat
i think i can achieve this by making a maincaller "maincaller.bat" file to run this:
batch.bat [set-file=input.txt]>>log.txt
or something similar to result in something like this:;
input.txt reads abra cadabra
i need a command to result in
"arbadac arba" within the >>log.txt file
EDIT* OUTPUT.TXT will now be reffered to as log.txt
I don't quite get, what you want, but here is some code to "revert" a string:
#echo off
setlocal enabledelayedexpansion
set "string=Hello World"
set "reverse="
for /l %%i in (0,1,100) do set "reverse=!string:~%%i,1!!reverse!"
echo %string% - %reverse%

Call file with custom extension in batch

I'm using a second batch file in my main one to save/load variables, like this:
if not exist "KeemyDataPersistence.bat" (
echo #ECHO OFF > KeemyDataPersistence.bat
echo SET GENDER=F >> KeemyDataPersistence.bat
echo SET BGCOLOR=0 >> KeemyDataPersistence.bat
echo SET FGCOLOR=2 >> KeemyDataPersistence.bat
)
call KeemyDataPersistence.bat
It's working fine, but I'd like to save that second file (KeemyDataPersistence.bat) with another extension (.keemy), so KeemyDataPersistence.keemy. It saves fine just by replacing .bat by .keemy in the whole code, but when using call KeemyDataPersistence.keemy, it runs the windows default window to choose the program the user wants to open it with.
How would I be able to make it call the file as a batch file?
Try first to run this script once with admin permissions (and .bat extension):
#echo off
rem :: A files with .keemy extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:\caller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
rem :: associating file extension
assoc .keemy=batps
ftype batps=c:\caller "%%1" %*

Downloading file from server over FTP

I need to download a file that is placed in a folder called abc_20140221_123456 in server1 over ftp to my local directory. The problem is that the last six characters of the folder name are not fixed. For example today the folder may be called abc_20140221_123456 and tomorrow it might be called abc_20140221_234567. I am having problems in writing an automation batch script to do the same.
Here's the script I am working on:
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
echo open server1>>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
echo lcd E:\>>file.tmp
:: Not sure how to cd to abc_20140221_* from here
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I know that I can loop through directories using for like this:
for /d %%d in (' %path%/*%folderName%* ') do (
echo get filename.txt>>file.tmp
echo y>>file.tmp
)
But "for" doesn't work inside ftp>.
Any help is highly appreciated. Thanks.
I am assuming that for a given date there is only one abc_YYYYMMDD_nnnnnn directory.
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
set root=E:
echo open server1>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
for /D %%D in (%root%\%foldername%??????) do set target=%%D
echo lcd %target%>>file.tmp
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I set root to E:\ based on your original script - you can change it to whatever the actual parent directory is. I also changed
echo open server1>>file.tmp
to
echo open server1>file.tmp
so it creates a new temp file. Otherwise, it will keep appending to file.tmp each time the script runs. If that is what you want, or if you are generating a unique script file name each time and were just using file.tmp as a placeholder, ignore that change and use >> as before.
If there is more than one folder for a given day, I believe this script will use whichever one comes up last when the wildcards are expanded. I did not test this out, however.

using space in path for copying files using batch script

I want to copy all the *.jar files to another directory i wrote the below script
echo Enter path to ILM_HOME:
set /p ILM_HOME=
echo Deployment in progress
copy WEB-INF/lib/*.jar "%ILM_HOME%"/webapp/WEB-INF/lib
I use C:\Documents and Settings\asimon\Desktop\test as my input
It gives me syntax of the command is incorrect
I think the problem is Documents and Settings I even put "%ILM_HOME%" and I don't need c:\Docume~1\asimon\Desktop\test any other solution?
Update
This is working
#echo off
echo
echo Enter path to ILM_HOME:
set /p ILM_HOME=
IF EXIST "%ILM_HOME%\stopApplimation.bat" (
echo Deployment in progress
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"
CALL "%ILM_HOME%\stopApplimation.bat"
CALL "%ILM_HOME%\startApplimation.bat"
) ELSE (
echo %ILM_HOME% path is incorrect.
)
also any linux solution is also helpfull with .sh for the below 2 statements
"%ILM_HOME%/stopApplimation.bat"
"%ILM_HOME%/startApplimation.bat"
for linux, how can i replace the above 2 statements?
$ILM_HOME/stopApplimation.sh
$ILM_HOME/startApplimation.sh
Did you try
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"?
EDITED:
In your batch use CALL "%ILM_HOME%\stopApplimation.bat"

Shortest Windows batch file code to get first line of a file set to a variable?

I am looking for the shortest amount of code possible that will get the first line of a text file and set it to a variable in Windows.
There are lots of examples of this kind of thing in other threads on this StackExchange site but I am not looking for how to loop through and display all lines in a file. What I want is to just simply get the first line.
set /p var= <Text.txt
echo %var%
Referenced from: http://forums.techguy.org/software-development/551867-batch-file-read-line-text.html
Ironically the poster is able to get the first line but wanted to loop through the whole file!
This is just a slightly shorter version:
set/pz=<file
echo %z%
If delayed expansion has been enabled before (Setlocal EnableDelayedExpansion or cmd started with /V:ON option) then there's a shorter (one-liner) version
set /pz=<filename.txt&echo !z!
I couldn't get this to work under Windows 7; the variable was simply not set. But here's a slightly longer solution:
for /F "delims=" %%i in (Text.txt) do (
set Z=%%i
goto BREAK1
)
:BREAK1
echo %Z%
I had to change the line of file for a text file inside Program Files(x86) directory. After trying a lot of solutions, the following solution worked for me:
Steps:
Use type command piped with findstrto get a file with the desired data
Delete the original file
Use type command to move the content back to the file with the original file name
Delete the new file
Example:
#echo off
set originalFile="C:\Program Files (x86)\<Target File Path>\<Target File Name>"
set newFile="C:\Program Files (x86)\<Target File Path>\<Target newFile Name>"
if exist %originalFile% (
type %originalFile% | findstr /v T4VSHost >> %newFile%
del %originalFile% /F /Q
type %newFile% >> %originalFile%
del %newFile% /F /Q
)
#echo on
Note:
When I tried writing the change back to the original file in step 1, the result was a an empty file.
Note the string I am matching is T4VSHost, which occurs in the file at path C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude
just FYI...
I tried it and didn't work. But I realized finally that I had to reproduce the text file (originally the text file is generated by a powershell script) so when I did this and it works.
:: output to different file
Type Text.txt > text2.txt
Then apply the same code (change text.txt to text2.txt)
set /p var= <Text2.txt
echo %var%
If I use the original one, I would either get garbage in %var%, not sure if the text file has not <CR> or what. Anyway, by reproducing the text file, it seems working fine.

Resources