1.

Solve : Open and Reading the file?

Answer» HI,
I just tried the second line in the batch file. Find the output below:

C:\TEMP>sample.bat

C:\TEMP>for /F "usebackq tokens=4 delims='" %I in (`findstr /i "reportSearchPath
" "test.xml"`) do (set output=%I )

C:\TEMP>echo *actual name found:* %%I
*actual name found:* %%I

C:\TEMP>pause
Press any key to continue . . .

C:\TEMP>

...

Sorry to confuse you but could you run that batch file with the example xml you supplied in the same folder...I ran the batch file with only the second statement in the same folder as the XML file.With the xml file called test.txt? See I don't know why that's not working sorry, logically it should and it does for me.The xml file is test.xml and not test.txt. It has to be saved as ".xml" Am trying with the same, but no result I meant test.xml, sorry. Looks like you'll have to go with the VBS for now, unless someone more expert than me in Batch can figure it out?

Ghostdog, have you tried my script? Does it work for you?Quote from: DeltaSlaya on August 21, 2007, 10:51:46 PM
Ghostdog, have you tried my script? Does it work for you?
no, have not. can u post your latest version of the batch, and the format of the xml you have.Heres the latest code, I believe:

Quote
@echo off

:: Change "C:\OUTPUT" to output directory with NO "\" at end.
set outdir=C:\OUTPUT
if 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 xmls
if 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 xml
set output=

for /f "usebackq tokens=4 delims='" %%I in (`findstr /i "reportSearchPath" "%input%.xml"`) do (
set output=%%I
goto copy
)

:copy
set output=%output::=-%
copy "%input%.pdf" "%outdir%\%output%.pdf"
pause

And the .xml file

Quote
<?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>

You will also need a .pdf file of the same name in the xml's directory. The variables to set are clearly defined.
Also could the OP please try that again, I noticed that it wasn't working because it was looking in extension not the file name?well, i think it does work.
Code: [Select]C:\temp>dir /B
test.pdf
test.xml
test1.pdf
test1.xml

C:\temp>cd ..

C:\>test.bat
1 file(s) copied.
Press any key to continue . . .
C:\temp>dir /B
PRV-INT-001-Provider NCPDP Interface Error Report 2.pdf
test.pdf
test.xml
test1.pdf
test1.xml

however, only for 1 file. Needs extra for loops to iterate multiple xml files(or maybe not, by OP's requirements)..however that's another story.Well he said the most recent, thats what it does, yet for some reason does not do anything for them. I still believe on their end they entered something wrong or changed something. They should try the above, by itself with the right things changed and all the correct files and dirs present.Honestly, if the OP hasn't got my code working then there's no real point anyway. They said it would be run after each time the files have been created.

Anyway, how I would have done it, using skip=. I'm just wondering why does skip=0 not work? It just skips the 0, I guess if skip is defined then you can't perform a command on the first line..Ghostdog,

I ran the script with two files in the src folder. But, its moving only the 1st file and not the second one. Below is the output:

C:\vbscript>vb_batch.bat

C:\vbscript>CD C:\vbscript

C:\vbscript>cscript /nologo vb_script.vbs
found PRV-INT-001 PRV-INT-001
The string to replace is PRV-INT-001-Provider NCPDP Interface Error Report
PRV-INT-001-Provider NCPDP Interface Error Report.pdf
Renaming pdf ...
found PRV-INT-002 PRV-INT-002
C:\vbscript>

here's a modification
Code: [Select]Dim objFSO,objFile,objRE,colMatches,oMatches
Dim myFiles, srcFolder, dstFolder,dstFile,line,strToFind,strFileName
Dim pdfBase,xmlBase,i,strContents
Dim pdfStore(),pdfFullStore() 'define some array to store paths
Set objFSO = CreateObject("Scripting.FileSystemObject")
srcFolder="c:\temp" 'Server source folder location
dstFolder="c:\temp" 'Destination Folder as desired
i=0 'array counter
For 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
Next
For 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
Next

Function 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 = result
End Function

output:
Code: [Select]C:\vbscript>dir c:\temp /B
test.pdf
test.xml
test1.pdf
test1.xml

C:\vbscript>cscript /nologo MoveCogNosReports2.vbs
found test test
found test1 test1

C:\vbscript>dir c:\temp /B
PRV-INT-001 Provider NCPDP Interface Error Report.pdf
PRV-INT-002 Provider NCPDP Interface Error Report.pdf
test.xml
test1.xml

Hey! Tx... but am able to use the previous script. Vl run this script and let u know in case of NE issues


Discussion

No Comment Found