Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.
C:\>notepad filename
Find xml / pdf file name. Assign to variable. (need to know what filename to open based on date etc?).Find in filename.xml the actual name (need to know how to distinguish this).Use actual name as the name to copy the filename.pdf to D:\
Option ExplicitDim objFSO,objFile,line,strToFindDim myFiles, srcFolder, dstFileDim pdfBase,xmlBase,iSet objFSO = CreateObject("Scripting.FileSystemObject")srcFolder="c:\temp" 'Server locationstrToFind = "Rep1_test" 'Enter string you want to find'dstFile = "c:\temp\"&strToFind 'this is file to rename toDim pdfStore(),pdfFullStore()i=0For Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "pdf" Then pdfBase = objFSO.GetBaseName(myFiles) ReDim Preserve pdfStore(i) ReDim Preserve pdfFullStore(i) pdfStore(i)=pdfBase pdfFullStore(i)=myFiles i=i+1 End If NextFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "xml" Then xmlBase = objFSO.GetBaseName(myFiles) For i=LBound(pdfStore) To UBound(pdfStore) If pdfStore(i) = xmlBase Then WScript.Echo "found " , xmlBase, pdfStore(i) Set objFile=objFSO.OpenTextFile(myFiles,1) Do Until objFile.AtEndOfLine line=objFile.ReadLine If InStr(1, line, strToFind) >0 Then WScript.Echo "found repl_test ", line WScript.Echo "Renaming pdf ..." objFSO.MoveFile pdfFullStore(i),dstFile&".pdf" End If Loop Set objFile=Nothing End If Next End If Next
strToFind = "Rep1_test" 'Enter string you want to find'dstFile = "c:\temp\"&strToFind 'this is file to rename to
If that doesn't work and / or you'd like a 100% automated batch script I STILL would need to know "How to decide what .xml / .pdf file to perform the actions on, is it based on last modified etc. or would it just be the only one in the folder?".ghostdog74's code, I believe requires manual input each time, as I believe the actual report name in the xml file would change.QuotestrToFind = "Rep1_test" 'Enter string you want to find'dstFile = "c:\temp\"&strToFind 'this is file to rename toIf I'm correct this means you have to know what you're looking for to find it, I believe you would like that process to be automated.Please answer the questions and I'm sure ghostdog will also improve his script if indeed that is required?
Quote from: DeltaSlaya on August 16, 2007, 04:41:03 AMIf that doesn't work and / or you'd like a 100% automated batch script I STILL would need to know "How to decide what .xml / .pdf file to perform the actions on, is it based on last modified etc. or would it just be the only one in the folder?".ghostdog74's code, I believe requires manual input each time, as I believe the actual report name in the xml file would change.QuotestrToFind = "Rep1_test" 'Enter string you want to find'dstFile = "c:\temp\"&strToFind 'this is file to rename toIf I'm correct this means you have to know what you're looking for to find it, I believe you would like that process to be automated.Please answer the questions and I'm sure ghostdog will also improve his script if indeed that is required?YES, I mentioned all that, now if you wouldn't mind please answer the question, cheers.
@echo offcd C:\cls:inputset input=:: Check for xmlsif not exist "*.xml" ( echo No .xml files. pause >nul exit):: Set input to last .xml in dirfor /f "usebackq delims=" %%I in (`dir /b /a:-d /o:-d *.xml`) do ( set input=%%~nI goto output):output:: Grab 'actual' name of pdf from xmlset output=for /f "usebackq tokens=4 delims='" %%I in ("%input%.xml") do ( set output=%%I goto copy):copycopy "%input%.pdf" "D:\%output%.pdf"pause
Option ExplicitOn Error Resume NextDim objFSO,objFile,objRE,colMatches,oMatchesDim myFiles, srcFolder, dstFolder,dstFile,line,strToFind,strFileNameDim pdfBase,xmlBase,i,strFileContentsDim pdfStore(),pdfFullStore() 'define some array to store pathsSet objFSO = CreateObject("Scripting.FileSystemObject")' Create regexp Set objRE = New RegExpobjRE.Global = TrueobjRE.IgnoreCase = FalseobjRE.Pattern = "\[@name=(.*?)\]" 'pattern to search forsrcFolder="c:\temp" 'Server source folder locationdstFolder="c:\temp" 'Destination Folder as desiredi=0 'array counterFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "pdf" Then pdfBase = objFSO.GetBaseName(myFiles) ReDim Preserve pdfStore(i) ReDim Preserve pdfFullStore(i) pdfStore(i)=pdfBase pdfFullStore(i)=myFiles i=i+1 End If NextFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "xml" Then xmlBase = objFSO.GetBaseName(myFiles) For i=LBound(pdfStore) To UBound(pdfStore) If pdfStore(i) = xmlBase Then WScript.Echo "found " , xmlBase, pdfStore(i) Set objFile=objFSO.OpenTextFile(myFiles,1) strFileContents = objFile.ReadAll objFile.Close Set colMatches = objRE.Execute(strFileContents) Set oMatches = colMatches(1) If Len(oMatches) = 0 Then WScript.Echo "@name not found" Else strToFind = Replace(oMatches,"[@name='","") strToFind = Replace(strToFind,"']","") WScript.Echo "The string to replace is ",strToFind dstFile = dstFolder&"\"&strToFind WScript.Echo dstFile WScript.Echo "Renaming pdf ..." objFSO.MoveFile pdfFullStore(i),dstFile&".pdf" End If Set objFile=Nothing Set oMatches=Nothing Set colMatches=Nothing End If Next End If Next
<reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test1']</reportSearchPath>
That should work. Grabs filename (without ext) of newest xml (gives error if none exist) then looks in %input%.xml, using tokens finds the actual name. Then setting this name as the output it copies %input%.pdf to D:\%output%.pdf. I believe that is correct?Input folder has been set as C: in example, please change only the "CD ..." line to go to the INPUT directory.Output folder is D:\.Tell me first what the folders should be but otherwise that worked fine for me with your example xml file and a pdf file with same name.
I am unable to run the VB script. I tried executing it from command line but nothing seems to be happening.
For eg., for a Cognos report "Rep1_test", two files exist in the current location: 1325_123467.xml 1325_123467.pdf
@echo off:: Change "C:\OUTPUT" to output directory with NO "\" at end.set outdir=C:\OUTPUTif not exist "%outdir%" ( echo Output folder not exist. pause >nul exit):: Change "C:\" to input directory with "\" at end.cd C:\clsset input=:: Check for xmlsif not exist "*.xml" ( echo No .xml files. pause >nul exit):: Set input to last modified .xml in dir for /f "usebackq delims=" %%I in (`dir /b /a:-d /o:-d *.xml`) do ( set input=%%~nI goto out):out:: Grab 'actual' name of pdf from xmlset output=for /f "usebackq tokens=4 delims='" %%I in (`findstr /i "reportSearchPath" "%input%.xml"`) do ( set output=%%I goto copy):copyset output=%output::=-%copy "%input%.pdf" "%outdir%\%output%.pdf"pause
Ghostdog, I made the modifications accordingly and only then tried executing the VBS file. But, nothing seems to be happening.
C:\>set input=C:\>if not exist "*.xml" (echo No .xml files. pause 1>nul exit)C:\>for /F "usebackq delims=" %I in (`dir /b /a:-d /o:-d *.xml`) do (set input=%~nI goto out)C:\>(set input=test goto out)C:\>set output=C:\>for /F "usebackq tokens=4 delims='" %I in (`findstr /i "reportSearchPath" "test.xml"`) do (set output=%I goto copy)C:\>(set output=PRV-INT-001:Provider NCPDP Interface Error Report goto copy)C:\>set output=PRV-INT-001-Provider NCPDP Interface Error ReportC:\>copy "test.pdf" "C:\OUTPUT\PRV-INT-001-Provider NCPDP Interface Error Report.pdf" 1 file(s) copied.C:\>pausePress any key to continue . . .
C:\temp>dir /B1325_123467.pdf1325_123467.xml1325_123468.pdf1325_123468.xmlC:\temp>more 1325_123467.xmlline1line2line3 <reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test1]</reportSearchPath> <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of Report_test1']</reportViewSearchPath>line blahline blah blahC:\temp>more 1325_123468.xmldfvasdfssdfsddfgfwfsdfdfhhe45gdfg <reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test2']</reportSearchPath> <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of Report_test1']</reportViewSearchPath>sfsdfsdfsdfsdsdfsfsfddsghfbvsbxbC:\temp>cd \C:\>cd vbscriptC:\vbscript>cscript /nologo moveCognoReports.vbsfound 1325_123467 1325_123467The string to replace is Report_test1c:\temp\Report_test1Renaming pdf ...found 1325_123468 1325_123468The string to replace is Report_test2c:\temp\Report_test2Renaming pdf ...C:\vbscript>cd \tempC:\temp>dir /B1325_123467.xml1325_123468.xmlReport_test1.pdfReport_test2.pdf
but are not fetching the desired result.
Regarding the search criteria of the name, the report name would be found after the second "@name" statement in the xml code. The code is given below:<reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test1']</reportSearchPath> <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of Report_test1']</reportViewSearchPath>
<reportSearchPath>/content/package[@name='Provider']/report[@name='PRV-INT-001:Provider NCPDP Interface Error Report']</reportSearchPath> <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of PRV-INT-001:Provider NCPDP Interface Error Report']</reportViewSearchPath>
You are right, we have to consider the 2nd @name in <reportSearchPath>. And, coming to the Report Name, the actual report name doesn't have &aposin it. Its appearing only when am running the VB script.
Option ExplicitOn Error Resume NextDim objFSO,objFile,objRE,colMatches,oMatchesDim myFiles, srcFolder, dstFolder,dstFile,line,strToFind,strFileNameDim pdfBase,xmlBase,i,strFileContentsDim pdfStore(),pdfFullStore() 'define some array to store pathsSet objFSO = CreateObject("Scripting.FileSystemObject")' Create regexp Set objRE = New RegExpobjRE.Global = TrueobjRE.IgnoreCase = FalseobjRE.Pattern = "\[(.*?)\]"srcFolder="c:\temp" 'Server source folder locationdstFolder="c:\temp" 'Destination Folder as desiredi=0 'array counterFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "pdf" Then pdfBase = objFSO.GetBaseName(myFiles) ReDim Preserve pdfStore(i) ReDim Preserve pdfFullStore(i) pdfStore(i)=pdfBase pdfFullStore(i)=myFiles i=i+1 End If NextFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "xml" Then xmlBase = objFSO.GetBaseName(myFiles) For i=LBound(pdfStore) To UBound(pdfStore) If pdfStore(i) = xmlBase Then WScript.Echo "found " , xmlBase, pdfStore(i) Set objFile=objFSO.OpenTextFile(myFiles,1) Do Until objFile.AtEndOfLine line=objFile.ReadLine If InStr(1,line,"<reportSearchPath>") > 0 Then Set colMatches = objRE.Execute(line) Set oMatches = colMatches(1) If Len(oMatches) = 0 Then WScript.Echo "@name not found" Else strToFind= Replace(oMatches,"[@name='","") strToFind = Replace(strToFind,"']","") strToFind = Replace(strToFind,":","") WScript.Echo "The string to replace is ",strToFind dstFile = strToFind&".pdf" WScript.Echo dstFile WScript.Echo "Renaming pdf ..." objFSO.MoveFile pdfFullStore(i),dstFolder&"\"&dstFile End If End If Loop End If Next End If Next Set objFile=Nothing Set oMatches=Nothing Set colMatches=Nothing
C:\temp>dir /Btest.pdftest.xmlC:\temp>cscript /nologo c:\vbscript\MoveCogNosReports.vbsfound test testThe string to replace is PRV-INT-001Provider NCPDP Interface Error ReportPRV-INT-001Provider NCPDP Interface Error Report.pdfRenaming pdf ...C:\temp>dir /BPRV-INT-001Provider NCPDP Interface Error Report.pdftest.xmlC:\temp>
for /F "usebackq tokens=4 delims='" %%I in (`findstr /i "reportSearchPath" "test.xml"`) do (set output=%%Igoto copy)
C:\Users\DeltaSlaya>FINDSTR /I "reportSearchPath" "test.xml" <reportSearchPath>/content/package[@name='Provider']/report[@name='PRV-INT-001:Provider NCPDP Interface Error Report']</reportSearchPath>C:\Users\DeltaSlaya>
for /F "usebackq tokens=4 delims='" %%I in (`findstr /i "reportSearchPath" "test.xml"`) do (set output=%%I)echo *actual name found:* %output%pause
C:\Users\DeltaSlaya>for /F "usebackq tokens=4 delims='" %I in (`findstr /i "reportSearchPath" "test.xml"`) do (set output=%I )C:\Users\DeltaSlaya>(set output=PRV-INT-001:Provider NCPDP Interface Error Report )C:\Users\DeltaSlaya>echo *actual name found:* PRV-INT-001:Provider NCPDP Interface Error Report*actual name found:* PRV-INT-001:Provider NCPDP Interface Error ReportC:\Users\DeltaSlaya>pausePress any key to continue . . .
Ghostdog, have you tried my script? Does it work for you?
@echo off:: Change "C:\OUTPUT" to output directory with NO "\" at end.set outdir=C:\OUTPUTif not exist "%outdir%" ( echo Output folder not exist. pause >nul exit):: Change "C:\" to input directory with "\" at end.cd C:\set input=:: Check for xmlsif not exist "*.xml" ( echo No .xml files. pause >nul exit):: Set input to last modified .xml in dirfor /f "usebackq delims=" %%I in (`dir /b /a:-d /o:-d "*.xml"`) do ( set input=%%~nI goto out):out:: Grab 'actual' name of pdf from xmlset output=for /f "usebackq tokens=4 delims='" %%I in (`findstr /i "reportSearchPath" "%input%.xml"`) do ( set output=%%I goto copy):copyset output=%output::=-%copy "%input%.pdf" "%outdir%\%output%.pdf"pause
<?xml version="1.0" encoding="utf-8" ?>- <!-- Copyright (C) 2006 Cognos Incorporated. All Rights Reserved. Cognos (R) is a trademark of Cognos Incorporated. -->- <!-- Experimental. Product features described in this file may not be supported in future releases. -->- <outputDescriptor xmlns="http://developer.cognos.com/schema/OutputDescriptor/1.0" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <asOfTime>2007-08-12T13:38:08.093Z</asOfTime> <burstKey /> <contact /> <defaultDescription /> <defaultName>2007-08-12T13:38:09.078Z</defaultName> <fileName>1376_1186940289078.pdf</fileName> <locale>en-us</locale> <owner>rgajendra</owner> <ownerNamespace>COGNOS-DEV Team</ownerNamespace> <parameterValues /> <reportSearchPath>/content/package[@name='Provider']/report[@name='PRV-INT-001:Provider NCPDP Interface Error Report']</reportSearchPath> <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of PRV-INT-001:Provider NCPDP Interface Error Report']</reportViewSearchPath> </outputDescriptor>
C:\temp>dir /Btest.pdftest.xmltest1.pdftest1.xmlC:\temp>cd ..C:\>test.bat 1 file(s) copied.Press any key to continue . . .C:\temp>dir /BPRV-INT-001-Provider NCPDP Interface Error Report 2.pdftest.pdftest.xmltest1.pdftest1.xml
Dim objFSO,objFile,objRE,colMatches,oMatchesDim myFiles, srcFolder, dstFolder,dstFile,line,strToFind,strFileNameDim pdfBase,xmlBase,i,strContentsDim pdfStore(),pdfFullStore() 'define some array to store pathsSet objFSO = CreateObject("Scripting.FileSystemObject")srcFolder="c:\temp" 'Server source folder locationdstFolder="c:\temp" 'Destination Folder as desiredi=0 'array counterFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "pdf" Then pdfBase = objFSO.GetBaseName(myFiles) ReDim Preserve pdfStore(i) ReDim Preserve pdfFullStore(i) pdfStore(i)=pdfBase pdfFullStore(i)=myFiles i=i+1 End If NextFor Each myFiles In objFSO.GetFolder(srcFolder).Files If objFSO.GetExtensionName(myFiles) = "xml" Then xmlBase = objFSO.GetBaseName(myFiles) For i=LBound(pdfStore) To UBound(pdfStore) If pdfStore(i) = xmlBase Then WScript.Echo "found " , xmlBase, pdfStore(i) strToFind = getString(myFiles) dstFile = strToFind&".pdf" objFSO.MoveFile pdfFullStore(i),dstFolder&"\"&dstFile End If Next End If NextFunction getString(theFile) Set objRE = New RegExp objRE.Global = True objRE.IgnoreCase = False objRE.Pattern = "<reportSearchPath>.*/report\[@name='(.*?)'\]</reportSearchPath>" Set objFile=objFSO.OpenTextFile(theFile,1) strContents=objFile.ReadAll Set Matches = objRE.Execute(strContents) For Each match In Matches For Each smatch In match.Submatches result=Replace(smatch,":"," ") Next Next getString = resultEnd Function
C:\vbscript>dir c:\temp /Btest.pdftest.xmltest1.pdftest1.xmlC:\vbscript>cscript /nologo MoveCogNosReports2.vbsfound test testfound test1 test1C:\vbscript>dir c:\temp /BPRV-INT-001 Provider NCPDP Interface Error Report.pdfPRV-INT-002 Provider NCPDP Interface Error Report.pdftest.xmltest1.xml