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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thor28
Posts: 2
Joined: 01 Sep 2010 11:58

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

#1 Post by thor28 » 01 Sep 2010 12:08

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:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#2 Post by orange_batch » 01 Sep 2010 17:48

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"

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

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

#3 Post by ghostmachine4 » 01 Sep 2010 19:01

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

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

#4 Post by ghostmachine4 » 01 Sep 2010 19:09

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...

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#5 Post by orange_batch » 01 Sep 2010 21:21

Image

thor28
Posts: 2
Joined: 01 Sep 2010 11:58

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

#6 Post by thor28 » 02 Sep 2010 06:02

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!

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

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

#7 Post by ghostmachine4 » 02 Sep 2010 06:28

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     

Post Reply