How do I find spaces in strings with batch - batch-file

I've looked everywhere for an answer to this. Basically I need to find a " "(a space) in batch.
So, what i'm doing is making a custom command import/export batch script. Here is what i start by doing:
echo Enter commands file name without the .bat
set/p "file=>"
Now, after that set/p... line, I want to test if the "file" string contains a space.
How I can achieve this without using a different coding language? I'm keeping this strictly batch.

This works for me:
for /f "tokens=2" %%a in ("%file%") do Echo Contains Space
Which will only Echo Contains Space if there is a whitespace in file

Related

Is there a work around for ":" (colon) when downloading from ftp using windows

I'm creating a batch script to a get a file based on what option the user chooses. The only problem is, the file in the ftp server has a colon and from what i've researched, windows does not accept ":" colon.
Is it possible to replace that character before downloading?
Below is a sample of my code.
Echo open sample.net>first.dat
Echo user>>first.dat
Echo password>>first.dat
Echo ascii>>first.dat
Echo cd directory>>first.dat
Echo lcd folder>>first.dat
Echo get sample-text-10-16-2017_16:36:00:340033.txt>>first.dat
Echo bye>>first.dat
ftp -v -i -s:first.dat
del first.dat
As you can see also, I get a list first of the file names inside the folder for the user to input the file name. I just wrote a specific file name for the example
I'm still not familiar with the for loops in batch but I think that it is one way of replacing the characters in a file name before downloading
[Untried]
get remotefilename localfilename
is apparently valid, so placing a valid windows filename as a second argument should d/l to the file specified.
[Addendum - also untried]
(after Echo lcd folder>>first.dat)
echo mls remotefilesrequired awindowlistfilename>>first.dat
rem this should log in and create awindowslistfilename
rem containing the remote filelist
ftp -v -i -s:first.dat
del second.dat 2>nul
setlocal enabledelayedexpansion
for /f "delims=" %%a in (awindowslistfilename) do (
set "remote=%%a"
echo get !remote! !remote::=.!>>second.dat
)
endlocal
Echo bye>>second.dat
ftp -v -i -s:second.dat
del first.dat
del second.dat
Since I'm not aware of the return format for mls, I'm assuming that it's a simple file-list, one to a line.
This code first executes the ftp log-on palaver and an mls command, creating awindowslistfile locally.
It then deletes second.dat (the 2>nul suppresses error messages like file not found appearing on stderr)
setlocal enabledelayedexpansion and endlocal bracket a mode where the syntax changes such that !var! may be used to access the run-time value of a variable, whereas %var% always refers to the parse-time value.
The for/f command reads the filename (parenthesised) and assigns each line in turn to the metavariable %%a. The delims= option ensures that the entire line is assigned, ignoring the normal tokenising procedure.
A series of individual get commands is then written to second.dat, with the substitution of : by . in the name.
Finally, add the bye and FTP again.
(I'm not sure whether first.dat will also require a bye and second.bat will need to prelimnary commands, but could be...)
Note that it's batch convention to enclose filenames that may contain separators like Space,; in "quotes". How FTP will feel about that, if necessary, I can only guess.
Naturally, extra lines within the loop
set "remote=!remote:x=y!"
could be used to serially replace character sequences x by y if there are any other problematic characters encountered.

How to delete the first line of a file using Batch?

How to delete the first line of a file using Batch?
I really need help... I've tried everything but it didn't work. :(
I've tried this :
#echo off
Type test.txt | findstr /I /V /C:"So, lets remove this line" >>Test2.txt
exit
Some tasks are clumsy with pure batch techniques (and mangles characters) - this is a robust solution and uses a helper batch file called findrepl.bat that uses built in jscript-ing from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file.
type "file.txt"|findrepl /v /o:1:1 >"newfile.txt"
Another method is to use more.exe but it has limitations with the number of lines at 64K and it mangles TAB characters.
more +2 <file.txt >newfile.txt
(echo off
for /f "skip=1 tokens=1* delims=" %A in (c:\windows\win.ini) do echo %A
echo on)
You'll need to loop through your file line by line and output each line, except the first one.
From your example it looks like you might be looking for a specific string.
try this question/answer .. it might help you get you on your way.

Extract a line with a string with double quote by using findstr

First of all thank you for this site, I'm a terrible coder and so i need your help with making a batch script, i try to extract only lines that contains Copy " in that line from a text file by using findstr which actually works without that whitespace and double quote. But it will extract the line with "Copy and Help" too which i don't need.
Example:
My text contains (source.txt)
a
asd Copy and help with these command prompt:
a
asd Copy "c:\.." a b c(white space)
a
asd Copy and help with these command prompt:
Copy "d:\.." a c c(tab space)
avs
Copy "e:\.." a a c
vvddf
Output file (op.txt) (should be)
Copy "c:\.." a b c
Copy "d:\.." a c c
Copy "e:\.." a a c
After my original code I tried to usefindstr /c:"Copy ^"" > op.txtbut this doesn't work. To let the findstr know to search a line containing Copy " Copy[whitespace][double quote]
Updated section
First off my required batch script is working from help by foxidrive. There is still some tweak to do, but i will post it as another question. This question is answered.
This is my updated code for now.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="details.txt"
IF EXIST %source% (
FIND /i "copy " <%source% |FIND "\" >op.txt
) ELSE (
Exit
)
Thanks foxidrive for the start off and solving my first issue.
Sorry for my English, it's poor like my coding
A simplistic solution is to use this
find ":\" <source.txt >op.txt
Or this is another workaround:
find ":\" <source.txt |find /i "copy " >op.txt

How to open a file using a string variable in path name in batch file

I have a text file, call it path.txt in C:\path.txt and it will only have one line of text at a time. That line of text will be a file path, call it C:\projects\test.txt.
What's the best way to first read the text from C:\path.txt and then second use the sort command in a batch file to alphabetize the file whose path is defined by the text string in path.txt ?
Lastly, I want to erase the line of text from that C:\path.txt file.
Please let me know if this is too vague or needs a better explanation and thanks in advance.
The batch file I have now reads:
FOR /F "tokens=*" %%i IN (C:\DONOTMODIFY.txt) DO #ECHO %%i
set "filename=%%i"
SORT filename /O filename
Why the artificial limitation of only one line? Anyway yes this is simple enough. Anyway this will process any number of lines in target.txt. Code off my head so test before real use.
#echo off
for /f %%i in (target.txt) do (
sort %%i /o %%i
)
echo. > target.txt
EDIT:
Instead of echo. > target.txt you could use copy /y nul target.txt > nul that actually creates a totally empty file unlike echo. that makes a blank line.
PS: a tip for future questions: Show that you actually tried something. This is not a do my program for you site.

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