Couple Variable questions in Fsearch script
Moderator: DosItHelp
Re: Couple Variable questions in Fsearch script
Not sure if WSH is locked down, but it is giving me an error.
Line 1
Char 14
Error Expected End of Statement
Cod 500a0401
I deleted line 1 and now get this error.
Line 2
Char 7
Error Syntax Error
code 800a03ea
Line 1
Char 14
Error Expected End of Statement
Cod 500a0401
I deleted line 1 and now get this error.
Line 2
Char 7
Error Syntax Error
code 800a03ea
Re: Couple Variable questions in Fsearch script
It's a batch file, not a native VBS script.
If you save it as a batch file, does it work?
Add this the end to test it:
If you save it as a batch file, does it work?
Add this the end to test it:
Code: Select all
echo %filenum%
pause
Re: Couple Variable questions in Fsearch script
My apologies Foxidrive, I've been looking at so many different options on how to fix this I completely missed that this was a batch file
This appears to work, so far, I will test with another user that had issues but I think we have it.
Thank you again very much

This appears to work, so far, I will test with another user that had issues but I think we have it.
Thank you again very much

Re: Couple Variable questions in Fsearch script
Hi again,
Sorry for bringing up this issue again, we had an issue with a user not realizing the script was ran and this caused a downstream error so the request was to name the separated file based on the contents.
I looked through the forum here and found 3 different coding fragments that should work but have not been successful in adding them to the process.
I have tried all three versions with no success, as previously stated.
This is code I am using currently to set the name based on date and time.
Sorry to ask for help again, but any assistance would be great. There may be more than 1 file in the target folder at 1 time so it still being able to differentiate between those files on the output is required.
Sorry for bringing up this issue again, we had an issue with a user not realizing the script was ran and this caused a downstream error so the request was to name the separated file based on the contents.
I looked through the forum here and found 3 different coding fragments that should work but have not been successful in adding them to the process.
Code: Select all
@echo off&setlocal
cd %filepath%
for /f "delims=" %%i in ('dir /a-d/b *.txt') do (
set "nname="
set "fname=%%~i"
for /f "usebackq=" %%f in ("%%~i") do if not defined nname set "nname=%%f"
setlocal enabledelayedexpansion
set "nname=!nname:~32,2!"
echo rename "!fname!" "!nname!.txt"
endlocal
@echo off
setlocal
cd %filepath%
for /F "delims=" %%a in ('dir /A-D /B *.txt') do (
for /F "tokens=1* delims=:" %%b in (
'findstr /N "^" "%%a" 2^>NUL ^| findstr "^3:" 2^>NUL ^| (set /P @^=^& set @ ^)'
) do set "line1=%%c"
setlocal EnableDelayedExpansion
ECHO ren "%%a" "!line1:~32,2!%%~Xa"
endlocal
)
This batch file do what you need:
@echo off
setlocal enabledelayedexpansion
for %%f in (*.) do (
set /p firstline=< %%f
set thedate=!firstline:~32,2!
set name=!thedate:/=-!
echo ren %%f !name!.txt
)
I have tried all three versions with no success, as previously stated.
This is code I am using currently to set the name based on date and time.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "filepath=M:\Pharmacy\PACMED\test"
set "ffile=%filepath%\Meditech extracts\*.meds"
set "fsearch=%filepath%\Pacmed Sep Script\Data\Narc_tabs.txt"
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%"
set "num=0"
for %%z in ("%ffile%") do (
set /a num=num+1
findstr /lg:"%fsearch%" "%%z">>"%filepath%\Meditech extracts\Narcotics\%filenum%-!num!-Narc.txt"
findstr /lvg:"%fsearch%" "%%z">>"%filepath%\Meditech extracts\Pacmed\%filenum%-!num!-Pacmed.txt"
Copy "%filepath%\Meditech extracts\Pacmed\*.txt" "%filepath%\Meditech extracts\Archives\Pacmed\"
)
Copy "%filepath%\Meditech extracts\Narcotics\*.txt" "%filepath%\Meditech extracts\Archives\Narcotics\"
)
Sorry to ask for help again, but any assistance would be great. There may be more than 1 file in the target folder at 1 time so it still being able to differentiate between those files on the output is required.
Re: Couple Variable questions in Fsearch script
Does it run more than once a day?
Shows us the first line of the file in question please.
Shows us the first line of the file in question please.
Re: Couple Variable questions in Fsearch script
Yeah, staff are running this ~15 times a day; most of the time it is a single file being separated, but there are a few times during the day where there are multiple files in the folder.
Re: Couple Variable questions in Fsearch script
Ta. I gather you want to add something from the files to the filename.
Shows us the first line of one of the files in question please, and outline which bit needs to be extracted to the filename.
Mask any text that is private but don't change any special characters,
Shows us the first line of one of the files in question please, and outline which bit needs to be extracted to the filename.
Mask any text that is private but don't change any special characters,
Re: Couple Variable questions in Fsearch script
Code: Select all
TEST,PATIENT 1 [i]TE[/i]ST-1 ER1BE-101-A ER02541119 ERH 005024 20140113 0800 1 E0289075 TEST-1
The part in italics is that I was looking at pulling to change the name of the file with. It starts at character 32 and is 2 characters long.
Thanks
Re: Couple Variable questions in Fsearch script
Test this and when you run it, it should show on the screen something like name is set to "TE"
followed by press any key to continue.
Up to this stage it will not change anything.
You can close the window with the mouse at that stage, and if it looks ok then remove the following two lines and you can run it for real.
echo name is set to "!name!"
pause
The "TE" is added between the datetime and number. Change that to suit yourself.
Two other things to note: the code you show has closing brackets at the bottom and they look like there is one too many and that they are in the wrong place.
I've changed it so the two copy commands run after all the TXT files have been created.
followed by press any key to continue.
Up to this stage it will not change anything.
You can close the window with the mouse at that stage, and if it looks ok then remove the following two lines and you can run it for real.
echo name is set to "!name!"
pause
The "TE" is added between the datetime and number. Change that to suit yourself.
Two other things to note: the code you show has closing brackets at the bottom and they look like there is one too many and that they are in the wrong place.
I've changed it so the two copy commands run after all the TXT files have been created.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "filepath=M:\Pharmacy\PACMED\test"
set "ffile=%filepath%\Meditech extracts\*.meds"
set "fsearch=%filepath%\Pacmed Sep Script\Data\Narc_tabs.txt"
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%"
set "num=0"
for %%z in ("%ffile%") do (
set "name="
set /p "name=" < "%%z"
set "name=!name:~31,2!
echo name is set to "!name!"
pause
set /a num=num+1
findstr /lg:"%fsearch%" "%%z">>"%filepath%\Meditech extracts\Narcotics\%filenum%-!name!-!num!-Narc.txt"
findstr /lvg:"%fsearch%" "%%z">>"%filepath%\Meditech extracts\Pacmed\%filenum%-!name!-!num!-Pacmed.txt"
)
Copy "%filepath%\Meditech extracts\Pacmed\*.txt" "%filepath%\Meditech extracts\Archives\Pacmed\"
Copy "%filepath%\Meditech extracts\Narcotics\*.txt" "%filepath%\Meditech extracts\Archives\Narcotics\"
Re: Couple Variable questions in Fsearch script
Thanks Foxidrive, it is working perfectly. I knew it was something simple I was over looking.