batch file to read specific letters from a text file? - windows-7

I have a txt file called named.txt Is there a way to create a batch file to read every letters 2 through 7 letters from every line from name.txt and ignore the rest and output it onto a different txt file called name2.txt. For example I have this in the txt file:
G2010060sample.png
G2010061sample.png
G2010062sample.png
G2010063sample.png
and the batch file would create a new txt file like this :
2010060.png
2010061.png
2010062.png
2010063.png

Excellent online ressource http://ss64.com/nt/syntax.html
#echo off
if exist output.txt del output.txt
for /f "delims=" %%i in (input.txt) do call :ParseLine %%i
goto :eof
:ParseLine
set line=%1
set line=%line:~1,7%
echo %line%.png>> output.txt
goto :eof

cut -b 2-7,15-18 < infile.txt > outfile.txt

Related

How to sort a file on multiple positions using Windows command line?

I have an index text file, and I'm having trouble sorting it. I've been looking online for an answer, but Google hasn't pulled up anything with multi-positional searches.
Trying to do so with Unix (which would be easy), would be done as
sort inputfile -k1.1 -k3.3 -o outputfile
should accomplish the task, but trying to do so gives me Cygwin errors of already specifying the input twice (UNIX sorts are out!).
I need to sort this index file, either with Windows console applications or Perl on both positions.
Here is the input data:
1925699|0003352_0050003895.pdf|00500003895|0003352
1682628|0003352_0050003894.pdf|00500003894|0003352
1682628|0003352_0050003893.pdf|00500003893|0003352
The desired output is:
1682628|0003352_0050003893.pdf|00500003893|0003352
1682628|0003352_0050003894.pdf|00500003894|0003352
1925699|0003352_0050003895.pdf|00500003895|0003352
I'm currently trying to use:
sort/+1,7 /+32,11 < inputfile > outputfile
But I've failed to get this to be successful. (It only sorts the first parameter.) Again Unix is out of the question, and I can do it in Perl, but can this be done in Windows command line?
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q45575219.txt"
SET "outfile=%destdir%\outfile.txt"
SET "tempfile=%destdir%\tempfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
FOR /f "tokens=1,3delims=|" %%s IN ("%%a") DO (
ECHO(%%s%%t^|%%a
)
)
)>"%tempfile%"
(
FOR /f "tokens=1*delims=|" %%a IN (' sort "%tempfile%" ' ) DO ECHO(%%b
)>"%outfile%"
DEL "%tempfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q45575219.txt containing your data for my testing.
Produces the file defined as %outfile%
Uses a temporary file defined as %tempfile%
Read the source file, assigning each line to %%a. Analyse %%a using pipe as a delimiter and select the first and third tokens. Prefix the first and third tokens to the entire line, separated by a pipe and echo into a temporary file.
sort the temporary file, tokenise again on pipe, selecting the first token (before the first pipe) and the rest of the line; output only the rest to the destination file.

Script hangs when executing in windows batch file

Hello I have a batch file that is used to move text file from on directory to another. The problem is that when there is a text file larger then 7 MB the scrip hangs and freezes the process, which leads to manually force the batch to end.
Why does this bat hang when it moves larger files then 7mb?. How can I solve this to let it move any size text file?
Thank you in advance for your help.
PS. TYPE was used because the original file is in ANSI/UNIX format and the only way we found to convert it to ANSI/PC was using TYPE.
cd /d "c:\users\you\"
for %%i in (*.txt) do (
echo processing %%i
TYPE "%%i" | MORE /P > "c:\temp\%%i"
del "%%i"
)
The purpose of the more command is to present only one page worth of text at a time. When the output device is not a console, the effective page length is very long but not infinite - it will still pause after writing 65534 lines.
Instead, try this:
(for /F "delims=" %%L in (%%i) do #echo %%L) > "c:\temp\%%i"
Command line breakdown:
for /F - read the contents of a file
"delims=" - don't treat spaces or tabs as delimeters
%%L - the variable (same as the %%i in the for command from your original script)
%%i - the file to read
#echo - writes the variable to standard output
( ) > file.txt - redirects standard output to the destination file

Batch file attribute check

I'm having a little troubles with my script in .bat. My task is to write a script that will check a file for a few things. I've already defined some of those things, but now I'm stuck. My problem is that I don't know how to define a condition that says: If the file is hidden or read-only, delete this attribute and write some info about the change to the file (some text).
And then I'm having a second problem and that is that the script has always to write something to the file, but when I try to write something to the file (while the script is running) and then I save it, there is always just the thing the script has to write in it. Would somebody please give me some advice? I'm a novice. Thanks a lot for all the responses.
here is the script itself:
#echo off
title file-checking script
set file="file.txt"
set maxbytesize=1
type NUL > file.txt
pause
:loop
if exist file.txt #echo ok> file.txt
if not exist file.txt type NUL > file.txt
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
echo.File is under %maxbytesize% byte
) ELSE (
del file.txt
)
timeout/t 2
goto loop
read HELP FOR and you will notice that checking the attributes is similar to checking the file size
set ATTRS=%%~aA
Use >> instead of > to append data to an existing file, preserving existing content. The > redirection will overwrite any existing content.
You can use the following to test if a file is hidden (after you have proven that it exists):
dir /b /ah file.txt >nul 2>nul && (
echo file is hidden
) || (
echo file is not hidden
)

Batch file -> copying and comparing ASCII files

Can anyone help me write this batch file?
If there is no csv file inside "copy\" folder, batch file to copy
"original\file.csv" to "copy\file.txt"
Batch file to generate another csv file with the differences
between the newest .OK and the "copy/file.txt".
First point:
if not exist "copy\file.csv" copy "original\file.csv" "copy\file.csv"
Second point:
#echo off
( for /f "delims=" %%i in (file.txt) do (
findstr /C:"%%i" file.OK >nul || echo %%i
)
)>out.txt
Pseudo-Code for better understanding:
For each line in file text do:
Does this line exist in file.OK? If not, write the line...
...to out.txt

how to print words which is separated with delimiters using batch file?

I am very new to DOS Comments.
I have a text file which has so many entries but it s separated by a delimeter
(Ex: Hi; conf.txt; 161; new team)
I want to print each and every separation in a separate line.
Ex:
The output should be
Hi
conf.txt
161
new team
Can you guide me?
Try:
#echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%a in (file.txt) do (
set str=%%a
echo !str:;=!
))>file.tmp
move file.tmp file.txt >nul
Where 'file.txt' is the name of your text file (you need to change it in both places in the batch file).

Resources