Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#16
Post
by sfournier » 08 Jul 2013 16:57
I actually discovered something that is going to be an issue. The completed script works perfectly, except if there are 2 files in the same folder, this combines the files in the output.
Are there any variables that will cause it to keep the files separate?
Code: Select all
setlocal
set "filepath=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test"
set "ffile=%filepath%\original\*.txt"
set "fsearch=%filepath%\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 "ftemp=%temp%\%random%.tmp"
(for /f usebackq %%i in ("%fsearch%") do echo( %%i )>"%ftemp%"
for %%z in ("%ffile%") do (
findstr /lg:"%ftemp%" "%%z">>"%filenum%Narc.txt"
findstr /lvg:"%ftemp%" "%%z">>"%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\"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#17
Post
by foxidrive » 09 Jul 2013 02:21
Try this - and check if that last type command is needed.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "filepath=N:\Clinical Support\Pharmacy\PDDC Staff\Automated Assistant\Pacmed\pm sep test"
set "ffile=%filepath%\original\*.txt"
set "fsearch=%filepath%\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 "num=0"
for %%z in ("%ffile%") do (
set /a num=num+1
findstr /lg:"%fsearch%" "%%z">>"%filenum%-!num!-Narc.txt"
findstr /lvg:"%fsearch%" "%%z">>"%filenum%-!num!-Pacmed.txt"
Move "%filepath%\original\*Pacmed.txt" "%filepath%\Repackaged_Extracts\Pacmed\"
Move "%filepath%\original\*Narc.txt" "%filepath%\Repackaged_Extracts\Narcotics\"
Copy "%filepath%\Repackaged_Extracts\Narcotics\%filenum%-!num!-Narc.txt" "%filepath%\Repackaged_Archive\Narcotics\"
Copy "%filepath%\Repackaged_Extracts\Pacmed\%filenum%-!num!-Pacmed.txt" "%filepath%\Repackaged_Archive\Pacmed\"
)
type "%file%.new?"
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#18
Post
by sfournier » 09 Jul 2013 09:19
Thank you again Foxidrive. I feel bad for asking for so much help, but it has been so long since I have had to code in Dos I've forgotten most of it. It is slowly coming back.
This worked perfectly and it works without the last line of code.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#19
Post
by foxidrive » 10 Jul 2013 03:24
sfournier wrote:Thank you again Foxidrive. I feel bad for asking for so much help, but it has been so long since I have had to code in Dos
You're welcome. Many of us like having the chance to help with some script - providing good details and exact names and strings and you'll usually get more appropriate code.
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#20
Post
by sfournier » 10 Jul 2013 10:56
Hi again,
I proposed the working script to my managers and they had another suggestion that I had not thought of.
Is it possible to create another search, done initially before the separation, to exclude files if they are in another.
For example
set "fsearch=%filepath\data\excludesites.txt"
have any files that contain the information in this file stop processing at this point?
Or should I create a new batch to run first and build in a IF loop?
I hope that makes sense. I'm on the Tcon with the managers and typing as I go.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#21
Post
by foxidrive » 10 Jul 2013 17:16
sfournier wrote:Hi again,
I proposed the working script to my managers and they had another suggestion that I had not thought of.
Is it possible to create another search, done initially before the separation, to exclude files if they are in another.
For example
set "fsearch=%filepath\data\excludesites.txt"
have any files that contain the information in this file stop processing at this point?
I can't understand what you want to do, apart from a search that exits the batch file on some condition. What does the search do? What is being searched for?
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#22
Post
by sfournier » 11 Jul 2013 10:05
Sorry Foxidrive, I meant to delete both posts. It was a search that exits the batch file, but upon more investigation on my part, it should probably not be needed. I'm trying to keep the file as clean/simple as possible in case someone else has to maintain it in the future.
Sorry for the confusion and thank you again for all the help.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#23
Post
by foxidrive » 12 Jul 2013 03:46
s'ok. If you want to expand it in the future then maybe it can be revisited then. It is always easier to change the code while it's fresh in our minds though.
If you want to search some file, for example, and if it finds a text string then exit the batch file: that is a pretty simple addition - given the file and location and text to search for.
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#24
Post
by sfournier » 30 Oct 2013 11:11
Hello all,
I'm having an issue with the script that I'm really having issues troubleshooting.
There are multiple people required to run the script, however not all users are able to have the script run through completely. What is occuring is the file is failing to write the 2 new files created during the fsearch; these users are able to do move and copy functionality within the folder.
I have looked at user privileges and all users have the same privileges within the folders, this is causing me a very big headache and you all have been extremely helpful thus far so I am posing the question to you.
Steve
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#25
Post
by foxidrive » 30 Oct 2013 11:38
Is the source drive on a network and do the users have permission to access it?
Something a little more mundane: check what this command shows on various users - ones that work and ones that fail:
The users may have changed their date and time format.
If that is the case then a more robust method can be used to acquire the date and time.
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#26
Post
by sfournier » 30 Oct 2013 13:04
Thanks Foxidrive, I will test the date time.
The source file is on a network drive that all the users have access to.
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#27
Post
by sfournier » 30 Oct 2013 16:30
So I did test it and was able to determine the date is displaying differently for the users that have issues.
The ones that cannot run it have the day added to the date so it displays as "WED 10/30/2013" instead of "30/10/2013"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#28
Post
by foxidrive » 31 Oct 2013 00:48
If you use XP Pro and higher then replace this:
Code: Select all
set dd=%date% %time%
set "filenum=%dd:~0,2%%dd:~3,2%%dd:~6,4%%dd:~11,2%%dd:~14,2%"
with this:
Code: Select all
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "filenum=%DD%%MM%%YYYY%%HH%%Min%"
The format there is DDMMYYYYHHMM and can be adjusted as you prefer it.
It uses WMIC and provides a very robust method of getting a timestamp.
-
sfournier
- Posts: 43
- Joined: 21 May 2013 12:38
#29
Post
by sfournier » 31 Oct 2013 09:51
Thank you Foxidrive, we are having an issue with the WMIC as it says only admin can run this.

this is just ridiculous something as simple as a date can cause this.
i have a vbs that can set the date but it I'm having issue formatting the time into it.
Code: Select all
Function Pad(Value, PadCharacter, Length)
Pad = Right(String(Length,PadCharacter) & Value, Length)
End Function
Dim Today
Today = Date
WScript.Echo Pad(Day(Today), "0", 2) & Pad(Month(Today), "0", 2) & Pad(Year(Today), "0", 4)
And then I use the following code to add the time but it is in the wrong format.
Code: Select all
FOR /F %%i IN ('cscript "%~dp0yyyymmdd.vbs" //Nologo') do SET MYDATE=%%i
set filenum=%mydate%%time%
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#30
Post
by foxidrive » 31 Oct 2013 16:28
Here is batch code to run a VBS script that sets the date string. Try it and see if it works at your company, as they might lock down WSH too.
Code: Select all
:: date time using WSH/VBS
set TmpFile="%temp%.\tmp.vbs"
echo> %TmpFile% n=Now
echo>>%TmpFile% With WScript
echo>>%TmpFile% .Echo "set m1=" + monthname(month(n), true)
echo>>%TmpFile% .Echo "set m2=" + monthname(month(n), false)
echo>>%TmpFile% .Echo "set woy=" + CStr(datepart("ww", n))
echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
echo>>%TmpFile% End With
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del "%temp%.\tmp.bat"
del %TmpFile%
set TmpFile=
set "filenum=%day%%month%%year%%hour%%min%%sec%"