cQFILE ;File manipulation Caché ;cQFILE; Q ; OR(R) ; Open file R for read ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext N ER,T S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 I $$TST(R) Q 3 ; file does not exist O R:"R":3 S T=$T S:'T ER=2 I T U R ORZ U 0 Q ER ; ORF(R,L) ; Open file R for read with fixed length L ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; L is the length of the records you want to read N ER,T S $ZT="ERR",R=$G(R),L=+$G(L),ER=0 I '$L(R) Q 1 I $$TST(R) Q 3 ; file does not exist O R:("RF":L):3 S T=$T S:'T ER=2 I T U R ORFZ U 0 Q ER ; OW(R) ; Open file R for write ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext N ER,T S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 O R:"WNS":3 S T=$T S:'T ER=2 I T U R OWZ U 0 Q ER ; OA(R) ; Open file R for append ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext N ER,T S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 O R:"WAS":3 S T=$T S:'T ER=2 I T U R OAZ U 0 Q ER ; CF(R) ; Close file R ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext N ER S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 C R CFZ U 0 Q ER ; RF(R,K) ; Read from file R into K ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K has to be passed by reference, i.e. S ER=$$RF(file,.A) puts ;; the error in ER and returns the string read into A ;; If ER=99 = End of File the value returned will be the empty string N ER S $ZT="ERR",R=$G(R),ER=0,K="" I '$L(R) Q 1 I $$TST(R) Q 3 ; file does not exist U R R K RFZ U 0 Q ER ; RFF(R,K,L) ; Read from file R into K with fixed length L ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K has to be passed by reference, i.e. S ER=$$RF(file,.A) puts ;; the error in ER and returns the string read into A ;; L is the length of the records you want to read ;; If ER=99 = End of File the value returned can contain data if the ;; the size of the file is not a multiple of the recordlength you ;; read. You should check the variable for a non-empty string. N ER S $ZT="ERR",R=$G(R),L=+$G(L),ER=0,K="" I '$L(R) Q 1 I $$TST(R) Q 3 ; file does not exist U R R K RFFZ U 0 Q ER ; WF(R,K) ; Write to file R string K ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K contains the data you want to write INCLUDING record separators ;; i.e. S ER=$$WF(file,data_$c(13,10)) is equal to 'W data,!' N ER S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 I $$TST(R) Q 3 ; file does not exist U R W K WFZ U 0 Q ER ; RD(R) ; Read and display file R S ER=0,R=$G(R) I '$L(R) S ER=1 G RDZ F S ER=$$RF(R,.K) Q:ER I 'ER W !,K RDZ U 0 Q ER ; RS(R,S,K) ; Read file R and search for S and return result in K S ER=0,R=$G(R) I '$L(R) S ER=1 G RSZ S S=$$UC($G(S)) I '$L(S) S ER=2 G RSZ F S K="",ER=$$RF(R,.K) Q:ER I 'ER,$$UC(K)[S Q RSZ U 0 Q ER ; YZ Q ; FS(R,K) ; Get size of File R into K ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K has to be passed by reference, i.e. S ER=$$FS(file,.A) puts ;; the error in ER and returns the file size into A N ER S $ZT="ERR",R=$G(R) I $$TST(R) Q 1 ; file does not exist S ER=0,K=$ZU(140,1,R) I K<0 S ER=2 FSZ Q ER ; FMD(R,K) ; Get file modificaton date of File R into K ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K has to be passed by reference, i.e. S ER=$$FS(file,.A) puts ;; the error in ER and returns the date into A N ER S $ZT="ERR",R=$G(R) I $$TST(R) Q 1 ; file does not exist S K=$ZU(140,2,R),ER=0 I K<0 S ER=2 FMDZ Q ER ; FCD(R,K) ; Get file creation date of File R into K ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K has to be passed by reference, i.e. S ER=$$FS(file,.A) puts ;; the error in ER and returns the date into A N ER S $ZT="ERR",R=$G(R) I $$TST(R) Q 1 ; file does not exist S K=$ZU(140,3,R),ER=0 I K<0 S ER=2 FCDZ Q ER ; TST(R) ; Check if file or dir R exists ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; if R ends with a '\' we asume it's a directory. ;; BEWARE: you cannot test for the existance of a share !! ;; i.e. if \\system\share exists, you must either test ;; - a file: S ER=$$TST("\\system\share\file.ext") ;; or ;; - a directory: S ER=$$TST("\\system\share\dir\") N ER S $ZT="ERR",R=$TR($G(R),"/","\"),ER=2 I '$L(R) Q 1 I $L(R,"\")>2,$E(R,$L(R))="\" S R=$E(R,1,$L(R)-1) S ER=$ZU(140,4,R) I ER'=0 S ER=2 TSTZ Q ER ; DEL(R) ; Delete file R ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; if the file does not exist, you get 'succesfully' deleted N ER S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 I $$TST(R) Q 0 ; file does not exist S ER=$ZU(140,5,R) S:ER=-2 ER=0 DELZ Q ER ; REN(R,S) ; Rename file R to S ;; R & S can be x:\dir\file.ext or \\system\share\dir\file.ext N ER S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 S S=$G(S),ER=0 I '$L(S) S ER=2 Q 3 I $$TST(R) Q 4 ; file does not exist S ER=$ZU(140,6,R,S) RENZ Q ER ; FAT(R,K) ; Get attributes for file R into K ;; R can be x:\dir\file.ext or \\system\share\dir\file.ext ;; K has to be passed by reference, i.e. S ER=$$FS(file,.A) puts ;; the error in ER and returns the date into A N ER S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 I $$TST(R) Q 2 ; file does not exist S K=$ZU(140,7,R),ER=0 I K<0 S ER=3 FATZ Q ER ; CRED(R) ; Create directory R ;; R can be x:\dir or \\system\share\dir ;; if the directory already exists, you get 'succesfully' created N ER,I S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 I $E(R,$L(R))="\" S R=$E(R,1,$L(R)-1) F I=$E(R,1,2)="\\"*3+2:1:$L(R,"\") S ER=$$CREDA($P(R,"\",1,I)) Q:ER Q ER CREDA(R) I '$$TST(R) Q 0 ; directory does already exist S ER=$ZU(140,9,R) S:ER=-183 ER=0 CREDZ Q ER ; DELD(R) ; Delete directory R ;; R can be x:\dir or \\system\share\dir ;; if the directory does not exist, you get 'succesfully' deleted N ER S $ZT="ERR",R=$G(R),ER=0 I '$L(R) Q 1 I $$TST(R) Q 0 ; directory does not exist S ER=$ZU(140,10,R) S:ER=-2 ER=0 DELDZ Q ER ; CPFD ; Display file cache.cpf S R=$P($ZU(86),"*"),ER=$$OR(R) I ER U 0 W !,"File ",R," not found",! Q D RD(R),CF(R) Q ; KEYD ; Display file cache.key S R=$$KEY,ER=$$OR(R) I ER U 0 W !,"File ",R," not found",! Q D RD(R),CF(R) KEYDZ Q ; KEY() N R S R=$P($ZU(86),"*") Q $P(R,"\",1,$L(R,"\")-1)_"\Mgr\Cache.key" ; CP(S,T,debug) ; Copy source file S to destination file T N ER,I,INFO,LANG,OS,W2K S S=$G(S),T=$G(T) I '$L(S) S ER=15_" = source is null string" G CPZ I '$L(T) S ER=16_" = destination is null string" G CPZ S ER=$$CRED($P(T,"\",1,$L(T,"\")-1)),INFO=$$INFO^cQ14 S W2K=$$W2K,LANG=$$LANG() S I=$S(W2K:"",1:"echo "_$S(LANG="FR":"o",LANG="NL":"j",1:"y")_"| ")_"copy """_S_""" """_T_""" /v "_$S(W2K:"/y",1:"") S ER=$$JW(I_" >"_INFO_$S($G(debug):".txt",1:"")) ;copy file I 'ER S ER=ER_" = no errors" CPZ Q ER ; RCP(S,T,debug,X) ; RoboCopy source directory S to destination directory T, X = exclude files N ER,I,INFO,J,K,LANG,OS,W2K S S=$TR($G(S),"/","\"),T=$TR($G(T),"/","\"),debug=+$G(debug),X=$G(X) F Q:$E(S,$L(S))'="\" S S=$E(S,1,$L(S)-1) I '$L(S) S ER=15_" = source is null string" G XCPZ I '$L(T) S ER=16_" = destination is null string" G XCPZ I $E(T,$L(T))'="\" S T=T_"\" S ER=$$CRED(T),INFO=$$INFO^cQ14 S I=$E($ZU(86))_":\DataM\Tools\RoboCopy.exe "_S_" "_T_" /mir /zb /copyall /r:3 /w:3 /ns /nc /np /log:"_$E($ZU(86))_":\DataM\Temp\bu_log.txt" I $L(X) S I=I_" /xf "_X I $G(debug) U 0 W !,$G(I),! S ER=$$JW("echo "_I_" >"_INFO) S ER=$$JW(I) ;xecute command I 'ER S ER=ER_" = no errors" RCPZ Q ER ; XCP(S,T,debug,X) ; XCopy source directory S to destination directory T, X = exclude files N ER,I,INFO,J,K,LANG,OS,W2K S S=$TR($G(S),"/","\"),T=$TR($G(T),"/","\"),debug=+$G(debug),X=$G(X) F Q:$E(S,$L(S))'="\" S S=$E(S,1,$L(S)-1) I '$L(S) S ER=15_" = source is null string" G XCPZ I '$L(T) S ER=16_" = destination is null string" G XCPZ I $E(T,$L(T))'="\" S T=T_"\" S ER=$$CRED(T),INFO=$$INFO^cQ14,W2K=$$W2K,LANG=$$LANG() S I=$S(W2K:"",1:"echo "_$S(LANG="FR":"o",LANG="NL":"j",1:"y")_"| ")_"xcopy """_S_""" """_T_""""_$S(debug:"",1:" /q")_" /h /r /s /e "_$S(W2K:"/y",1:"") I $L(X) D . S J=$$INFO^cQ14("txt"),ER=$$OW(J) Q:ER . F K=1:1:$L(X,",") S ER=$$WF(J,$P(X,",",K)_$C(13,10)) Q:ER . Q:ER S ER=$$CF(J) . S I=I_" /EXCLUDE:"_J I $G(debug) U 0 W !,$G(I),! S ER=$$JW("echo "_I_" >"_INFO) S ER=$$JW(I_" >>"_INFO) ;xecute command I 'ER S ER=ER_" = no errors" XCPZ Q ER ; DFID(S) ; Delete all files in directory S N ER,I,INFO,LANG,OS,W2K S S=$G(S) I '$L(S) S ER=15_" = no directory given" G DFIDZ I $E(S,$L(S))'="\" S S=S_"\" S INFO=$$INFO^cQ14,W2K=$$W2K,LANG=$$LANG() S I=$S(W2K:"",1:"echo "_$S(LANG="FR":"o",LANG="NL":"j",1:"y")_"| ")_"del """_S_"""*.* "_$S(W2K:"/q",1:"") S ER=$$JW(I_" >"_INFO) ;copy file I 'ER S ER=ER_" = no errors" DFIDZ Q ER ; FSEQ(S) ; find next available number for file S_JJJJMMDD_x.EXT N I,X F I=1:1:999 S X=$P(S,".",1,$L(S,".")-1)_"_"_$ZD($H,8)_"_"_$E(1000+I,2,4)_$S($L($P(S,".",$L(S,"."))):"."_$P(S,".",$L(S,".")),1:"") Q:$$TST(X) FSEQZ Q X ; OS() ; Get OS version N INFO,ER S INFO=$$INFO^cQ14 S OS="",ER=$$JW("ver >"_INFO) I 'ER S ER=$$OR(INFO)+$$RS(INFO,"Windows",.OS)+$$CF(INFO) Q OS ; LANG() ; Get OS language N INFO,L,ER S INFO=$$INFO^cQ14 S L="UK",ER=$$JW("dir c:\*.* >"_INFO) I 'ER D . S ER=$$OR(INFO)+$$RS(INFO,"File(s)",.L)+$$CF(INFO) I $L(L) S L="UK" Q . S ER=$$OR(INFO)+$$RS(INFO,"Bestand(en)",.L)+$$CF(INFO) I $L(L) S L="NL" Q . S ER=$$OR(INFO)+$$RS(INFO,"Fichier(s)",.L)+$$CF(INFO) I $L(L) S L="FR" Q LANGZ Q L ; W2K() S OS=$$OS I $$UC(OS)["WINDOWS 2000" Q 1 ;W2K I $$UC(OS)[" 5." Q 1 ;WXP, W2K3 I $$UC(OS)[" 6." Q 1 ;Vista, W2008, W2008R2 I $$UC(OS)[" 7." Q 1 ;Windows 7 W2KZ Q 0 ; JW(R) S R=$G(R) I $L(R) Q $ZF(-1,R) JWZ Q 1 ; JNW(R) S R=$G(R) I $L(R) Q $ZF(-2,R) JNWZ Q 1 ; ERR U 0 S ER=0 I $L(R) C R I $ZE["" S ER=99 I $ZE["" S ER="11,"_$ZE I $ZE["" S ER=12_","_$ZE I $ZE["" S ER=13_","_$ZE I $ZE["" S ER=14_","_$ZE ERRZ Q ER ; UC(K) Q $TR(K,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ") ; ZZ ; 17.01.2012 - 15:58 * Cache-r6.4.9