DOS batch loop?

  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post October 29th, 2010, 7:13 am

Hello =-)

I'm hoping someone can help me? I have made this DOS batch file to loop 3 times:
Code: [ Select ]
FOR %%A IN (1, 2, 3) DO (
gswin32c.exe -sDEVICE=jpeg -r200 -o xxx-%%A_%%d.jpg xxx-%%A.pdf
)
  1. FOR %%A IN (1, 2, 3) DO (
  2. gswin32c.exe -sDEVICE=jpeg -r200 -o xxx-%%A_%%d.jpg xxx-%%A.pdf
  3. )


It works but instead of "FOR %%A IN (1, 2, 3)" I would rather have something like "FOR %%A (1 to 3)"

Any help would be appreciated, thanks :)
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 29th, 2010, 7:13 am

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post November 2nd, 2010, 5:26 pm

Use the /L flag. You can then specify a range of numbers with start, step, and end values:

Code: [ Select ]
for /L %%A in (1, 1, 3) do (
 gswin32c.exe -sDEVICE=jpeg -r200 -o xxx-%%A_%%d.jpg xxx-%%A.pdf
)
  1. for /L %%A in (1, 1, 3) do (
  2.  gswin32c.exe -sDEVICE=jpeg -r200 -o xxx-%%A_%%d.jpg xxx-%%A.pdf
  3. )


http://ss64.com/nt/for_l.html
The Beer Monocle. Classy.
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 3rd, 2010, 10:02 am

Perfect, thanks :iconthumbright:
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 3rd, 2010, 2:55 pm

I like this batch file, I've been adding more stuff to it. Basically it runs ghostscript to break multi-page pdf files into individual jpegs ( they are scanned documents which have to go into a database which only imports individual image files ). Afterwards it runs 7zip to archive the original pdfs to save space.

Code: [ Select ]
set client1=LastName_FirstName

rem ** make seperate directory for the jpgs

mkdir %client1%_jpgs

rem ** If LastName_FirstName/LastName_FirstName_Category1-1.pdf
rem ** has 10 pages, it becomes
rem ** LastName_FirstName_jpegs/LastName_FirstName_Category1-1_1.jpg,
rem ** ...-2.jpg, etc 
rem ** ( and so on through ...-58.pdf )

FOR /L %%A IN (1, 1, 58) DO (
gswin32c.exe -sDEVICE=jpeg -r200 -o %client1%_jpgs/%client1%_Category1-%%A_%%d.jpg %client1%\%client1%_Category1-%%A.pdf
)

FOR /L %%A IN (1, 1, 1) DO (
gswin32c.exe -sDEVICE=jpeg -r200 -o %client1%_jpgs/%client1%_Category2-%%A_%%d.jpg %client1%\%client1%_Category2-%%A.pdf
)

FOR /L %%A IN (1, 1, 4) DO (
gswin32c.exe -sDEVICE=jpeg -r200 -o %client1%_jpgs/%client1%_Category3-%%A_%%d.jpg %client1%\%client1%_Category3-%%A.pdf
)

rem *** 7zip files and move to archive

7z a -t7z %client1%.7z %client1%\

move %client1%.7z c:\ghostscript\archive
  1. set client1=LastName_FirstName
  2. rem ** make seperate directory for the jpgs
  3. mkdir %client1%_jpgs
  4. rem ** If LastName_FirstName/LastName_FirstName_Category1-1.pdf
  5. rem ** has 10 pages, it becomes
  6. rem ** LastName_FirstName_jpegs/LastName_FirstName_Category1-1_1.jpg,
  7. rem ** ...-2.jpg, etc 
  8. rem ** ( and so on through ...-58.pdf )
  9. FOR /L %%A IN (1, 1, 58) DO (
  10. gswin32c.exe -sDEVICE=jpeg -r200 -o %client1%_jpgs/%client1%_Category1-%%A_%%d.jpg %client1%\%client1%_Category1-%%A.pdf
  11. )
  12. FOR /L %%A IN (1, 1, 1) DO (
  13. gswin32c.exe -sDEVICE=jpeg -r200 -o %client1%_jpgs/%client1%_Category2-%%A_%%d.jpg %client1%\%client1%_Category2-%%A.pdf
  14. )
  15. FOR /L %%A IN (1, 1, 4) DO (
  16. gswin32c.exe -sDEVICE=jpeg -r200 -o %client1%_jpgs/%client1%_Category3-%%A_%%d.jpg %client1%\%client1%_Category3-%%A.pdf
  17. )
  18. rem *** 7zip files and move to archive
  19. 7z a -t7z %client1%.7z %client1%\
  20. move %client1%.7z c:\ghostscript\archive
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post November 3rd, 2010, 7:39 pm

