I have a little issue when my batch writes the time before 10 AM.
What it does is scan the barcode from a user card and writes date, time and that code into a .csv file, so that it can be imported into a excel file.
here is my code:
#echo off
...
:: set /p fname="Digite o nome do arquivo a guardar os logs: "
set fname=refeicao
goto REF
:REF
....
set cod=""
set /p cod="-> "
if /i %cod%=="" goto err
goto ok
:print
echo %date% %time% %cod%>>w:\REFEICAO\%fname%.csv
goto REF
:err
....
goto REF
:OK
...
goto print
:qq
exit
The problem is that when time is earlier than 10AM, it exports with a space before, let's suppose it's 4 AM. It would be like: " 4:00:00,00" instead of "04:00:00,00"
How could i get rid of that space or turn it into a "0"?
[Actual code inserted - minus irrelevant echos etc.]
echo %time: =0%
echo %time: =%
The first line changes spaces to 0, the second one removes spaces. Use the one better fits your problem
Related
Hello please help me with this one!
I would like to check if the user input URL contains the defined SUBSTRING or not.
If yes I would like to GOTO LONG else GOTO SHORT
Thank you!
#echo off
setlocal enabledelayedexpansion enableextensions
SET /P "URL= Input the link of the video: "
SET "SUBSTRING=?filter=archives&sort=time"
ECHO !URL! | FINDSTR /C:"!SUBSTRING!">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END
:END
pause
Using double quotes properly helps fix some of your problems. There shouldn't be any need to use delayed expansion if you use quotes for the echo as well.
#echo off
SET /P "URL=Input the link of the video:"
SET "SUBSTRING=?filter=archives"
ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:END
pause
Update Showing execution of batch file.
C:\Users\Squashman\Desktop>test.bat
Input the link of the video:https://www.twitch.tv/videos/578427308?filter=archives&sort=time
THIS IS A LONG LINK
"THE LINK NUMBER IS 1"
Press any key to continue . . .
So this is the working code. I will now mention the problems I had so others can learn from it
I must not enable setlocal enabledelayedexpansion
I have to use quotes around variables for the SET command
Because I have delayedexpansion disabled I must use percentages instead of exclamation marks when I want to use a variable in a command later
Correct me if I am wrong but thats what I figured out with these guys' help and on my own by trial and error
I made this list and updated the code so if a newbie like me gets stuck they can have a look at this post.
Once again thank you guys!
#echo off
SET /P "URL=Input the link of the video:"
SET "SUBSTRING=?filter=archives"
ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:END
pause
A user defined input file has to be integrated into the entries of pre-set output file,
changing the data at a given block [A-Z] and index [1-75] position.
The input file structure with wildcards for variable parts:
* 00asr[1-75] 00asr*
b -v -o 00asr34 00asr34.hkx (example)
b -v 00asr35 00asr35.hkx (example)
Output file as above but with multiple blocks [A-Z]asr[1-75]:
* Aasr[1-75] Aasr*
* Basr[1-75] Basr*
...
* Zasr[1-75] Zasr*
The user has to pick a given letter A-Z to determine parts of the string and the block to replace information in.
Then we can replace the %input% substrings 00asr with %block%asr; they will then match given substrings in the %output%:
* 00asr[1-75] 00asr* >> * Zasr[1-75] Zasr* in the case of Z.
#ECHO OFF
ECHO INPUT FILE:
ECHO %1
SET INTEXT=%1
ECHO.
SET /P "LETTER=Letter? "
SET "REPLACE=%LETTER%asr"
SET TMP=tmp.txt
setlocal enabledelayedexpansion
FOR /F "tokens=1,* delims=¶" %%A IN ( '"TYPE %INTEXT%"') DO (
SET string1=%%A
SET modified=!string1:00asr=%REPLACE%!
echo !modified! >> %TMP%
)
PAUSE
I adapted this code. Looks like good code to me, but I'm like a blind man.
Now either the %TMP%s first-line [1-75] is required to determine whether there is any offset in the block
or the strings have to be compared, overwriting the line containing substring * Zasr[1-75] Zasr* with the same one from %TMP%;
or in the case of offset skipping a given amount of lines in %output% before overwriting the lines.
I'm not sure which option would be easier to implement or quicker to execute,
since I'm entirely failing at doing this.
The whole method should work something like this:
Input File
o <parameters> 00asr3 00asr3.hkx
o <parameters> 00asr4 00asr4.hkx
o <parameters> 00asr5 00asr5.hkx
User Input
Q
OUTPUT BEFORE
...
p Oasr75 Oasr75.hkx
p Qasr1 Qasr1.hkx
p Qasr2 Qasr2.hkx
p Qasr3 Qasr3.hkx
p Qasr4 Qasr4.hkx
p Qasr5 Qasr5.hkx
p Qasr6 Qasr6.hkx
....
OUTPUT AFTER
...
p Oasr75 Oasr75.hkx
p Qasr1 Qasr1.hkx
p Qasr2 Qasr2.hkx
o <parameters> Qasr3 Qasr3.hkx
o <parameters> Qasr4 Qasr4.hkx
o <parameters> Qasr5 Qasr5.hkx
p Qasr6 Qasr6.hkx
....
I managed to write the file renaming parts just fine; but this... I don't even.
I have been looking at various articles but due to not actually knowing any batch I'm not getting ahead with this.
1 Was very helpful, but I outright fail to compare strings correctly and can't seem to manage to assign them to variables.
Any help is appreciated. I might even understand what I'm doing wrong (which currently is most things).
I've solved it by myself.
Here's a shortened version of the script.
Cleaning input somewhat, changing the token in tmp, getting target parameters,
removing old target strings from file, merging files, deleting temps.
I'm confident this code isn't very good either way so further explanation seems unnecessary.
#ECHO OFF
SET _PACK=%1
SET "PATH=%~dp0"
SET "_LIST=in.txt"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_ERROR=0"
:USERINPUT
IF %_ERROR%==1 (
ECHO INVALID INPUT: string too long.
SET "_ERROR=0"
)
IF %_ERROR%==2 (
ECHO INVALID INPUT: non-letter.
SET "_ERROR=0"
)
SET /P "LETTER=ENTER NEW BLOCK LETTER A-Z: "
ECHO "%LETTER%" SELECTED
IF NOT "%LETTER:~1,1%"=="" (
SET "_ERROR=1"
GOTO USERINPUT
)
if "%LETTER%" lss "A" (
SET "_ERROR=2"
GOTO USERINPUT
)
if "%LETTER%" gtr "Z" (
SET "_ERROR=2"
GOTO USERINPUT
)
SET "_STRING=%LETTER%asr"
' renaming files here
SET TMP=asr.tmp
DEL %TMP%
SET "_COUNT=0"
FOR /F "tokens=1,* delims=¶" %%A IN ( '"TYPE %_PACK%"') DO (
SET _LINE=%%A
SET _NEWLINE=!_LINE:0asr=%_STRING%!
ECHO !_NEWLINE!>> %TMP%
CALL SET /A _COUNT=_COUNT+1
)
ECHO NUMBER OF INPUT LINES: !_COUNT!
SET /P _OFFSET=< %TMP%
CALL SET _OFFSET=%%_OFFSET:*asr=%%
CALL SET _OFFSET=%%_OFFSET:~0, 2%%
CALL SET _OFFSET=%_OFFSET: =%
ECHO OFFSET: %_OFFSET%.
SET /A _END=_COUNT+_OFFSET-1
COPY /Y in.txt in.tmp
FOR /L %%X IN (%_OFFSET%, 1, %_END%) DO (
SET "_SEARCH=%_STRING%%%X "
ECHO DELETING OLD STRING : !_SEARCH!
FINDSTR /V /C:"!_SEARCH!" %_PACK% > in.tmp
del in.txt
ren "in.tmp" "in.txt"
)
TYPE %TMP%>> in.txt
DEL %TMP%
PAUSE
DEL %1
EXIT
I'm trying to compare a bunch of files with names like
"BD12-CD9.txt"
to folders named
"BD12-CD9 - somefoldername"
I'm removing the .txt extension to get the ID code "BD12-CD9" and want to check if the folder name contains the code.
I can't figure out how to do this without an if statement that compares strings exactly. (might be an easy solution here like jQuery's indexOf())
So I need to strip the ID code from the file name and do a direct comparison.
The problem is the ids and names are not always exactly the same length e.g. "BD12-CD10.txt" so I can't just do "set folderName2=!folderName:~0,8!"
I found a strlength function and put it in another batch file called strlen.bat and it works great, echoing the length of my stripped ID codes. How do I use the echoed value though?
move.bat
#echo off
#setlocal enabledelayedexpansion
set "errfolderpath=C:\Testing\Moving txt files automatically\"
echo.
for %%x in (*.*) do (
set fileName=%%x
set fileName2=!fileName!
set fileName2=!fileName2:~0,-4!
call strlen !fileName2!
FOR /D %%K in ("%errfolderpath%*") DO (
SET folderName=%%~nK
set folderName2=!folderName!
set folderName2=!folderName:~0,11!
echo folder: !folderName2!, file: !fileName2!
if !folderName2! == !fileName2! echo MOVE '!fileName!' to '!folderName!'
)
echo %_len%
echo.
)
PAUSE
strlen.bat
#echo off
:: strlen.bat
:: http://acm.zhihua-lai.com
if [%1] EQU [] goto end
:loop
if [%1] EQU [] goto end
set _len=0
set _str=%1
set _subs=%_str%
:getlen
if not defined _subs goto result
:: remove first letter until empty
set _subs=%_subs:~1%
set /a _len+=1
goto getlen
:result
echo %_len%
shift
goto loop
:end
For the batch file variable usage, if you look to the code, a variable called _len is defined. This variable is then echoed to console, but is not removed (no reasignation, no endlocal, ...) , so just use it.
Anyway, it is easier than it seems.
#echo off
setlocal enableextensions disabledelayedexpansion
set "errfolderpath=C:\Testing\Moving txt files automatically"
for %%a in (*.txt) do (
set "notMoved=1"
for %%b in ("%errorfolderpath%\%%~na*") do if defined notMoved (
set "notMoved="
move "%%~fa" "%%~fb"
)
)
For each file, search a folder with the same prefix (the name of the file referenced in %%a, without extension, is %%~na). If found, move the file (the full path to the file is %%~fa) to the target folder and use a switch variable to "mark" the file as moved to avoid problems in the case of two folders with the same prefix (that will be retrieved in the inner for %%b) .
I have a text file that groups different subsystems using [ ] and then contains item flags in each subgroup. Here is a snippet of the file such that you can get an understanding for what it looks like (notice each subgroup can have the same items):
[EV]
Verbosity=0
Alignment=123
[FluidLevelControl]
BufferTypeLastUsed=TWEEN
Enable Dip Tube=no
Alignment=456,efg
[PressureLevelControl]
Enabled=yes
Alignment=789,abc
Calibration Date=1280919634
[BufferFrontValve]
Log=yes
Alignment=987
Note, the above file is in excess of 2000 lines. I imagine the script is going to take a little while to execute. I also know that there is a better framework to do this but in our application we need it to run from a flash drive and be able to be plugged into our instrument which run WinXP without a .NET frameworks etc.
What I would like to do is use a .bat file to search the document for a specific subsystem (ie. [DesiredSubsystem]) and desired item within the subsystem then modify the item data. For example, in the above text I may want to change the Alignment from 789 to 12345 in the PressureLevelControl subgroup.
I understand there is no way to effective replace / update a text file using a bat file. I've created a function to read in a file and write it to a new file, now I'm trying to develop a clean way to identify the line items and what subgroup they are in as to replace the desired text as needed.
Here is what I have commented with my plan:
Update: I spent the afternoon writing some code that seems to work as shown below, there is most def better methods.
::SET VARS
set "varDebugFP=\\svchafile\Teams\Test Engineering\Productivity Tools\MFG BAT Files\SpecificTest\"
set varSource=%varDebugFP%Debug\
set varDestination=%varDebugFP%Debug\
set varFileName=specific.ini
::Do Text File Editing
setlocal enableDelayedExpansion
set "LastGroup=NONE"
::preserve blank lines using FINDSTR, even if a line may start with :
for /f "usebackq delims=*" %%A in (`type "%srcFile%" ^| findstr /n "^"`) do (
set "strLine=%%A"
set "strLine=!strLine:*:=!"
::Check to see if the is defined and greater than 2 characters inidicating a good line
if defined strLine if NOT "!strLine:~2,1!"=="" if "!strLine:~0,1!!strLine:~-1!"=="[]" (set "LastGroup=!strLine!")
::Set the paramaters looking to match
set "DesiredGroup=[TestGroup]"
set "DesiredItem=TestItem"
set "ReplaceLineWith=NewTestItemLine=NewData"
::Look for match on current line
if defined strLine if "!LastGroup!"=="!DesiredGroup!" if NOT "!strLine!"=="!strLine:TestItem=Mod!" (set "strLine=!ReplaceLineWith!")
::Note, in the above line I would like 'TestItem' to be the 'DesiredItem' variable but I can't get it working due to the DelayedExpansion
::Set the additonal paramaters looking to match
::Note, there are multiple items I want to change at once without having to reitterate through the org long (2000+lines) file
set "DesiredGroup=[TestGroup2]"
set "DesiredItem=TestItem2"
set "ReplaceLineWith=NewTestItemLine2=NewData2"
if defined strLine if "!LastGroup!"=="!DesiredGroup!" if NOT "!strLine!"=="!strLine:TestItem=Mod!" (set "strLine=!ReplaceLineWith!")
::I plan to copy and paste the above section as many times as needed to capture all the lines I need to edit (at this point about ~10)
::I don't really understand why the "(" in the below line, I found it in an example on stackoverflow and it seems to work.
echo(!strLine!>>"%newFile%"
)
endlocal
::Replace org file with new file, delete org file (this part I have figured out)
Is there a better way of doing this? Can anyone help complete the code as I'm having a lot of trouble parsing this correctly.
Update: Thanks for the two methods proposed in the answers below. They are very long and I learned a lot from them. Not entirely sure how to implement the functions however and my biggest concern is that using the function will make repetitive reads from the file slowing it down dramatically. I'm very new to this bat file thing but I know its very powerful if you know the commands and are creative.
Thanks in advance for any and all help. -Dan
So many people want to use batch to edit text files - There are many SO questions dealing with the subject. But it is quite difficult (and relatively slow) to do so robustly using only native batch commands.
You are better off using some other tool. One good option is to use something like a free Windows port of sed or awk. But those require downloading non-native executables onto your machine, something that is forbidden in many offices.
I have written REPL.BAT - a hybrid JScript/batch utility that performs a regular expression search and replace on stdin and writes the result to stdout.. The script only uses native scripting available to all modern Windows machines from XP onward. Full documentation is embedded within the script, including a link to a MicroSoft page that describes all available JScript regex metacharacters.
Assuming that REPL.BAT is in your current directory, or better yet, somewhere within your PATH, then the following simple batch script can be used to modify the value of any item within a specific subsystem.
::MODIFY_CONFIG.BAT File SubSystem Item NewValue
::
:: Any argument that contains spaces or special characters should be quoted.
::
:: File = File to modify, may include full path
:: SubSystem = The section containing the item to modify (without brackets)
:: Item = The Item within the SubSystem that is to be modified
:: NewValue = The new value for the item
#echo off
type "%~1"|repl "(^ *\[%~2] *\r?\n(?: *[^[].*\n)*? *%~3=)[^\r\n]*" "$1%~4" m >"%~1.new"
move /y "%~1.new" "%~1" >nul
Here is a call to the script that changes Alignment within PressureLevelControl to 12345
MODIFY_CONFIG yourFile.ini PressureLevelControl Alignment 12345
#ECHO OFF
SETLOCAL
:: Read parameters
:: %1 is subgroup
:: %2 is item
:: %3 is new value
:: %3 missing = report value
SET "subgroup=%~1"
SET "item=%~2"
SET "newval=%~3"
IF NOT DEFINED subgroup ECHO syntax:%~nx0 "subgroup" "item" "newvalue"&GOTO :EOF
IF NOT DEFINED item ECHO syntax:%~nx0 "subgroup" "item" "newvalue"&GOTO :EOF
ECHO %*
:: state=0 (looking for subgroup) 1 (found subgroup)
SET /a state=0
:: result=0 (did nothing) 2 (found subgroup, not data line) 3 (found subgroup more than once)
:: 4 (found and replaced data line) 5 (found subgroup more than once, replaced data once)
:: 6 (detected dataline more than once, replaced once) 9 (reporting only - value found)
SET /a result=0
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "^" q21263073.txt') DO (
SET "line=%%b"
CALL :process
IF DEFINED repro ECHO(%%b
REM pause
)
)>newfile.txt
SET "replacefile="
CALL :report%result%
IF DEFINED replacefile ECHO A NEW FILE HAS BEEN CREATED
GOTO :EOF
:report0
ECHO [%subgroup%] NOT found
GOTO :eof
:report2
ECHO [%subgroup%] %item% NOT found
GOTO :eof
:report3
ECHO [%subgroup%] found repeatedly - %item% NOT found
GOTO :eof
:report4
ECHO [%subfound%] %olditem% found replaced %oldvalue% with %newval%
SET replacefile=Y
GOTO :eof
:report5
ECHO [%subgroup%] found repeatedly - %olditem% found replaced %oldvalue% with %newval%
GOTO :eof
:report6
ECHO [%subgroup%] %item% found repeatedly - %olditem% found replaced %oldvalue% with %newval% ONCE
GOTO :eof
:report9
ECHO [%subgroup%] %olditem% found with value %oldvalue%
GOTO :eof
:process
:: blank line ?
SET repro=Y
IF NOT DEFINED line GOTO :EOF
IF "%line:~0,1%%line:~-1%"=="[]" GOTO fsubsys
:: only process data lines if state=1
IF NOT %state%==1 GOTO :EOF
IF %result% gtr 5 GOTO :EOF
SET "fvalue="
FOR /f "tokens=1*delims==" %%p IN ("%line%") DO SET "fitem=%%p"&SET "fvalue=%%q"
:: Did we have an item=value line?
IF NOT DEFINED fvalue GOTO :EOF
CALL :matchit "%fitem%" "%item%"
IF NOT DEFINED matched GOTO :eof
:: we found a matching item within a subgroup.
:: result must be 2,3,4 or 5
FOR %%z IN (2.4 3.5 4.6 5.6) DO FOR /f "tokens=1,2delims=." %%c IN ("%%z") DO IF %result%==%%c SET result=%%d
IF %result%==6 GOTO :EOF
:: Haven't yet replaced value
:: Do we have a replacement?
SET "olditem=%fitem%"&SET "oldvalue=%fvalue%"
IF NOT DEFINED newval SET result=9&GOTO :eof
SET "repro="
ECHO(%fitem%=%newval%
GOTO :eof
:: found a subgroup name
:fsubsys
SET /a state=0
:: Is it the one we're looking for?
CALL :matchit "%line:~1,-1%" "%subgroup%"
IF NOT DEFINED matched GOTO :eof
SET /a state=1
FOR %%z IN (0.2 2.3 4.5) DO FOR /f "tokens=1,2delims=." %%c IN ("%%z") DO IF %result%==%%c SET result=%%d
IF %result%==2 SET "subfound=%line:~1,-1%"
GOTO :eof
:: match %1 to %2. If matches, set matched to not empty. if not, set matched to empty
:: here is where we can have some fun.
:matchit
SET "matched="
SET "string1=%~1"
SET "string2=%~2"
:: Case-insensitive exact match?
IF /i "%string1%"=="%string2%" SET matched=Y&GOTO :EOF
:: partial-string match. If specified item begins "+" then match rest against item found
:: so +ali matches "Alignment"
IF NOT %string2:~0,1%==+ GOTO npsm
CALL SET string3=%%string1:*%string2:~1%=%%
IF /i "%string3%"=="%string1%" GOTO :eof
IF /i "%string2:~1%%string3%"=="%string1%" SET matched=Y
GOTO :EOF
:: initials - so "Enable Dip Tube" is matched by "edt"
:npsm
CALL :inits %string1%
IF /i "%string3%"=="%string2%" SET matched=Y
GOTO :eof
:inits
SET "string3=%2"
IF NOT DEFINED string3 GOTO :EOF
SET "string3="
:initsl
SET string1=%1
IF NOT DEFINED string1 GOTO :EOF
SET string3=%string3%%string1:~0,1%
SHIFT
GOTO initsl
OK - I got carried away...
I used a file named q21263073.txt with your sample data for my testing. The file newfile.txt will be produced and may be made to replace the original if desired (advisable only if replacefile is defined.)
Required syntax is thisbatch subgroup item newvalue
Any parameter that contains spaces should be "quoted".
Features:
name-matching is case-insensitive.
You can use a leading + to abbreviate to a unique start-of-string, so +pr
would match PressureLevelControl.
Error report produced if abbreviation is not unique within a section.
You can abbreviate Space Separated Names to the initials SSN.
if the newvalue is omitted, a report of an existing value is shown.
It should be fairly obvious that you could easily make modifications to allow values to be added or deleted rather than just changed.
Playing with the filenames and other matters are now in the hands of those that show an interest.
BATCH FILE to remove duplicate strings (containing Double Quotes); and keep blank lines
Note: The Final Output must have original strings with Double Quotes and Blank lines.
I have been working on this for a long time and I can not fine a solution, thanks
in advance for your assistance. When I get the remove duplicates working something
else doesn't... I know that it looks like I haven't done much work but I have
trimmed this down for clarity.
#echo on
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL EnABLEDELAYEDEXPANSION
REM -- Prepare the Prompt for easy debugging -- restore with prompt=$p$g
prompt=$g
rem The finished program will remove duplicates lines
:START
set "_duplicates=TRUE"
set "_infile=copybuffer.txt"
set "_oldstr=the"
set "_newstr=and"
call :BATCHSUBSTITUTE %_infile% %_oldstr% %_newstr%
pause
goto :SHOWINTELL
goto :eof
:BATCHSUBSTITUTE
type nul> %TEMP%.\TEMP.DAT
if "%~2"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %1|find /n /v """') do (
set "_line=%%B"
if defined _line (
if "%_duplicates%"=="TRUE" (
set "_unconverted=!_line!"
set "_converted=!_line:"=""!"
FIND "!_converted!" %TEMP%.\TEMP.DAT > nul
if errorlevel==1 (
>> %TEMP%.\TEMP.DAT echo !_unconverted!
)
)
) ELSE (
echo(>> %TEMP%.\TEMP.DAT
)
)
goto :eof
:SHOWINTELL
#echo A|move %TEMP%.\TEMP.DAT doubleFree.txt
start doubleFree.txt
goto :eof
Input: copybuffer.txt
this test 'data' may have a path C:\Users\Documents\30% full.txt
this test 'data' may have a path C:\Users\Documents\30% full.txt
this test 'data' may have duplicates
this test 'data' may have duplicates
this test 'data' may drive "YOU NUTS"
this test 'data' may drive "YOU NUTS"
this test 'data' may drive "YOU NUTS"
this test 'data' may drive "YOU NUTS"
this test 'data' may drive "YOU NUTS"
this test 'data' may drive "YOU NUTS"
this test 'data' may have Blank Lines
this test 'data' may have Blank Lines
this test 'data' may have "Double Quoted text" in the middle of the string
this test 'data' may have "Double Quoted text" in and middle of and string
this test 'data' may have "Trouble with the find" command
this test 'data' may have "Trouble with and find" command
this test 'data' may drive "YOU NUTS"
this test 'data' may drive "YOU NUTS"
Actual Output: doubleFree.txt (Note: last two lines are NOT duplicates)
this test 'data' may have a path C:\Users\Documents\30% full.txt
this test 'data' may have duplicates
this test 'data' may drive "YOU NUTS"
this test 'data' may have Blank Lines
this test 'data' may have "Double Quoted text" in the middle of the string
this test 'data' may have "Double Quoted text" in and middle of and string
this test 'data' may have "Trouble with the find" command
this test 'data' may have "Trouble with and find" command
the main problem seems to be the expansion of special characters, like quotation marks.
You could avoid this by using the delayed expansion, so special characters are ignored.
(Not perfect here, but nearly, there are only problems with exclamation marks and carets)
The next problem is to search strings with quotation marks with the find command.
You have to double them.
#echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL EnABLEDELAYEDEXPANSION
REM -- Prepare the Prompt for easy debugging -- restore with prompt=$p$g
prompt=$g
rem The finished program will remove duplicates lines
:START
set "_duplicates=TRUE"
set "_infile=copybuffer.txt"
set "_oldstr=the"
set "_newstr=and"
call :BATCHSUBSTITUTE %_infile% %_oldstr% %_newstr%
pause
goto :SHOWINTELL
goto :eof
:BATCHSUBSTITUTE
type nul> %TEMP%.\TEMP.DAT
type nul> %TEMP%.\TEMP2.DAT
if "%~2"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %1|find /n /v """') do (
set "_line=%%B"
if defined _line (
if "%_duplicates%"=="TRUE" (
set "_unconverted=!_line!"
set "_converted=!_line:"=""!"
FIND "!_converted!" %TEMP%.\TEMP.DAT > nul
if errorlevel==1 (
>> %TEMP%.\TEMP.DAT echo !_unconverted!
)
call set "_converted=%%_line:"=#%%"
)
) ELSE (
echo(>> %TEMP%.\TEMP.DAT
)
)
goto :eof
:SHOWINTELL
#echo A|move %TEMP%.\TEMP.DAT doubleFree.txt
start doubleFree.txt
goto :eof
Use a good tool for file processing. If you have the luxury to download stuff, you can try gawk for windows .
C:\test> gawk "!a[$0]++ && $0~/\042|\047/|| !NF" file
this test 'data' may have a path C:\Users\Documents\30% full.txt
this test 'data' may have duplicates
this test 'data' may drive "YOU NUTS"
this test 'data' may have Blank Lines
this test 'data' may have "Double Quoted text" in the middle of the string
this test 'data' may have "Double Quoted text" in and middle of and string
this test 'data' may have "Trouble with the find" command
this test 'data' may have "Trouble with and find" command
this test 'data' may drive "YOU NUTS"
If not, native languages like vbscript is still better at batch to do such stuff.
strFile= WScript.Arguments(0)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set d = CreateObject("Scripting.Dictionary")
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine=objFile.ReadLine
If Not d.Exists(strLine) Then
d.Add strLine, 1
End If
Loop
objFile.Close
For Each strkey In d.Keys
WScript.Echo strkey ', d.Item(strkey)
Next
Usage:
C:\test>cscript //nologo myscript.vbs file