Couple Variable questions in Fsearch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sfournier
Posts: 43
Joined: 21 May 2013 12:38

Couple Variable questions in Fsearch script

#1 Post by sfournier » 14 Jun 2013 12:26

Hello all,
Currently I'm using the below script to take my file "Test2.txt" and separate it into 2 files based on the "tabs.txt" file.
I am hiding a roadblock trying to get some variables to work;
1 - have the script run against ANY file in a folder, ie "M:\pha\test\*.txt"
2 - move the original file to a folder "M:\pha\test\archive"
3 - add the date and time to the name of the 2 New files
4 - move the 2 new files to separate folders

Any help would be greatly appreciated. It's been a while since I've created batch files and feel out of my depth at times. Thank you for sharing your expertise.

Code: Select all

@echo off&setlocal
set "ffile=TEST2.txt"
set "fsearch=Narc_tabs.txt"

set "ftemp=%temp%\%random%.tmp"
(for /f usebackq %%i in ("%fsearch%") do echo( %%i )>"%ftemp%"
findstr /lg:"%ftemp%" "%ffile%">"%file%Narc.txt"
findstr /lvg:"%ftemp%" "%ffile%">"%file%Pacmed.txt"
del "%ftemp%"
type "%file%.new?"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Couple Variable questions in Fsearch script

#2 Post by foxidrive » 15 Jun 2013 03:39

You have an extra ( before the for command.

You seem to be taking the last line of the Narc_tabs.txt file - is that what is meant to happen?

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#3 Post by sfournier » 17 Jun 2013 09:19

the extra ( is the only way the script seems to work. If i remove it, it just copies Test2.txt into the new Pacmed file instead of separating out the items in the Narc_tabs.txt file.

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#4 Post by sfournier » 17 Jun 2013 12:30

Update on my roadblocks.
1 - have the script run against ANY file in a folder, ie "M:\pha\test\*.txt"
Still unable to have it find any .txt file to use, remains static
2 - move the original file to a folder "M:\pha\test\archive"
Original file not moving.
3 - add the date and time to the name of the 2 New files
Not able to apply a generated name to the files, always Static
4 - move the 2 new files to separate folders
The new files created now copy and move to separate folders.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Couple Variable questions in Fsearch script

#5 Post by foxidrive » 17 Jun 2013 22:11

Describe to us what you want to do and let us devise a method.

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#6 Post by sfournier » 18 Jun 2013 14:26

Ok; The software we use at work creates a file for us to package, we have recently got another machine that will be handling specific items. What i'm trying to do is have the batch file take any .TXT file that is in a folder and compare it to a list to create 2 separate files. I would like to have the new created files to have unique names to remove possible human error when packaging these files.
Then take those new files and move them to another folder, as well copy a back up file. Move the Original file and move that into an archive.
The script above currently does the separation to create 2 files, but looks for just the specific file. But when I try to point it to a folder to pull any file in the folder; it is adding in the folder location to the text. This causes the files to not be read on our machines.
I have been able to code in the Move and Copy of the new separate files without any issues, so that is now working. So should be able to move the original file as well.

I'm sorry if that is running together; 9000000 things to do at one time here.
If that does not make sense let me know.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Couple Variable questions in Fsearch script

#7 Post by foxidrive » 19 Jun 2013 03:22

This may help. Your description is not totally clear.

Code: Select all

@echo off
set "fsearch=c:\source folder\Narc_tabs.txt"
set "destination=c:\target\folder"
set "backup=c:\backup"

:loop
set filenum=%random%
if exist "%destination%\%filenum%Narc.txt" goto :loop
if exist "%destination%\%filenum%Pacmed.txt" goto :loop

pushd "c:\folder\with\txt files"

for /f "delims=" %%a in ('dir *.txt /b /a-d') do (

echo processing "%%a"

:: what does this line do that you can't do with %fsearch%
(for /f usebackq %%i in ("%fsearch%") do echo( %%i )>"%ftemp%"

findstr /lg:"%ftemp%" "%%a">"%destination%\%filenum%Narc.txt"
findstr /lvg:"%ftemp%" "%%a">"%destination%\%filennum%Pacmed.txt"
del "%ftemp%"

copy "%destination%\%filenum%Narc.txt" "%backup%"
copy "%destination%\%filennum%Pacmed.txt" "%backup%"

move "%%a" "%backup%"
)
popd

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#8 Post by sfournier » 25 Jun 2013 15:35

Thank you for the response, what you put should be working, but i'm having issues. I do not know if my brain just isn't grasping this correctly or i'm just being pulled in to many different directions to concentrate on this properly.

But can you see what I'm missing?

Code: Select all

@echo off
set "fsearch=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Data\Narc_tabs.txt"
set "destination=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Original"
set "backup=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Original_Archive"

:loop
set filenum=%random%
if exist "N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Repackaged_Extracts\Narcotics\%filenum%Narc.txt" goto :loop
if exist "N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Repackaged_Extracts\Pacmed\%filenum%Pacmed.txt" goto :loop

pushd "N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Original"

for /f "delims=" %%a in ('dir *.txt /b /a-d') do (

echo processing "%%a"

:: what does this line do that you can't do with %fsearch%
(for /f usebackq %%i in ("%fsearch%") do echo( %%i )>"%ftemp%"

findstr /lg:"%ftemp%" "%%a">"N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Repackaged_Extracts\Narcotics\%filenum%Narc.txt"
findstr /lvg:"%ftemp%" "%%a">"N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Repackaged_Extracts\Pacmed\%filennum%Pacmed.txt"
del "%ftemp%"

copy "N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Repackaged_Extracts\Narcotics\%filenum%Narc.txt\%filenum%Narc.txt" "%backup%"
copy "N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Repackaged_Extracts\Pacmed\%filenum%Pacmed.txt\%filennum%Pacmed.txt" "%backup%"

move "%%a" "%backup%"
)
popd

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Nearly complete.. a bit more help if possible

#9 Post by sfournier » 03 Jul 2013 15:39

Hello all,
I've figured out the scripting required almost completely; save for 1 issue.
when my batch file creates the 2 new text files it is adding the the file name/location to the front of every line of text. What am I missing to have this not happen?
It should appear like this

Code: Select all

ISAAK,AGNETHA                  MN-1E
but appears like this

Code: Select all

TEST3.txt:ISAAK,AGNETHA                  MN-1E


The code I'm using is below.

Code: Select all

setlocal

set "ffile=*.txt"
set "fsearch=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test\Data\Narc_tabs.txt"
set dd=%date% %time%
set "filenum=%dd:~0,2%%dd:~3,2%%dd:~6,4%%dd:~11,2%%dd:~14,2%"
set "filepath=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test"

set "ftemp=%temp%\%random%.tmp"
(for /f usebackq %%i in ("%fsearch%") do echo( %%i )>"%ftemp%"
findstr /lg:"%ftemp%" "%ffile%">"%filenum%Narc.txt"
findstr /lvg:"%ftemp%" "%ffile%">"%filenum%Pacmed.txt"
del "%ftemp%"
type "%file%.new?"

Move "%filepath%\original\*Pacmed.txt" "%filepath%\Repackaged_Extracts\Pacmed\"
Move "%filepath%\original\*Narc.txt" "%filepath%\Repackaged_Extracts\Narcotics\"

Copy "%filepath%\Repackaged_Extracts\Narcotics\%filenum%Narc.txt" "%filepath%\Repackaged_Archive\Narcotics\"
Copy "%filepath%\Repackaged_Extracts\Pacmed\%filenum%Pacmed.txt" "%filepath%\Repackaged_Archive\Pacmed\"

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Couple Variable questions in Fsearch script

#10 Post by Squashman » 03 Jul 2013 16:03

Threads merged.

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#11 Post by sfournier » 03 Jul 2013 16:20

Thank you Squashman

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#12 Post by sfournier » 04 Jul 2013 15:56

What is stumping me, is if I specify the ffile name it does not write in the output files, if it left as the wildcard *, it writes in the output.

I will take any and all input on this, even if you think a completely different code function would work better.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Couple Variable questions in Fsearch script

#13 Post by foxidrive » 04 Jul 2013 18:53

Replace this:

findstr /lg:"%ftemp%" "%ffile%">"%filenum%Narc.txt"
findstr /lvg:"%ftemp%" "%ffile%">"%filenum%Pacmed.txt"

with this:

Code: Select all

for %%z in ("%ffile%") do (
findstr /lg:"%ftemp%" "%%z">>"%filenum%Narc.txt"
findstr /lvg:"%ftemp%" "%%z">>"%filenum%Pacmed.txt"
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Couple Variable questions in Fsearch script

#14 Post by Squashman » 04 Jul 2013 19:32

I am wondering two things.
1) Why are you making a copy of a file to a temp location and then using that as your input?
2)Why are you using a for loop to create the temp file?

sfournier
Posts: 43
Joined: 21 May 2013 12:38

Re: Couple Variable questions in Fsearch script

#15 Post by sfournier » 08 Jul 2013 10:36

Thank you Foxidrive, that works perfectly.

@Squashman - of all the different trials I did of the coding and getting assistance on here, it seems to be the only way it works. If you have any other suggestions I'm gladly willing to test.

Post Reply