I worked with GhostScript a few years ago at an old job, but I was lucky enough to be working in UNIX with a real shell language ;)
The Beer Monocle. Classy.
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 15th, 2010, 10:16 am

spork wrote:
I worked with GhostScript a few years ago at an old job, but I was lucky enough to be working in UNIX with a real shell language ;)


It would probably be idea to work with that. For now I just need something quick and dirty to get these files moving. 8)

I have another question if anyone can help. I am trying to use the "append" command for another DOS batch file.

When I googled I found this:

Quote:
append str file line -- appends a string to a specific line in a text file
-- str [in] - string to be appended
-- file [in] - file name to append the string to
-- line [in] - line number to append the string to, first line is 1, omit for last line


This is supposed to be the proper syntax. When I try it however it gives an error, something like "too many parameters". It accepts the append command alone, so I must be doing something wrong. I want to be able to go to a line number. Any help would be appreciate, thanks :)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post November 15th, 2010, 10:22 am

What is the command that you're passing?
The Beer Monocle. Classy.
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 15th, 2010, 11:54 am

I first tried this:

Quote:
set test="test"
append %test% testfile1.bat 1


( I wasn't sure about syntax but was hoping to append line 1 )

I also tried just this:

Quote:
set test="test"
append %test% testfile1.bat


Either way it responds "too many parameters".
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post November 15th, 2010, 2:20 pm

What version of Windows are you using? Where did you get "append" from? It's not part of the cmd interpreter itself and isn't a program packaged with Windows to my knowledge.

Code: [ Select ]
C:\>append
'append' is not recognized as an internal or external command,
operable program or batch file.
  1. C:\>append
  2. 'append' is not recognized as an internal or external command,
  3. operable program or batch file.


Type "where append" and paste the path it gives.
The Beer Monocle. Classy.
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 15th, 2010, 3:09 pm

Here's what I get:

Quote:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>ver

Microsoft Windows XP [Version 5.1.2600]

C:\>append

C:\>where append
'WHERE' is not recognized as an internal or external command,
operable program or batch file.

C:\>


It recognizes 'append' it seems but doesn't do anything.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post November 15th, 2010, 3:44 pm

Odd. Try "append /?"
The Beer Monocle. Classy.
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 15th, 2010, 3:50 pm

Quote:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>append /?
Allows programs to open data files in specified directories as if they were in
the current directory.

APPEND [[drive:]path[;...]] [/X[:ON | :OFF]] [/PATH:ON | /PATH:OFF] [/E]
APPEND ;

[drive:]path Specifies a drive and directory to append.
/X:ON Applies appended directories to file searches and
application execution.
/X:OFF Applies appended directories only to requests to open files.
/X:OFF is the default setting.
/PATH:ON Applies appended directories to file requests that already
specify a path. /PATH:ON is the default setting.
/PATH:OFF Turns off the effect of /PATH:ON.
/E Stores a copy of the appended directory list in an environment
variable named APPEND. /E may be used only the first time
you use APPEND after starting your system.

Type APPEND ; to clear the appended directory list.
Type APPEND without parameters to display the appended directory list.

C:\>


Guess this isn't what I was looking for :(

But thanks for your help :)
  • GhostScript
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 29, 2010
  • Posts: 13
  • Status: Offline

Post November 17th, 2010, 5:12 pm

I wanted to share a work-around I found for the append problem above ...

Here is the main file:

Code: [ Select ]
client1="LastName_FirstName"

call change2jpg.bat
  1. client1="LastName_FirstName"
  2. call change2jpg.bat


This runs the batch script in the previous post to convert to jpgs.

Code: [ Select ]
set add_ext="_jpgs"

set "client1=%client1%%add_ext%"
  1. set add_ext="_jpgs"
  2. set "client1=%client1%%add_ext%"


