Search and Edit Text Files w/ Batch - batch-file

I'm fairly new with batch scripting, and currently trying to make my work easier (which will please my boss) with a script. Is there a way for a batch file to search a specific given input inside a text file, and echo into the text file?
For example, the text file looks like this:
4/11/2016
01530594
Time In: 1:30
01565832
Time In: 2:32
The given string is, in this example, 01530594 and I need to search for it inside the text file, and record the time out to make it look like this:
4/11/2016
01530594
Time In: 1:30
Time Out: 3:21
01565832
Time In:
If other stuff is needed to make it easier (say, already have Time Out: from the beginning, without a time) is required, that's more than acceptable.
I know search/replace/edit stuff with a batch file is tedious, is this possible?

set "timevalue=3:21"
(
for /f "delims=" %%i in (infile.txt) do (
echo %%i
echo %%i|find "Time In:" >nul && echo Time Out: %timevalue%
)
)>outfile.txt
The for loop reads the file line by line. The first echo writes the line to the outfile.
The second echo line is a bit more complicated:
echo %%i|find "Time In:" looks, if the line contains the string "Time In:". >nul writes its output to nirvana (we don't need it, we only want to know if it is there).
&& means "execute the next command, if the previous command was successful (that was find). If so, write an additional line Time Out: %timevalue%
Put the whole for loop into a block (between ( and ) and redirect it's output to a file

Related

Creating .bat file that will do the following

I need help creating a .bat file that will ask the user to input a specifi file name search in a specific file location with thousands of .txt files, move the single file to user desktop, and change it's .txt extention to .csv. If possible, loop code without closing/exiting the .bat. Any kind of help is apperciated. Thank you.
Welcome to StackOverflow. Over here, we don't just print free code for you to use. But since you said you don't know how to code, I made the file for you with steps on what each command does.
The code I wrote won't work since I had to import a "Select Folder" module that doesn't work in pure batch, so the final file is downloadable here as an executable file.
This is the source code, at which you should take a look at if you want to know how it works:
#echo off
rem <--- This is the REM command. Anything typed after "rem" will not show up. It is used mainly to comment.
rem The first line of code was "#echo off". Echo is the command used to manage printing text. In this case, we made it so anything typed will not show us other random text.
cls
rem "cls" is the command used to clear your screen of text and colors. I put it at the top in case you invoke the file from the command prompt, it will clear your screen.
color 9
rem "Color" is the command you write to flavor your text. The color light blue looks nice, so the code "9" will set all the text to that color.
rem Here's a list of all the different colors and their corresponding codes:
rem 0 = Black 8 = Gray
rem 1 = Blue 9 = Light Blue
rem 2 = Green A = Light Green
rem 3 = Aqua B = Light Aqua
rem 4 = Red C = Light Red
rem 5 = Purple D = Light Purple
rem 6 = Yellow E = Light Yellow
rem 7 = White F = Bright White
rem The command itself works like this: color <backgroundColor><foregroundColor>
rem So if I type "color F0", the background will be Bright White and the Text will be Black
echo;
rem "echo;" in this instance will produce a new blank line. I put it there just so it will look nicer once we run the file.
echo;
echo From which directory to produce tranfers?
rem Woah! This "echo" command is helpful! In THIS instance, we're using it to print text. Anything written after "echo" will be printed on screen.
:Repeat
rem ":Repeat" is a label. This means that at any point in the code we can go back to it and continue executing commands from where it started. You can make a label by typing ":" with a name after it.
cls
echo Selected Folder: %dir%
echo;
echo;
echo Which file to transfer? (Don't write extention)
set /P filetoget=
rem Cool! Finally! User input! "Set" is the command used for managing variables. In this instance, we are allowing the newly created variable "filetoget" to be set to whatever the user types after the "=" sign.
echo;
echo;
echo Locating...
rem "Locating..." will now be printed on screen. I put a couple of other notices to keep you updated on where the code is currently running. This is also helpful for when you get an error message, then you will know around what point in the code your error is taking place.
cd %dir%
rem "CD" keeps track of where the command interpreter should be directory-wise. This means that the command interpreter will check for files, create files, read files, manage directories and so much more based on where the "CD" is currently pointed in. For example, having the "CD" set to your folder means now we can move a file from that folder to wherever we want.
IF NOT EXIST %filetoget%.txt goto NotFound
rem "IF"! The lovely "IF"! So many uses, I can't list them all. But for the most part, you're right. It detects if "IF" statements are true or false. "IF EXIST" or "IF NOT EXIST" makes sure that a file is existing at the location of the current "CD".
rem "Goto" is our twin for the Labels we talked about earlier. This command in total says "Check if the file that the user told us about earlier exists. If it doesn't, go to a place in the code labeled ':NotFound'"
echo Moving...
move /Y %filetoget%.txt "%UserProfile%\Desktop"
rem Here is the actual fun bit. Doing the process of moving the file. "Move" moves files from one place to another. Here, we are moving the variable the user gave us earlier (the file we selected) to the Desktop. Also, the "/Y" means that if there is another file in the same folder with the same name we are about to set it to, it will override it and replace it.
cd "%UserProfile%\Desktop"
echo Changing filetype...
rename /Y %filetoget%.txt %filetoget%.csv
rem "Rename" is just as simple to use as "move". Here we take the file the user gave us, and change the .txt extension to a .csv extension. We could also change the name of the file itself, but in this case we don't want to.
echo Completed!
timeout /T 0 /NOBREAK >nul
rem "Timeout" is simple but hard to understand at a first glance. It pauses the interface for an amount of time, in this case for 0 seconds. 0 seconds on a "timeout" command stops it for just the slightest bit. I put this here so you can perhaps notice the "Completed" message I wrote before it. Obviously you could set a bigger number for more seconds.
goto Repeat
rem Mainly, here our code ends. Since all was completed successfully, it should go back to the Label "Repeat" to loop all the commands we have just executed.
:NotFound
echo;
echo;
echo Your file was not found in the directory selected! Make sure you have typed the name of the file without it's extension, and that you have selected the correct directory.
echo;
pause
rem "Pause" is the command used to pause the command prompt until a key is pressed. Pretty simple.
goto Repeat

Copying every nth line from text file

I have a very large data set (~7 million lines) from a data logger. I would like to sample the data and copy every 800th line to a new text file. The motive for this is that the text file is too large to import into my analysis software. Ideally, I would like to use a batch script or something similar. Speed is very important, as it is possible that future data files could be even larger.
At one point, I had something kind of working using:
findstr/N . test.txt| findstr ^[0-9]*0: >temporaryFile
FOR /F "tokens=1,* delims=: " %%i in (temporaryfile) do echo %%j > outputFile.txt
Which would keep one line out of every 10. I am not super familiar with the syntax, and this does not currently work as intended.
Edit:
The solution put in by #LotPings works well

How to copy one line from a text document to clipboard using command prompt

I need to be able to copy one line at a time from a text document to clipboard using command prompt.
Example I have a text document like this:
Line 1
Line 2
Line 3
etc...
Example: I need to be able to copy line 2 to clipboard and only line 2
What command would something like that be? or is there even a command like that?
#ECHO OFF
SETLOCAL
FOR /f "skip=1delims=" %%a IN (q27763354.txt) DO ECHO %%a|clip&GOTO done
:done
GOTO :EOF
I used a file named q27763354.txt containing your data for my testing.
the number used as "skip" is one less than the line-number you want. skip=0 is invalid, omit the skip=0 if you want the first line from the file.

redirecting a stream for the entire .bat file inside itself

I've recently started working with .bat files, and I'm trying to redirect the output to a file.
I've found 2 options, so far:
echo aaa > out.txt - which sends the output of the single echo command to the specified file (can also be appended using >>)
calling the entire file from the cmd using somefile.bat > out.txt (which is actually similar to number 1, as it sends the output of the single command somefile.bat to out.txt)
What I'm looking for is something else - I'm trying to have a line in my file that sends all the output from that point forth to the file.
Thanks!
echo this goes to screen
(
echo this line goes to the file
echo also this line and the ping-output
ping www.stackoverflow.com
echo and this
)>file.txt
echo this goes to screen again
Note:
all inside the block (between ( and )) is parsed at once. If you use variables inside the block, you may need delayed expansion.
There is no universal solution. It depends of the batch file requirements.
For a lot of batch files, the answer from Stephan will work without problems, taking in consideration what he pointed: all the code is inside a block and any variable management inside it may require delayed expansion.
Other alternative is to move the code under a subroutine, calling it with the redirection
#echo off
call :mainProcess %* > outputFile
exit /b
:mainProcess
:: here the batch file begins
echo %1 %2 %3

Loop through each line and delete it after

I have a MS-DOS batch script that executes a custom command for each line in a specific text file. And I want it to delete each line after processed so that the next time I open the batch I can resume it from where I stopped without starting all over and executing the loop through the same lines. The lines only have numbers.
file.txt:
76561197967664150
76561197960466635
76561197969570587
76561197978933289
76561198011880885
76561197977884769
76561198010665215
76561198012847269
76561197988209745
76561197991756815
76561197999860750
76561198012060656
76561198020700372
76561198005281666
ALSO, if it is easier, the code may also delete 250 lines all at once after processed.
Why not write out the last number you processed to a file instead of deleting from the main file? That would make it much simpler. Next time you process the file, you skip numbers until you find the one you wrote out to disk.
if you don't care to rewrite your file with each processed line:
:loop
REM read first line:
set /p first=<file.txt
REM write changed file.txt:
find /v "%first%" file.txt>file.tmp
del file.txt
ren file.tmp file.txt
echo processing %first%...
echo press any key for next line or CTRL-C to break
pause >nul
goto :loop
NOTE: if you really mean MS-DOS (which I doubt), this won't work.

Resources