Page 1 of 1

HELP!List the same prefix files in a txt separated by commas

Posted: 01 Sep 2010 12:08
by thor28
I have some pdf files in a dir with a code in the name and I need to find the same code and export to a txt file separated by commas. Like this:009900(2).pdf, 009900(3).pdf, 012345(2).pdf, 012345(3).pdf, 012345(4).pdf >> 012345.csv (This file will contain the text: 012345(2).pdf, 012345(3).pdf, 012345(4).pdf) and 009900.csv (This file will contain exactly :009900(2).pdf, 009900(3).pdf)

Please I need so much this bat. :cry: :cry: :cry:

Re: HELP!List the same prefix files in a txt separated by co

Posted: 01 Sep 2010 17:48
by orange_batch
So many repeat question types lately, hmm...

Puts all 009900*.pdf filenames CSV'ed into 009900.csv.
Puts all 012345*.pdf filenames CSV'ed into 012345.csv.

Code: Select all

setlocal enabledelayedexpansion

:: Make sure this ends with \
set "target=C:\...\Target Folder\"

for %%x in ("%target%*.pdf") do (
set "name=%%~nx"
if "!name:~0,6!"=="009900" set "out009900=!out009900!%%~nxx, "
if "!name:~0,6!"=="012345" set "out012345=!out012345!%%~nxx, "
echo:%%x
)
set "out009900=%out009900:~0,-2%"
set "out012345=%out012345:~0,-2%"
echo:!out009900!>"%target%009900.csv"
echo:!out012345!>"%target%012345.csv"

Re: HELP!List the same prefix files in a txt separated by co

Posted: 01 Sep 2010 19:01
by ghostmachine4
here's a vbscript

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
   If objFS.GetExtensionName(strFile) = "pdf" Then      
      strFileName = strFile.Name
        m = InStr(strFileName,"(")
        If m > 0 Then
            number= Mid(strFileName,1,m-1)
            strNewFileName = number&".csv"
            If Not objFS.FileExists(strNewFileName) Then
                 Set objNewFile = objFS.CreateTextFile(strNewFileName,True)
            Else
               Set objNewFile = objFS.OpenTextFile(strNewFileName,8)
            End If             
            objNewFile.Write(strFileName &",")
            objNewFile.Close
        End If   
   End If   
Next


usage:

Code: Select all

c:\test> cscript //nologo myscript.vb

Re: HELP!List the same prefix files in a txt separated by co

Posted: 01 Sep 2010 19:09
by ghostmachine4
orange_batch wrote:So many repeat question types lately, hmm...
Puts all 009900*.pdf filenames CSV'ed into 009900.csv.
Puts all 012345*.pdf filenames CSV'ed into 012345.csv.


but it doesn't cater for other numbers...

Re: HELP!List the same prefix files in a txt separated by co

Posted: 01 Sep 2010 21:21
by orange_batch
Image

Re: HELP!List the same prefix files in a txt separated by co

Posted: 02 Sep 2010 06:02
by thor28
Hello, thanks for all!

In the Vb Script how can I include the file without "(x)" if I have? Like this: 009900.pdf, 009900(2).pdf, 009900(3).pdf, 012345(2).pdf, 012345(3).pdf, 012345(4).pdf >> 012345.csv

Thank you again!

Re: HELP!List the same prefix files in a txt separated by co

Posted: 02 Sep 2010 06:28
by ghostmachine4

Code: Select all

....
m = InStr(strFileName,"(")
If m > 0 Then
 ....
else
   ' if there is no "(" in the file name
   ' do your stuff here.
end if