( the batch file above saved the jpgs to a directory "LastName_FirstName_jpgs" )

Code: [ Select ]
dir /b /o:n c:\ghostscript\%client1% >> 2_filelist1.bat


This makes a bare list of the directory contents, just the names and nothing else. I wanted to append these line by line somehow to make another file with commands to move each file, but there doesn't seem to be a way to do that with just a batch file, so I wrote a Python script to do this:

Code: [ Select ]

mkapnd ( dot ) py
  1. mkapnd ( dot ) py

"dot" = "." ( Ozzu thinks its a website, lol )

This script is at the end of this post. Basically it reads the directory listing line by line and asks which directory to move the file to, and writes a batch file with DOS commands to move to the appropriate directory ( by category ):

Code: [ Select ]

mkdir c:\ghostscript\%client1%\Category1
mkdir c:\ghostscript\%client1%\Category2
mkdir c:\ghostscript\%client1%\Category3
  1. mkdir c:\ghostscript\%client1%\Category1
  2. mkdir c:\ghostscript\%client1%\Category2
  3. mkdir c:\ghostscript\%client1%\Category3


Makes a subdirectory for each category.

Code: [ Select ]
call appended3.bat


This is the file made by the Python script. It would have something like:

move LastName_FirstName_pdf1_1_jpg c:\ghostscript\LastName_FirstName_jpgs\Category3\
move LastName_FirstName_pdf1_2_jpg c:\ghostscript\LastName_FirstName_jpgs\Category2\
move LastName_FirstName_pdf1_3_jpg c:\ghostscript\LastName_FirstName_jpgs\Category3\
(etc)

Then delete temporary files:

Code: [ Select ]

del c:\ghostscript\appended3.bat
del c:\ghostscript\2_filelist1.bat
  1. del c:\ghostscript\appended3.bat
  2. del c:\ghostscript\2_filelist1.bat


Here's the Python script from above:

Code: [ Select ]

client = "LastName_FirstName"  
backs = chr(92)
dir1 = client + "_jpgs" + backs
dir2 = r" c:\ghostscript" + backs
dir3 = client + "_jpgs"


d = {}

fp = open("appended3.bat","w")
text_file = open("2_filelist1.bat", "r")
lines = text_file.readlines()
for line in lines:
  if not line in d.keys():
    d[line] = 0
  d[line] = d[line] + 1

for line in lines:
    line = line.replace("\n","")
    
    print(line)    
    sort_option = input('Please enter a value:')
    if sort_option == "1":
      line_new = "move " + dir1 + line + dir2 + dir3 + "\Category1" + "\n"
    if sort_option == "2":
      line_new = "move " + dir1 + line + dir2 + dir3 + "\Category1" + "\n"
    if sort_option == "3":
      line_new = "move " + dir1 + line + dir2 + dir3 + "\Category1" + "\n"
    

    line2 = line_new
    fp.write(line2)
quit ()
  1. client = "LastName_FirstName"  
  2. backs = chr(92)
  3. dir1 = client + "_jpgs" + backs
  4. dir2 = r" c:\ghostscript" + backs
  5. dir3 = client + "_jpgs"
  6. d = {}
  7. fp = open("appended3.bat","w")
  8. text_file = open("2_filelist1.bat", "r")
  9. lines = text_file.readlines()
  10. for line in lines:
  11.   if not line in d.keys():
  12.     d[line] = 0
  13.   d[line] = d[line] + 1
  14. for line in lines:
  15.     line = line.replace("\n","")
  16.     
  17.     print(line)    
  18.     sort_option = input('Please enter a value:')
  19.     if sort_option == "1":
  20.       line_new = "move " + dir1 + line + dir2 + dir3 + "\Category1" + "\n"
  21.     if sort_option == "2":
  22.       line_new = "move " + dir1 + line + dir2 + dir3 + "\Category1" + "\n"
  23.     if sort_option == "3":
  24.       line_new = "move " + dir1 + line + dir2 + dir3 + "\Category1" + "\n"
  25.     
  26.     line2 = line_new
  27.     fp.write(line2)
  28. quit ()


It's kind of jenky right now, I'm going to work on it more, but it works and it's actually saved me a lot of time already.

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 102 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.